/// <summary> /// Used to decrypt something encrypted in AES /// </summary> /// <param name="key">AES Key received from the server</param> /// <param name="result">Encrypted byte array you want to decrypt</param> /// <returns>Decrypted version of "result"</returns> public static byte[] DecryptAES(byte[] key, byte[] result) { OpenSSL.Crypto.CipherContext d_ctx = new OpenSSL.Crypto.CipherContext(OpenSSL.Crypto.Cipher.AES_128_CBC); byte[] var = new byte[16]; for (int i = 0; i < 16; i++) { var[i] = key[i + 16]; } return(d_ctx.Decrypt(result, key, var)); }
/// <summary> /// EncryptAES is being used for encrypting the password sent to the authentication server /// </summary> /// <param name="key">AES Key received from the server</param> /// <param name="hash">Actually the string you want to encrypt</param> /// <returns>Encrypted byte[] of hash</returns> public static byte[] EncryptAES(byte[] key, string hash) { OpenSSL.Crypto.CipherContext d_ctx = new OpenSSL.Crypto.CipherContext(OpenSSL.Crypto.Cipher.AES_128_CBC); byte[] var = new byte[16]; for (int i = 0; i < 16; i++) { var[i] = key[i + 16]; } return(d_ctx.Encrypt(Encoding.Default.GetBytes(hash), key, var)); }
static void Main(string[] args) { if (args.Length == 0) { System.Console.WriteLine("Please enter a numeric argument."); Environment.Exit(0); } Console.WriteLine("Encrypt = 0 | Decrypt = 1: "); int mode = int.Parse(Console.ReadLine()); Console.WriteLine("key: " + args[0]); string s = args[0]; if (s.Length > 16 || s.Length == 0) { Console.WriteLine("Too looooooooooooong! (or short)"); Environment.Exit(0); } byte[] key = System.Text.Encoding.ASCII.GetBytes(s); Console.WriteLine("vector: " + args[1]); s = args[1]; if (s.Length > 16 || s.Length == 0) { Console.WriteLine("Too looooooooooooong! (or short)"); Environment.Exit(0); } byte[] vec = System.Text.Encoding.ASCII.GetBytes(s); Console.Write("path: "); s = @Console.ReadLine(); OpenSSL.Crypto.CipherContext ctx = new OpenSSL.Crypto.CipherContext(OpenSSL.Crypto.Cipher.AES_128_CTR); String line; using (StreamReader sr = new StreamReader(s)) { line = sr.ReadToEnd(); //Console.WriteLine(line); } string answer = "mode error"; if (mode == 1) { byte[] msg = Convert.FromBase64String(line); byte[] dec = ctx.Decrypt(msg, key, vec); answer = new string(System.Text.Encoding.ASCII.GetChars(dec)); //Console.WriteLine(kak); } else if (mode == 0) { byte[] msg = System.Text.Encoding.ASCII.GetBytes(line); byte[] enc = ctx.Encrypt(msg, key, vec); answer = Convert.ToBase64String(enc); } Console.WriteLine(answer); Console.ReadKey(); }
static void Main(string[] args) { int mode = (args[0] == "e" ? 0 : 1); string key_string = args[1]; if (key_string.Length > 16 || key_string.Length == 0) { Console.WriteLine("Key length must be in range [1, 16], not " + args[1].Length); Environment.Exit(0); } byte[] key = Encoding.ASCII.GetBytes(key_string); key_string = args[2]; if (key_string.Length > 16 || key_string.Length == 0) { Console.WriteLine("Vector length must be in range [1, 16]" + args[2].Length); Environment.Exit(0); } byte[] vector = Encoding.ASCII.GetBytes(key_string); string path = @"D:\Универ\Захист\Lab_5\Lab_5\Text\1.txt"; OpenSSL.Crypto.CipherContext ctx = new OpenSSL.Crypto.CipherContext(OpenSSL.Crypto.Cipher.AES_128_CTR); string line; using (StreamReader sr = new StreamReader(path)) { line = sr.ReadToEnd(); } string answer = ""; if (mode == 1) // decryption { byte[] msg = Convert.FromBase64String(line); byte[] decrypted = ctx.Decrypt(msg, key, vector); answer = new string(System.Text.Encoding.ASCII.GetChars(decrypted)); } else if (mode == 0) // encryption { byte[] msg = Encoding.ASCII.GetBytes(line); byte[] enc = ctx.Encrypt(msg, key, vector); answer = Convert.ToBase64String(enc); } Console.WriteLine(answer); Console.ReadKey(); }
/// <summary> /// EncryptAES is being used for encrypting the password sent to the authentication server /// </summary> /// <param name="key">AES Key received from the server</param> /// <param name="hash">Actually the string you want to encrypt</param> /// <returns>Encrypted byte[] of hash</returns> public static byte[] EncryptAES(byte[] key, string hash) { OpenSSL.Crypto.CipherContext d_ctx = new OpenSSL.Crypto.CipherContext( OpenSSL.Crypto.Cipher.AES_128_CBC ); byte[] var = new byte[16]; for ( int i = 0; i < 16; i++ ) { var[i] = key[i + 16]; } return d_ctx.Encrypt( Encoding.Default.GetBytes( hash ), key, var ); }
/// <summary> /// Used to decrypt something encrypted in AES /// </summary> /// <param name="key">AES Key received from the server</param> /// <param name="result">Encrypted byte array you want to decrypt</param> /// <returns>Decrypted version of "result"</returns> public static byte[] DecryptAES(byte[] key, byte[] result) { OpenSSL.Crypto.CipherContext d_ctx = new OpenSSL.Crypto.CipherContext( OpenSSL.Crypto.Cipher.AES_128_CBC ); byte[] var = new byte[16]; for ( int i = 0; i < 16; i++ ) { var[i] = key[i + 16]; } return d_ctx.Decrypt( result, key, var ); }