Exemplo n.º 1
0
        private byte[] EncryptIV(byte[] iv)
        {
            byte[] paddedIV = PadUtils.Pad(iv, 16, 0);

            using (RijndaelManaged rijndael = new RijndaelManaged())
            {
                rijndael.BlockSize = 128;
                rijndael.Mode      = CipherMode.ECB;
                rijndael.Padding   = PaddingMode.None;

                using (ICryptoTransform encryptor = rijndael.CreateEncryptor(_encryptionKey, null))
                    using (MemoryStream encryptedStream = new MemoryStream(paddedIV.Length * 2))
                        using (CryptoStream cryptoStream = new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(paddedIV, 0, paddedIV.Length);
                            return(encryptedStream.ToArray());
                        }
            }
        }
Exemplo n.º 2
0
        private byte[] EncryptValue(byte[] value, out byte[] iv)
        {
            byte[] paddedValue = PadUtils.Pad(value, 16, 0);

            using (RijndaelManaged rijndael = new RijndaelManaged())
            {
                rijndael.BlockSize = 128;
                rijndael.Mode      = CipherMode.CBC;
                rijndael.Padding   = PaddingMode.None;
                rijndael.GenerateIV();

                using (ICryptoTransform encryptor = rijndael.CreateEncryptor(_encryptionKey, rijndael.IV))
                    using (MemoryStream encryptedStream = new MemoryStream(paddedValue.Length * 2))
                        using (CryptoStream cryptoStream = new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(paddedValue, 0, paddedValue.Length);

                            iv = rijndael.IV;
                            return(encryptedStream.ToArray());
                        }
            }
        }