前往顾页
以后地位: 主页 > 收集编程 > .Net实例教程 >

实现加解密字符串,字符数组法度代码

时候:2011-05-24 14:33来源:知行网www.zhixing123.cn 编辑:麦田守望者

using System.Security.Cryptography;
using System.IO;
private RijndaelManaged myRijndael;
public string Key;
public string IV;
/**/
/// <summary>
/// 对称加密类的机关函数
/// </summary>
public Rijndael_(string key)
{
myRijndael = new RijndaelManaged();
Key = key;
IV = "67^%*(&(*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
}
/**/
/// <summary>
/// 对称加密类的机关函数
/// </summary>
public Rijndael_(string key, string iv)
{
myRijndael = new RijndaelManaged();
Key = key;
IV = iv;
}
/**/
/// <summary>
/// 获得密钥
/// </summary>
/// <returns>密钥</returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
myRijndael.GenerateKey();
byte[] bytTemp = myRijndael.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/**/
/// <summary>
/// 获得初始向量IV
/// </summary>
/// <returns>初试向量IV</returns>
private byte[] GetLegalIV()
{
string sTemp = IV;
myRijndael.GenerateIV();
byte[] bytTemp = myRijndael.IV;
int IVLength = bytTemp.Length;
if (sTemp.Length > IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < IVLength)
sTemp = sTemp.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/**/
/// <summary>
/// 加密体例
/// </summary>
/// <param name="Source">待加密的串</param>
/// <returns>颠末加密的串</returns>
public string Encrypt(string Source)
{
try
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
myRijndael.Key = GetLegalKey();
myRijndael.IV = GetLegalIV();
ICryptoTransform encrypto = myRijndael.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
catch (Exception ex)
{
throw new Exception("在文件加密的时候呈现错误!错误提示: \n" + ex.Message);
}
}
/**/
/// <summary>
/// 解密体例
/// </summary>
/// <param name="Source">待解密的串</param>
/// <returns>颠末解密的串</returns>
public string Decrypt(string Source)
{
try
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
myRijndael.Key = GetLegalKey();
myRijndael.IV = GetLegalIV();
ICryptoTransform encrypto = myRijndael.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception("在文件解密的时候呈现错误!错误提示: \n" + ex.Message);
}
}
/**/
/// <summary>
/// 加密体例byte[] to byte[]
/// </summary>
/// <param name="Source">待加密的byte数组</param>
/// <returns>颠末加密的byte数组</returns>
public byte[] Encrypt(byte[] Source)
{
try
{
byte[] bytIn = Source;
MemoryStream ms = new MemoryStream();
myRijndael.Key = GetLegalKey();
myRijndael.IV = GetLegalIV();
ICryptoTransform encrypto = myRijndael.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return bytOut;
}
catch (Exception ex)
{
throw new Exception("在文件加密的时候呈现错误!错误提示: \n" + ex.Message);
}
}
/**/
/// <summary>
/// 解密体例byte[] to byte[]
/// </summary>
/// <param name="Source">待解密的byte数组</param>
/// <returns>颠末解密的byte数组</returns>
public byte[] Decrypt(byte[] Source)
{
try
{
byte[] bytIn = Source;
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
myRijndael.Key = GetLegalKey();
myRijndael.IV = GetLegalIV();
ICryptoTransform encrypto = myRijndael.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return UTF8Encoding.UTF8.GetBytes(sr.ReadToEnd());
}
catch (Exception ex)
{
throw new Exception("在文件解密的时候呈现错误!错误提示: \n" + ex.Message);
}
}

顶一下
(1)
100%
踩一下
(0)
0%
------分开线----------------------------
标签(Tag):C# C#实例教程 c#根本教程 C#源代码
------分开线----------------------------
颁发评论
请自发遵循互联网相关的政策法规,严禁公布色情、暴力、革命的谈吐。
评价:
神色:
考证码:点击我更换图片
猜你感兴趣