using System;
using System.IO;
using System.Security.Cryptography; namespace RijndaelManaged_Example
{
class RijndaelExample
{
public static void Main()
{
try
{ string original = "Here is some data to encrypt!"; // Create a new instance of the Rijndael
// class. This generates a new key and initialization
// vector (IV).
using (Rijndael myRijndael = Rijndael.Create())
{
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV); // Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV); //Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
} }
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= )
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= )
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= )
throw new ArgumentNullException("Key");
byte[] encrypted;
// Create an Rijndael object
// with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV; // Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); // Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{ //Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
} // Return the encrypted bytes from the memory stream.
return encrypted; } static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= )
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= )
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= )
throw new ArgumentNullException("Key"); // Declare the string used to hold
// the decrypted text.
string plaintext = null; // Create an Rijndael object
// with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV; // Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); // Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{ // Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
} } return plaintext; }
}
}

原文地址:https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.cryptostream.aspx

最新文章

  1. putty基本操作
  2. [转]va_start和va_end使用详解
  3. Java Thread UncaughtExceptionHandler
  4. 在Mac OS X中搭建STM32开发环境(2)
  5. aix5.1 5.2 5.3 6.1 7.1运维技术总结
  6. powerdesigener 12.5注册机
  7. asp.net 页面执行过程
  8. javascript原生方法实现extend
  9. 【Python】 魔法方法
  10. Spring 4.2.5 + Quartz 2.2.0整合
  11. Android应用程序MVC框架实例分析
  12. 阿里面试题:为什么Map桶中个数超过8才转为红黑树
  13. cookies增删改擦操作
  14. centos安装守护进程工具supervisor
  15. MySQL 安全整理
  16. tomcat配置的环境变量catalina.home和catalina.base 区别
  17. Nmap命令的常用实例
  18. grafana启用mysql作为的后台数据库
  19. Android文档 - 账户管理器概述
  20. POJ 3067 - Japan - [归并排序/树状数组(BIT)求逆序对]

热门文章

  1. GEOS库学习之三:空间关系、DE-9IM和谓词
  2. Linux及安全期中总结
  3. Linux(9.21-9.27)学习笔记
  4. 20145222黄亚奇《Java程序设计》实验四实验报告
  5. PM2实用入门指南
  6. 【niubi-job——一个分布式的任务调度框架】----如何开发一个niubi-job的定时任务
  7. 我的第二个app上线:术购管家
  8. requirejs自己的学习
  9. Android之POST方法的使用
  10. OneZero第一次会议(非正式)