Esempio n. 1
0
 /// <summary>
 /// Using this instances parameters, create a decryptor
 /// </summary>
 /// <returns>A new decrypting transformation instance</returns>
 public override ICryptoTransform DecryptingTransform()
 {
     using (SymmetricAlgorithm aes = CreateAlgorithmInternal())
     {
         aes.Mode    = CipherMode.CBC;
         aes.Padding = PaddingMode.PKCS7;
         return(aes.CreateDecryptingTransform());
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Decrypt in one operation.
        /// </summary>
        /// <param name="cipherText">The complete cipher text</param>
        /// <returns>The decrypted result minus any padding</returns>
        public override byte[] Decrypt(byte[] cipherText)
        {
            if (cipherText == null)
            {
                throw new ArgumentNullException("cipherText");
            }

            using (SymmetricAlgorithm aes = CreateAlgorithmInternal())
            {
                aes.Mode    = CipherMode.CBC;
                aes.Padding = PaddingMode.None;
                using (ICryptoTransform decryptor = aes.CreateDecryptingTransform())
                {
                    byte[] plaintext = decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length);
                    return(plaintext);
                }
            }
        }
        public BlockAlgorithmKeyWrapTransform(SymmetricAlgorithm symmetricAlgorithm, Salt salt, KeyWrapDirection keyWrapDirection)
        {
            if (symmetricAlgorithm == null)
            {
                throw new ArgumentNullException("symmetricAlgorithm");
            }
            if (salt == null)
            {
                throw new ArgumentNullException("salt");
            }

            if (salt.Length != 0 && salt.Length < symmetricAlgorithm.Key().Length)
            {
                throw new InternalErrorException("Salt is too short. It must be at least as long as the algorithm key, or empty for no salt.");
            }
            _algorithm = symmetricAlgorithm;

            byte[] saltedKey = _algorithm.Key();
            saltedKey.Xor(salt.GetBytes().Reduce(saltedKey.Length));
            _algorithm.SetKey(saltedKey);

            _algorithm.Mode    = CipherMode.ECB;
            _algorithm.Padding = PaddingMode.None;

            BlockLength = _algorithm.BlockSize / 8;

            _transform = keyWrapDirection == KeyWrapDirection.Encrypt ? _algorithm.CreateEncryptingTransform() : _algorithm.CreateDecryptingTransform();
        }