How to make an encrypter and a decryptor in C#

hi all this how to is going to show you how to encrypt and decrypt you very own messages (this how to is for safely storing data and sending secure messages (both must have same key))

first: we need to make the key(a random assortment of letters numbers that translate into a single letter)
we make it like so(make it in a different file)

string[] replaceVall = new string[] {//the encrypted letter values corresponding to the bottom
				"gmzr", "mFQF", "dhHa", "vrwH", "jkmq", "PtZa", "Bf*J", "Sw-T", "+-tr", "Ag*k",
			       	"Ai#T", "+M#=", "BI~k", "ohgp", ".gpL", "NMws", "xavz", "o~MN", "Vzxq", "fqq,",
				"kz(t", "W!tk", "S-oJ", "tkOz", "z=aX", "RL*M", "t+_;", "gdW&", "%$?J", "LNPb",
				"ApYX", "Guv:", ";xPQ", "zobi", "ncOM", ";hE_", "*Kgo", "CO+Y", "vT-D", "QGW-",
				"ZRrp", "Kq-V", "GFkI", "hM#H", "V-uv", "of*v", "EHtJ", "gMCM", "yPxQ", "sOjL", 
				"PUPJ", "jnRS", "OxOG", "NRuk", "JCU+", "qaDK", "aIFX", "ZoTf", "AwuQ", "YOzt",
				"WIXQ", "YiwZ", "ReMT", "eRIC", "iwbg", "-Bu*", ":KrV", "bCFK", "HWQR", "TG#r",
				"UikE", "bOsu", "omEO", "Ia.D"
			};
			char[] alphabet = new char[] {//this is all the letters that i will use 
                                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
				'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E','F','G',
				'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
				'.',';',':',',','-','_','#','*','+','~','!','"','§','$','%','&','/','(',')','=','?',' '};

ok so now that we have that made we need to make the replacer (it finds the letter and returns the corresponding “replaceVall” value)

private string replacer(string message){
			char[] msg = new char[message.Length];
			for (int f = 0; f < msg.Length; f++) {
				Char.TryParse (message.Substring (f, 1), out msg [f]);//sets up the char array for 
                                //easier use with the key
			}
                        string end = "";//this will be the end result
			for (int x = 0; x < msg.Length; x++) {//loop for how long the message is
				for (int j = 0; j < alphabet.Length; j++) {//loop for every letter we have set up
					if (msg [x] == alphabet [j]) {//if the current letter is the same as the letter
                                        //in the "alphabet" array we know what number the encoded part is
						end += replaceVall [j];//add the encoded part the the end result
					}
				}
			}
			return end;
		}

so now we make the main function for the encrypter

public string encode(string message,int layers){
			string end_message = "";
			string temp_layer = message;



			for (int x = 0; x < layers; x++) {//loop for the set amount of layers
				temp_layer = replacer (temp_layer);//this is for layering
			}
			end_message = temp_layer;
			return end_message;
		}

now we can encode messages but now we need a way to decode them make a new file and enter this

string[] replaceVall = new string[] {//the encripted letter vallue's corosponding to the bottom
			"gmzr", "mFQF", "dhHa", "vrwH", "jkmq", "PtZa", "Bf*J", "Sw-T", "+-tr", "Ag*k",
			"Ai#T", "+M#=", "BI~k", "ohgp", ".gpL", "NMws", "xavz", "o~MN", "Vzxq", "fqq,",
			"kz(t", "W!tk", "S-oJ", "tkOz", "z=aX", "RL*M", "t+_;", "gdW&", "%$?J", "LNPb",
			"ApYX", "Guv:", ";xPQ", "zobi", "ncOM", ";hE_", "*Kgo", "CO+Y", "vT-D", "QGW-",
			"ZRrp", "Kq-V", "GFkI", "hM#H", "V-uv", "of*v", "EHtJ", "gMCM", "yPxQ", "sOjL", 
			"PUPJ", "jnRS", "OxOG", "NRuk", "JCU+", "qaDK", "aIFX", "ZoTf", "AwuQ", "YOzt",
			"WIXQ", "YiwZ", "ReMT", "eRIC", "iwbg", "-Bu*", ":KrV", "bCFK", "HWQR", "TG#r",
			"UikE", "bOsu", "omEO", "Ia.D"
		};
		char[] alphabet = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
			'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E','F','G',
			'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
			'.',';',':',',','-','_','#','*','+','~','!','"','§','$','%','&','/','(',')','=','?',' '};
		private string remove_layer(string message){
			string end = "";

			for (int x = 0; x < message.Length / 4; x++) {
				string line = message.Substring (x * 4,4);//get the next 4 digits
				for (int j = 0; j < replaceVall.Length; j++) {
					if (line.StartsWith( replaceVall [j])) {//this is the same as on top just in revers
						end += alphabet [j];
					}
				}
			}
			return end;
		}
		public string decode(string message){
			while (true) {
				bool islayer = false;
				for(int x=0;x<replaceVall.Length;x++){
					if (message.StartsWith(replaceVall[x])) {//check to see if there is another layer
                                        //if so then go agen
						islayer = true;
						break;
					}
				}
				if (islayer) {
					message = remove_layer (message);//if there is a layer decode agen

				} else {
					break;//if there is no more layers exit
				}
			}
			return message;
		}

ok now the main decoder is done know we need a main menu

public static void Main (string[] args)
		{
			top:
			Console.ForegroundColor = ConsoleColor.Green;
			Console.WriteLine ("!encoder and decoder!");
			Console.Title="encoder and decoder1.0";
			Console.WriteLine ("1: encode message");
			Console.WriteLine ("2: decode message");//menu selection screen
			string option = Console.ReadLine();
			Console.Clear ();

			if (option == "1") {
                                Console.WriteLine ("message you want to encode");
				string msg = Console.ReadLine ();
				Console.WriteLine ("layers of encription(max 4)");//select settings
				Console.WriteLine ("the more layers the longer the encode message will be");
				Console.WriteLine ("but it will be more secure");
				int x = 0;
				string layers = Console.ReadLine ();
				try{
					Int32.TryParse (layers, out x);//convert the layers string to an int
				}catch{
					Console.WriteLine ("must be a int e.g: 1(max 4)");
					goto top;
				}
				if (x <= 4) {
					var link = new encode_advanced ();
					string res = link.encode (msg, x);
					Console.WriteLine ("length:" + res.Length);//this is a safety feature because  
					Console.WriteLine ("message:");//the coder will fup after 4 layers e.g 5 (or up)
					Console.WriteLine (res);
				} else {
					Console.WriteLine ("must be a int e.g: 1(max 4)");
					goto top;
				}
			} else if (option == "2") {
				Console.WriteLine ("message you want to decode");
				string msg = Console.ReadLine ();
				var link = new decode_advanced ();//just enter the message you want to decode
				Console.WriteLine ("#Running");
				string res = link.decode (msg);
				Console.Clear ();
				Console.WriteLine (res);
			} 
			goto top;		
		}

bye for now and i hope you learnt some C# from this
warnings:

if the layer is above 4 it will fail
to send messages to your friends both of you will need the same key,
if you have the same key as the tutorial your message isn’t secure
i suggest making one from scratch,
if the decoder doesn’t work first try try again if it still doesn’t work make a new key,

disclaimer:

i don’t condone in miss use of this program under any circumstances
this tutorial is made for educational purposes no more no less

2 Likes

The syntax highlighting is better, but the indendation is off. Could you sort that?

This topic was automatically closed after 30 days. New replies are no longer allowed.