/// <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>解密后流数据,返回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>返回加密后数据</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> /// 非对称加密算法 /// </summary> /// <param name="data">字符串</param> /// <param name="profile">配置文件</param> /// <param name="key">output parameter containing the generated secret key </param> /// <param name="iv">output parameter containing the generated iv key</param> /// <param name="signature">数字签名</param> /// <returns>dencrypted string</returns> public static string Encrypt(string data, string profile, out string key, out string iv, out string signature) { //convert the string into stream var original = new MemoryStream(Encoding.UTF8.GetBytes(data)); //encrypte the stream var encryptedStream = Encrypt(original, profile, out key, out iv, out signature); //convert the encrypted into back to string var encryptedData = new byte[encryptedStream.Length]; encryptedStream.Read(encryptedData, 0, encryptedData.Length); return(Decryption.BytesToBase64(encryptedData)); }