/// <summary> /// 解密数据 /// </summary> /// <param name="data">待解密流数据</param> /// <param name="proEntity">解密配置</param> /// <returns>解密后流数据,返回MemoryStream</returns> /// <exception cref="ArgumentNullException">当解密配置为null时抛出此异常</exception> /// <exception cref="Exception">此方法只支持对称解密,如果使用非对称配置,抛出此异常</exception> public static Stream Decrypt(Stream data, EncryptionConfigEntity proEntity) { if (proEntity == null) { throw new ArgumentNullException("proEntity"); } if (proEntity.SymmetricAlgorithm != true) { throw new Exception("This method id not intended for asymmetric encryption"); } //retrieve the secret key and iv from the configuration file var provider = proEntity.AlgorithmProvider.CreateInstance <SymmetricAlgorithm>(); var key = proEntity.ExtentProperty["Key"]; var iv = proEntity.ExtentProperty["IV"]; provider.Key = Decryption.Base64ToBytes(key); provider.IV = Decryption.Base64ToBytes(iv); var decryptor = provider.CreateDecryptor(); //decrypt the stream var decStream = new CryptoStream(data, decryptor, CryptoStreamMode.Read); var decrypted = new MemoryStream(); var buffer = new byte[2048]; int count; while ((count = decStream.Read(buffer, 0, buffer.Length)) != 0) { decrypted.Write(buffer, 0, count); } decrypted.Position = 0; return(decrypted); }
/// <summary> /// 流数据加密 /// </summary> /// <param name="data">数据</param> /// <param name="proEntity">加密配置</param> /// <returns>加密后流数据,返回MemoryStream</returns> /// <exception cref="ArgumentNullException">当加密配置为null时抛出此异常</exception> /// <exception cref="Exception">此方法只支持对称加密,如果使用非对称配置,抛出此异常</exception> public static Stream Encrypt(Stream data, EncryptionConfigEntity proEntity) { if (proEntity == null) { throw new ArgumentNullException("proEntity"); } if (proEntity.SymmetricAlgorithm == false) { throw new Exception("This method id not intended for asymmetric encryption"); } //retrive the secret key and iv information var provider = proEntity.AlgorithmProvider.CreateInstance <SymmetricAlgorithm>(); provider.Key = Decryption.Base64ToBytes(proEntity.ExtentProperty["Key"]); provider.IV = Decryption.Base64ToBytes(proEntity.ExtentProperty["IV"]); var encryptor = provider.CreateEncryptor(); var encrypted = new MemoryStream(); //encrypt the stream symmetrically var encStream = new CryptoStream(encrypted, encryptor, CryptoStreamMode.Write); var buffer = new byte[1024]; int count; while ((count = data.Read(buffer, 0, 1024)) > 0) { encStream.Write(buffer, 0, count); } encStream.FlushFinalBlock(); encrypted.Position = 0; return(encrypted); }
/// <summary> /// 字符串加密 /// </summary> /// <param name="data">待加密数据</param> /// <param name="proEntity">加密配置</param> /// <returns>返回加密后数据</returns> public static string Encrypt(string data, EncryptionConfigEntity proEntity) { var original = new MemoryStream(Encoding.UTF8.GetBytes(data)); var encryptedStream = Encrypt(original, proEntity); var encryptedData = new Byte[encryptedStream.Length]; encryptedStream.Read(encryptedData, 0, encryptedData.Length); //convert the encrytped stream to string return(Decryption.BytesToBase64(encryptedData)); }
/// <summary> /// 根据指定算法和Key创建加密配置 /// </summary> /// <param name="algorithmProvider">算法</param> /// <param name="symmetricAlgorithm">同步异步算法</param> /// <param name="extentProperty">扩展数据</param> /// <returns>返回<see cref="EncryptionConfigEntity"/></returns> public static EncryptionConfigEntity CreateEncryptionConfigEntity(string algorithmProvider, bool symmetricAlgorithm, Dictionary <string, string> extentProperty) { var entity = new EncryptionConfigEntity { AlgorithmProvider = algorithmProvider, SymmetricAlgorithm = symmetricAlgorithm, ExtentProperty = extentProperty }; return(entity); }
/// <summary> /// 解密数据 /// </summary> /// <param name="data">待解密字符串</param> /// <param name="proEntity">解密配置</param> /// <returns>解密后字符串</returns> public static string Decrypt(string data, EncryptionConfigEntity proEntity) { //convert the string to stream var inputByteArray = Base64ToBytes(data); var original = new MemoryStream(inputByteArray); //MemoryStream original = new MemoryStream(Encoding.UTF8.GetBytes(data)); var decryptedStream = Decrypt(original, proEntity); var decryptedData = new Byte[decryptedStream.Length]; decryptedStream.Read(decryptedData, 0, decryptedData.Length); //convert the stream to string return(Encoding.UTF8.GetString(decryptedData)); }