一个私钥加密的例子
using System;
using System.Security;
using System.Security.Cryptography;
using System.IO;
namespace cry
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Cryop 的摘要说明。
/// </summary>
public class Cryop
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Keys与IV的长度相等
private static Byte[] Keys = System.Text.Encoding.Unicode.GetBytes("天天向上");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static Byte[] IV = new Byte[]
{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 加密
/// </summary>
public static Byte[] Enc(String test)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Security.Cryptography.RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
MemoryStream msStream = new MemoryStream();
byte[] toEncrypt;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ICryptoTransform encrypt = rc2.CreateEncryptor(Keys,IV);
CryptoStream csStream = new CryptoStream(msStream,encrypt,CryptoStreamMode.Write);
toEncrypt = System.Text.Encoding.Unicode.GetBytes(test);
csStream.Write(toEncrypt,0,toEncrypt.Length);
csStream.FlushFinalBlock();
}
catch(Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(ex.Message,ex);
}
return msStream.ToArray();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 解密
/// </summary>
public static String Dec(Byte[] enc)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Security.Cryptography.RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ICryptoTransform decrypt = rc2.CreateDecryptor(Keys,IV);
MemoryStream msDec = new MemoryStream(enc);
CryptoStream csDec = new CryptoStream(msDec,decrypt,CryptoStreamMode.Read);
byte[] fromEncpty = new Byte[enc.Length];
csDec.Read(fromEncpty,0,fromEncpty.Length);
return System.Text.Encoding.Unicode.GetString(fromEncpty);
}
catch(Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(ex.Message,ex);
}
}
}
}
posted on
2005-03-28 12:34
王员外
阅读(
938)
评论()
编辑
收藏
举报