예제 #1
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());
        }
예제 #2
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);
            }
        }
예제 #3
0
 public DecryptionBuffer(byte[] keyBytes, AutoSaltSizes saltSize, SymmetricCryptoAlgorithm cryptoAlgorithm = SymmetricCryptoAlgorithm.AES_256_CBC, PaddingMode paddingMode = PaddingMode.PKCS7)
 {
     _autoSaltLength          = (int)saltSize;
     _autoSaltCryptoAlgorithm = cryptoAlgorithm;
     _keyBytes            = keyBytes;
     _autosizeSalt        = true;
     _autoSaltPaddingMode = paddingMode;
 }
예제 #4
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);
         }
     }
 }
예제 #5
0
        public EncryptionBuffer(byte[] keyBytes, AutoSaltSizes saltSize, SymmetricCryptoAlgorithm cryptoAlgorithm = SymmetricCryptoAlgorithm.AES_256_CBC, PaddingMode paddingMode = PaddingMode.PKCS7)
        {
            byte[] salt = new byte[(int)saltSize];
            using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
                rng.GetBytes(salt);
            _outBuffer.AddBytes(salt);
            if (saltSize == AutoSaltSizes.Salt32)
            {
                // Since the smallest supported salt in Rfc2898DeriveBytes is 8 bytes we write the 4 byte salt twice
                ByteBuffer newSalt = new ByteBuffer();
                newSalt.AddBytes(salt);
                newSalt.AddBytes(salt);
                salt = newSalt.GetAllBytes();
            }
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(keyBytes, salt, 7);

            Initialize(key, cryptoAlgorithm, paddingMode);
        }
예제 #6
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));
 }
예제 #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));
 }