.Net对称加密解密算法之二(RijndaelManaged类实现)
By:Roy.LiuLast updated:2008-12-21
同样建立一个控制台程序:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入你要加密的字符串:");
string x = Console.ReadLine();
string y;
y = Encrypting(x);
Console.WriteLine("加密后的结果是:");
Console.WriteLine(y);
Console.WriteLine("现在调用解密方法:");
string z;
z = Decrypting(y);
Console.WriteLine("解密后的结果是:");
Console.WriteLine(z);
Console.Read();
}
static public string Encrypting(string strSource)
{
try
{
//把字符串放到byte数组中
byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
//建立加密对象的密钥和偏移量
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
//实例 RijndaelManaged 类将字符串写入流中
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.IV = IV;
myRijndael.Key = key;
ICryptoTransform encrypto = myRijndael.CreateEncryptor();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
byte[] bytOut = ms.ToArray();
return System.Convert.ToBase64String(bytOut);
}
catch (Exception ex)
{
throw new Exception("在文件加密的时候出现错误!错误提示: \n" + ex.Message);
}
}
static public string Decrypting(string Source)
{
try
{
//将解密字符串转换成字节数组
byte[] bytIn = System.Convert.FromBase64String(Source);
//给出解密的密钥和偏移量,密钥和偏移量必须与加密时的密钥和偏移量相同
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.IV = IV;
myRijndael.Key = key;
//实例流进行解密
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
ICryptoTransform encrypto = myRijndael.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader strd = new StreamReader(cs, Encoding.Default);
return strd.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception("在文件解密的时候出现错误!错误提示: \n" + ex.Message);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入你要加密的字符串:");
string x = Console.ReadLine();
string y;
y = Encrypting(x);
Console.WriteLine("加密后的结果是:");
Console.WriteLine(y);
Console.WriteLine("现在调用解密方法:");
string z;
z = Decrypting(y);
Console.WriteLine("解密后的结果是:");
Console.WriteLine(z);
Console.Read();
}
static public string Encrypting(string strSource)
{
try
{
//把字符串放到byte数组中
byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
//建立加密对象的密钥和偏移量
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
//实例 RijndaelManaged 类将字符串写入流中
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.IV = IV;
myRijndael.Key = key;
ICryptoTransform encrypto = myRijndael.CreateEncryptor();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
byte[] bytOut = ms.ToArray();
return System.Convert.ToBase64String(bytOut);
}
catch (Exception ex)
{
throw new Exception("在文件加密的时候出现错误!错误提示: \n" + ex.Message);
}
}
static public string Decrypting(string Source)
{
try
{
//将解密字符串转换成字节数组
byte[] bytIn = System.Convert.FromBase64String(Source);
//给出解密的密钥和偏移量,密钥和偏移量必须与加密时的密钥和偏移量相同
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.IV = IV;
myRijndael.Key = key;
//实例流进行解密
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
ICryptoTransform encrypto = myRijndael.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader strd = new StreamReader(cs, Encoding.Default);
return strd.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception("在文件解密的时候出现错误!错误提示: \n" + ex.Message);
}
}
}
}
From:一号门
Previous:.Net对称加密解密算法之一(DES方法)
COMMENTS