private void InitializeWithAutoSalt() { SymmetricAlgorithm symmetricAlg = CryptoCommon.GetSymmetricAlgorithm(_autoSaltCryptoAlgorithm); symmetricAlg.Padding = CryptoCommon.GetPaddingMode(_autoSaltPaddingMode); byte[] salt = _restBuffer.GetBytes(_autoSaltLength); if (_autoSaltLength == 4) // If salt length is 4, then add it twice because Rfc2898DeriveBytes supports only 8 byte salts { ByteBuffer newSalt = new ByteBuffer(); newSalt.AddBytes(salt); newSalt.AddBytes(salt); salt = newSalt.GetAllBytes(); } Rfc2898DeriveBytes key; if (_keyBytes != null) { key = new Rfc2898DeriveBytes(_keyBytes, salt, 7); } else { key = new Rfc2898DeriveBytes(_keyStr, salt, 7); } _keyStr = null; _keyBytes = null; byte[] ivBytes = key.GetBytes(symmetricAlg.BlockSize / 8); byte[] keyBytes = key.GetBytes(symmetricAlg.KeySize / 8); _keySize = symmetricAlg.KeySize; _ic = symmetricAlg.CreateDecryptor(keyBytes, ivBytes); _autosizeSaltInitialized = true; }
private void Initialize(Rfc2898DeriveBytes key, SymmetricCryptoAlgorithm cryptoAlgorithm, PaddingMode paddingMode) { _gotAllData = false; SymmetricAlgorithm symmetricAlg = CryptoCommon.GetSymmetricAlgorithm(cryptoAlgorithm); symmetricAlg.Padding = CryptoCommon.GetPaddingMode(paddingMode); byte[] ivBytes; byte[] keyBytes; lock (key) // Make key threadsafe from itself if you reuse the same one ove and over { key.Reset(); ivBytes = key.GetBytes(symmetricAlg.BlockSize / 8); keyBytes = key.GetBytes(symmetricAlg.KeySize / 8); } _keySize = symmetricAlg.KeySize; _ic = symmetricAlg.CreateDecryptor(keyBytes, ivBytes); }