/// <summary>
 /// 
 /// </summary>
 /// <param name="_algorithm"></param>
 public SymmetricCryptoServiceProvider(SymmetricCryptoAlgorithm _algorithm)
 {
     //
     // TODO: Add constructor logic here
     //
     algorithm__ = _algorithm;
 }
Exemple #2
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);
        }
Exemple #3
0
        public DecryptionBuffer(string password, byte[] salt, SymmetricCryptoAlgorithm cryptoAlgorithm = SymmetricCryptoAlgorithm.AES_256_CBC, PaddingMode paddingMode = PaddingMode.PKCS7)
        {
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, 7);

            Initialize(key, cryptoAlgorithm, paddingMode);
        }
Exemple #4
0
 public DecryptionBuffer(Rfc2898DeriveBytes key, SymmetricCryptoAlgorithm cryptoAlgorithm = SymmetricCryptoAlgorithm.AES_256_CBC, PaddingMode paddingMode = PaddingMode.PKCS7)
 {
     Initialize(key, cryptoAlgorithm, paddingMode);
 }
Exemple #5
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));
 }
Exemple #6
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));
 }
Exemple #7
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));
 }
Exemple #8
0
        public static byte[] DecryptData(string password, AutoSaltSizes saltSize, byte[] data, SymmetricCryptoAlgorithm algorithm = SymmetricCryptoAlgorithm.AES_256_CBC)
        {
            DecryptionBuffer decBuffer = new DecryptionBuffer(password, saltSize, algorithm);

            decBuffer.AddData(data, true);
            return(decBuffer.GetData());
        }
Exemple #9
0
 public static void DecryptFile(string password, AutoSaltSizes saltSize, string inputFileName, string outputFileName, SymmetricCryptoAlgorithm algorithm = SymmetricCryptoAlgorithm.AES_256_CBC)
 {
     using (FileStream inputStream = new FileStream(inputFileName, FileMode.Open, FileAccess.Read))
     {
         using (FileStream outputStream = new FileStream(outputFileName, FileMode.Create, FileAccess.Write))
         {
             DecryptStream(password, saltSize, inputStream, outputStream, algorithm);
         }
     }
 }
Exemple #10
0
        public static void DecryptStream(string password, AutoSaltSizes saltSize, Stream inputStream, Stream outputStream, SymmetricCryptoAlgorithm algorithm = SymmetricCryptoAlgorithm.AES_256_CBC)
        {
            if (!inputStream.CanRead)
            {
                throw new Exception("The input stream has to support read");
            }
            if (!outputStream.CanWrite)
            {
                throw new Exception("The output stream has to support write");
            }

            DecryptionBuffer decBuffer = new DecryptionBuffer(password, saltSize, algorithm);

            byte[] readBuffer = new byte[500000];
            bool   isLastData = false;

            while (!isLastData)
            {
                int nrOfBytes = inputStream.Read(readBuffer, 0, readBuffer.Length);
                isLastData = (nrOfBytes == 0);

                decBuffer.AddData(readBuffer, 0, nrOfBytes, isLastData);
                byte[] decryptedData = decBuffer.GetData();
                outputStream.Write(decryptedData, 0, decryptedData.Length);
            }
        }