private static Stream CreateStream(Stream s, bool bEncrypt, byte[] pbKey, byte[] pbIV) { ValidateArguments(s, bEncrypt, pbKey, pbIV); Serpent f = new SerpentManaged(); byte[] pbLocalIV = new byte[16]; Array.Copy(pbIV, pbLocalIV, 16); f.IV = pbLocalIV; byte[] pbLocalKey = new byte[32]; Array.Copy(pbKey, pbLocalKey, 32); f.KeySize = 256; f.Key = pbLocalKey; f.Mode = m_rCipherMode; f.Padding = m_rCipherPadding; ICryptoTransform iTransform = (bEncrypt ? f.CreateEncryptor() : f.CreateDecryptor()); Debug.Assert(iTransform != null); if (iTransform == null) { throw new SecurityException("Unable to create Serpent transform!"); } return(new CryptoStream(s, iTransform, bEncrypt ? CryptoStreamMode.Write : CryptoStreamMode.Read)); }
public static bool TestSerpent2() { SerpentManaged serpent = new SerpentManaged(); serpent.Padding = PaddingMode.None; serpent.Mode = CipherMode.ECB; byte[] key = new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] plaintext = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] ciphertext = new byte[] { 0xa2, 0x23, 0xaa, 0x12, 0x88, 0x46, 0x3c, 0x0e, 0x2b, 0xe3, 0x8e, 0xbd, 0x82, 0x56, 0x16, 0xc0 }; byte[] initializationVector = new byte[16]; ICryptoTransform encryptor = serpent.CreateEncryptor(key, initializationVector); ICryptoTransform decryptor = serpent.CreateDecryptor(key, initializationVector); byte[] result1 = new byte[16]; byte[] result2 = new byte[16]; encryptor.TransformBlock(plaintext, 0, 16, result1, 0); decryptor.TransformBlock(ciphertext, 0, 16, result2, 0); return(ByteUtils.AreByteArraysEqual(result1, ciphertext) && ByteUtils.AreByteArraysEqual(result2, plaintext)); }
public static bool TestSerpent() { SerpentManaged serpent = new SerpentManaged(); serpent.Padding = PaddingMode.None; serpent.Mode = CipherMode.ECB; byte[] key = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; byte[] plaintext = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; byte[] ciphertext = new byte[] { 0xde, 0x26, 0x9f, 0xf8, 0x33, 0xe4, 0x32, 0xb8, 0x5b, 0x2e, 0x88, 0xd2, 0x70, 0x1c, 0xe7, 0x5c }; byte[] initializationVector = new byte[16]; ICryptoTransform encryptor = serpent.CreateEncryptor(key, initializationVector); ICryptoTransform decryptor = serpent.CreateDecryptor(key, initializationVector); byte[] result1 = new byte[16]; byte[] result2 = new byte[16]; encryptor.TransformBlock(plaintext, 0, 16, result1, 0); decryptor.TransformBlock(ciphertext, 0, 16, result2, 0); return(ByteUtils.AreByteArraysEqual(result1, ciphertext) && ByteUtils.AreByteArraysEqual(result2, plaintext)); }
public static List <KeyValuePairList <SymmetricAlgorithm, byte[]> > GetAlgorithms(byte[] key) { // AesCryptoServiceProvider will use AES-NI if availible, but Rijndael is almost 3 times faster if AES-NI is not available. Rijndael aes = Rijndael.Create(); aes.Padding = PaddingMode.None; aes.Mode = CipherMode.ECB; SerpentManaged serpent = new SerpentManaged(); serpent.Padding = PaddingMode.None; serpent.Mode = CipherMode.ECB; TwofishManaged twofish = new TwofishManaged(); twofish.Padding = PaddingMode.None; twofish.Mode = CipherMode.ECB; List <KeyValuePairList <SymmetricAlgorithm, byte[]> > algorithms = new List <KeyValuePairList <SymmetricAlgorithm, byte[]> >(); KeyValuePairList <SymmetricAlgorithm, byte[]> aes_only = new KeyValuePairList <SymmetricAlgorithm, byte[]>(); aes_only.Add(aes, new byte[64]); algorithms.Add(aes_only); KeyValuePairList <SymmetricAlgorithm, byte[]> serpent_only = new KeyValuePairList <SymmetricAlgorithm, byte[]>(); serpent_only.Add(serpent, new byte[64]); algorithms.Add(serpent_only); KeyValuePairList <SymmetricAlgorithm, byte[]> twofish_only = new KeyValuePairList <SymmetricAlgorithm, byte[]>(); twofish_only.Add(twofish, new byte[64]); algorithms.Add(twofish_only); KeyValuePairList <SymmetricAlgorithm, byte[]> twofish_aes = new KeyValuePairList <SymmetricAlgorithm, byte[]>(); twofish_aes.Add(twofish, new byte[64]); twofish_aes.Add(aes, new byte[64]); algorithms.Add(twofish_aes); KeyValuePairList <SymmetricAlgorithm, byte[]> serpent_twofish_aes = new KeyValuePairList <SymmetricAlgorithm, byte[]>(); serpent_twofish_aes.Add(serpent, new byte[64]); serpent_twofish_aes.Add(twofish, new byte[64]); serpent_twofish_aes.Add(aes, new byte[64]); algorithms.Add(serpent_twofish_aes); KeyValuePairList <SymmetricAlgorithm, byte[]> aes_serpent = new KeyValuePairList <SymmetricAlgorithm, byte[]>(); aes_serpent.Add(aes, new byte[64]); aes_serpent.Add(serpent, new byte[64]); algorithms.Add(aes_serpent); KeyValuePairList <SymmetricAlgorithm, byte[]> aes_twofish_serpent = new KeyValuePairList <SymmetricAlgorithm, byte[]>(); aes_twofish_serpent.Add(aes, new byte[64]); aes_twofish_serpent.Add(twofish, new byte[64]); aes_twofish_serpent.Add(serpent, new byte[64]); algorithms.Add(aes_twofish_serpent); KeyValuePairList <SymmetricAlgorithm, byte[]> serpent_twofish = new KeyValuePairList <SymmetricAlgorithm, byte[]>(); serpent_twofish.Add(serpent, new byte[64]); serpent_twofish.Add(twofish, new byte[64]); algorithms.Add(serpent_twofish); foreach (KeyValuePairList <SymmetricAlgorithm, byte[]> algorithm in algorithms) { AssignKey(algorithm, key); } return(algorithms); }