Beispiel #1
0
        public static byte[] DecryptDataFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            SymmetricCryptoHelper.CanPerformDecrypt(cipherText, Key, IV);

            byte[] decryptedData = null;

            using (Rijndael rijAlg = Rijndael.Create())
            {
                rijAlg.Key = Key;
                rijAlg.IV  = IV;

                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (BinaryReader binaryReader = new BinaryReader(csDecrypt))
                        {
                            decryptedData = binaryReader.ReadAllBytes();
                        }
                    }
                }
            }

            return(decryptedData);
        }
Beispiel #2
0
        public static byte[] EncryptDataToBytes(byte[] Data, byte[] Key, byte[] IV)
        {
            SymmetricCryptoHelper.CanPerformEncrypt(Data, Key, IV);
            byte[] encryptedData;

            using (Rijndael rijAlg = Rijndael.Create())
            {
                rijAlg.Key = Key;
                rijAlg.IV  = IV;

                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (BinaryWriter binaryWriter = new BinaryWriter(csEncrypt))
                        {
                            binaryWriter.Write(Data);
                        }
                        encryptedData = msEncrypt.ToArray();
                    }
                }
            }

            return(encryptedData);
        }
Beispiel #3
0
        public static Stream DecryptDataToStream(byte[] Data, byte[] Key, byte[] IV, CryptoAlgorithm crypAlg = CryptoAlgorithm.Rijndael)
        {
            SymmetricCryptoHelper.CanPerformDecrypt(Data, Key, IV);

            byte[] decryptedData = null;

            if (crypAlg == CryptoAlgorithm.Rijndael)
            {
                using (Rijndael rijAlg = Rijndael.Create())
                {
                    rijAlg.Key = Key;
                    rijAlg.IV  = IV;

                    ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
                    decryptedData = GetDecryptedDataFromCryptoStream(Data, decryptor);
                }
                return(new MemoryStream(decryptedData));
            }
            else
            {
                using (Aes aesAlg = Aes.Create())
                {
                    aesAlg.Key = Key;
                    aesAlg.IV  = IV;

                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                    decryptedData = GetDecryptedDataFromCryptoStream(Data, decryptor);
                }
                return(new MemoryStream(decryptedData));
            }
        }
Beispiel #4
0
        public static bool EncryptDataAndSaveToFile(byte[] Data, byte[] Key, byte[] IV, string absolutePath, CryptoAlgorithm crypAlg = CryptoAlgorithm.Rijndael)
        {
            SymmetricCryptoHelper.CanPerformEncrypt(Data, Key, IV);

            if (crypAlg == CryptoAlgorithm.Rijndael)
            {
                using (Rijndael rijAlg = Rijndael.Create())
                {
                    rijAlg.Key = Key;
                    rijAlg.IV  = IV;

                    ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
                    WriteEncryptedDataToFile(Data, absolutePath, encryptor);
                }
            }
            else
            {
                using (Aes aesAlg = Aes.Create())
                {
                    aesAlg.Key = Key;
                    aesAlg.IV  = IV;

                    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                    WriteEncryptedDataToFile(Data, absolutePath, encryptor);
                }
            }

            return(true);
        }