コード例 #1
0
        public DecryptionBuffer(string password, string salt, SymmetricCryptoAlgorithm cryptoAlgorithm = SymmetricCryptoAlgorithm.AES_256_CBC, PaddingMode paddingMode = PaddingMode.PKCS7)
        {
            byte[]             saltValueBytes = CryptoCommon.GetBytes(salt);
            Rfc2898DeriveBytes key            = new Rfc2898DeriveBytes(password, saltValueBytes, 7);

            Initialize(key, cryptoAlgorithm, paddingMode);
        }
コード例 #2
0
        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;
        }
コード例 #3
0
        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);
        }
コード例 #4
0
 public static string DecryptBase64String(string password, AutoSaltSizes saltSize, string b64StrData, SymmetricCryptoAlgorithm algorithm = SymmetricCryptoAlgorithm.AES_256_CBC)
 {
     byte[] data          = Convert.FromBase64String(b64StrData);
     byte[] encryptedData = DecryptData(password, saltSize, data, algorithm);
     return(CryptoCommon.GetString(encryptedData));
 }
コード例 #5
0
 public static string EncryptBase64String(string password, string salt, string strData, SymmetricCryptoAlgorithm algorithm = SymmetricCryptoAlgorithm.AES_256_CBC)
 {
     byte[] data          = CryptoCommon.GetBytes(strData);
     byte[] encryptedData = EncryptData(password, salt, data, algorithm);
     return(Convert.ToBase64String(encryptedData));
 }
コード例 #6
0
 public static string EncryptString(string password, AutoSaltSizes saltSize, string strData, SymmetricCryptoAlgorithm algorithm = SymmetricCryptoAlgorithm.AES_256_CBC)
 {
     byte[] data          = CryptoCommon.GetBytes(strData);
     byte[] encryptedData = EncryptData(password, saltSize, data, algorithm);
     return(CryptoCommon.GetString(encryptedData));
 }