Beispiel #1
0
        protected override void EncryptRecord(DisposeContext d, IBufferOffsetSize input)
        {
            ICryptoTransform cipher;

            if (!Cipher.HasFixedIV)
            {
                EncryptionAlgorithm.GenerateIV();
                cipher = d.Add(EncryptionAlgorithm.CreateEncryptor());
            }
            else
            {
                cipher = encryptionCipher;
            }

            if (!Cipher.HasFixedIV)
            {
                Buffer.BlockCopy(EncryptionAlgorithm.IV, 0, input.Buffer, input.Offset, BlockSize);
            }

            var ret = cipher.TransformBlock(input.Buffer, input.Offset + HeaderSize, input.Size - HeaderSize, input.Buffer, input.Offset + HeaderSize);

            if (ret <= 0 || ret != input.Size - HeaderSize)
            {
                throw new InvalidOperationException();
            }

            if (Cipher.HasFixedIV)
            {
                var IV = new byte [BlockSize];
                Buffer.BlockCopy(input.Buffer, input.Offset + input.Size - BlockSize, IV, 0, BlockSize);
                EncryptionAlgorithm.IV = IV;
            }
        }
Beispiel #2
0
        public static byte[] EncryptOrDecryptData(bool encrypt, byte[] data, int start, int length)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            ICryptoTransform cryptoTransform = (encrypt ? EncryptionAlgorithm.CreateEncryptor() :
                                                EncryptionAlgorithm.CreateDecryptor());
            MemoryStream stream = new MemoryStream();

            using (CryptoStream cryptoStream = new CryptoStream(stream, cryptoTransform, CryptoStreamMode.Write))
            {
                cryptoStream.Write(data, start, length);
                cryptoStream.FlushFinalBlock();

                return(stream.ToArray());
            }
        }
Beispiel #3
0
        protected override void EncryptRecord(DisposeContext d, IBufferOffsetSize buffer)
        {
            ICryptoTransform cipher;

            if (!Cipher.HasFixedIV)
            {
                EncryptionAlgorithm.GenerateIV();
                cipher = d.Add(EncryptionAlgorithm.CreateEncryptor());
            }
            else
            {
                cipher = encryptionCipher;
            }

            if (!Cipher.HasFixedIV)
            {
                Buffer.BlockCopy(EncryptionAlgorithm.IV, 0, buffer.Buffer, buffer.Offset, BlockSize);
            }

            cipher.TransformBlock(buffer.Buffer, buffer.Offset + HeaderSize, buffer.Size - HeaderSize, buffer.Buffer, buffer.Offset + HeaderSize);
        }
Beispiel #4
0
        public override void InitializeCipher()
        {
                        #if DEBUG_FULL
            if (Cipher.EnableDebugging)
            {
                DebugHelper.WriteLine("INITIALIZE CIPHER: {0}", BlockSize);
            }
                        #endif

            EncryptionAlgorithm = CreateEncryptionAlgorithm(true);
            DecryptionAlgorithm = CreateEncryptionAlgorithm(false);

            // Legacy mode for TLS 1.0
            if (Cipher.HasFixedIV)
            {
                EncryptionAlgorithm.IV = (IsClient ? ClientWriteIV : ServerWriteIV).Buffer;
                DecryptionAlgorithm.IV = (IsClient ? ServerWriteIV : ClientWriteIV).Buffer;

                encryptionCipher = Add(EncryptionAlgorithm.CreateEncryptor());
                decryptionCipher = Add(DecryptionAlgorithm.CreateDecryptor());
            }

            base.InitializeCipher();
        }
 private void InitializeEncryptor(byte[] key, byte[] initializationVector)
 {
     Encryptor = EncryptionAlgorithm.CreateEncryptor(key, initializationVector);
     EncryptionAlgorithm.Padding = PaddingMode.PKCS7;
 }