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));
        }
Beispiel #2
0
        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));
        }
Beispiel #3
0
        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));
        }