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();
        }
Esempio n. 2
0
 /// <summary>
 /// Using this instances parameters, create an encryptor
 /// </summary>
 /// <returns>A new encrypting transformation instance</returns>
 public override ICryptoTransform EncryptingTransform()
 {
     using (SymmetricAlgorithm aes = CreateAlgorithmInternal())
     {
         aes.Mode    = CipherMode.CBC;
         aes.Padding = PaddingMode.PKCS7;
         return(aes.CreateEncryptingTransform());
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Encrypt
        /// </summary>
        /// <param name="plaintext">The complete plaintext bytes</param>
        /// <returns>The cipher text, complete with any padding</returns>
        public override byte[] Encrypt(byte[] plaintext)
        {
            if (plaintext == null)
            {
                throw new ArgumentNullException("plaintext");
            }

            using (SymmetricAlgorithm aes = CreateAlgorithmInternal())
            {
                aes.Mode    = CipherMode.CBC;
                aes.Padding = PaddingMode.None;
                using (ICryptoTransform encryptor = aes.CreateEncryptingTransform())
                {
                    byte[] cipherText = encryptor.TransformFinalBlock(plaintext, 0, plaintext.Length);
                    return(cipherText);
                }
            }
        }
        public CounterModeCryptoTransform(SymmetricAlgorithm algorithm, long blockCounter, int blockOffset)
        {
            if (algorithm == null)
            {
                throw new ArgumentNullException("algorithm");
            }

            if (algorithm.Mode != CipherMode.ECB)
            {
                algorithm.Clear();
                throw new ArgumentException("The algorithm must be in ECB mode.");
            }
            if (algorithm.Padding != PaddingMode.None)
            {
                algorithm.Clear();
                throw new ArgumentException("The algorithm must be set to work without padding.");
            }
            _algorithm         = algorithm;
            _startBlockCounter = _currentBlockCounter = blockCounter;
            _startBlockOffset  = _currentBlockOffset = blockOffset;

            _cryptoTransform = _algorithm.CreateEncryptingTransform();
            _blockLength     = _cryptoTransform.InputBlockSize;
        }