// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Text; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Prng; using Org.BouncyCastle.Security; namespace Encrypt { public class TestSymmetric { // generate random IV private static IRandomGenerator rng = new CryptoApiRandomGenerator(); private static byte[] GenIV(int size) { byte[] res = new byte[size]; rng.NextBytes(res); return res; } // encrypt: byte[] plain + byte[] key -> byte[] cipher // decrypt: byte[] cipher + byte[] key -> byte[] plain public static byte[] Encrypt(string alg, int blksize, byte[] plain, byte[] key) { byte[] iv = GenIV(blksize); IBufferedCipher encrypt = CipherUtilities.GetCipher(alg); ICipherParameters param = new ParametersWithIV(new KeyParameter(key), iv); encrypt.Init(true, param); byte[] temp = encrypt.DoFinal(plain); byte[] res = new byte[iv.Length + temp.Length]; Array.Copy(iv, 0, res, 0, iv.Length); Array.Copy(temp, 0, res, iv.Length, temp.Length); return res; } public static byte[] Decrypt(string alg, int blksize, byte[] cipher, byte[] key) { byte[] iv = new byte[blksize]; Array.Copy(cipher, 0, iv, 0, iv.Length); byte[] temp = new byte[cipher.Length - iv.Length]; Array.Copy(cipher, iv.Length, temp, 0, temp.Length); IBufferedCipher decrypt = CipherUtilities.GetCipher(alg); ICipherParameters param = new ParametersWithIV(new KeyParameter(key), iv); decrypt.Init(false, param); return decrypt.DoFinal(temp); } // Base64 encode: byte[] -> string // Base64 decode: string -> byte[] private static string Encode(byte[] b) { return Convert.ToBase64String(b); } private static byte[] Decode(string s) { return Convert.FromBase64String(s); } // convert string key to byte[] key private static byte[] GenKey(string key, int keysize) { IDigest ha = new Sha256Digest(); byte[] ba = Encoding.UTF8.GetBytes(key); ha.BlockUpdate(ba, 0, ba.Length); byte[] hash = new byte[32]; ha.DoFinal(hash, 0); byte[] res = new Byte[keysize]; Array.Copy(hash, 0, res, 0, keysize); return res; } // encrypt: string plain + string key -> Base64 string cipher // decrypt: Base64 string cipher + string key -> string plain public static string Encrypt(string alg, int blksize, string plain, string key, int keysize) { return Encode(Encrypt(alg, blksize, Encoding.UTF8.GetBytes(plain), GenKey(key, keysize))); } public static string Decrypt(string alg, int blksize, string cipher, string key, int keysize) { return Encoding.UTF8.GetString(Decrypt(alg, blksize, Decode(cipher), GenKey(key, keysize))); } // test private const string TEXT = "This is some random text to be used to test encryption."; private const string KEY = "This is a very long and super secret key that should really be like 4 times as long as keysize"; private static void Test(string label, string alg, int keysize, int blksize) { Console.WriteLine(label + ":"); Console.WriteLine(" " + TEXT); string cipher = Encrypt(alg, blksize, TEXT, KEY, keysize); Console.WriteLine(" " + cipher); string plain = Decrypt(alg, blksize, cipher, KEY, keysize); Console.WriteLine(" " + plain); } private static void Test2() { if(Decrypt("AES/CBC/PKCS7", 16, "Xc252As4sZuj12tLJtDk2l60tHxu1hk4FTB0l9IK/q96cWd+Uop4lXcId1hGEmm0SN1Xjua4YqVlr3rvT+Mw4hStrnz3h8TEfdENfUMqLMY=", KEY, 16) != TEXT) { throw new Exception("Ooops"); } } public static void Main(string[] args) { Test("AES 128 bit", "AES/CBC/PKCS7", 16, 16); Test("AES 192 bit", "AES/CBC/PKCS7", 24, 16); Test("AES 256 bit", "AES/CBC/PKCS7", 32, 16); Test("3DES 168 bit", "DesEde/CBC/PKCS7", 24, 8); Test("Blowfish 256 bit", "Blowfish/CBC/PKCS7", 32, 8); Test("Twofish 128 bit", "Twofish/CBC/PKCS7", 16, 16); Test("Twofish 192 bit", "Twofish/CBC/PKCS7", 24, 16); Test("Twofish 256 bit", "Twofish/CBC/PKCS7", 32, 16); Test("CAST5 128 bit", "Cast5/CBC/PKCS7", 16, 8); Test("IDEA 128 bit", "Idea/CBC/PKCS7", 16, 8); Test2(); } } }