![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// SecureAttribute 的摘要说明。
/// </summary>
public class SecureAttribute
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private static byte[] keys = new byte[]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
133, 98, 72, 143, 17, 162, 187, 83, 7, 15, 179, 199, 247
};
private static byte[] vectors = new byte[]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9, 139, 23, 177, 213, 63, 90, 129, 53, 122, 187, 190, 32, 10, 135, 138, 92
};
private static byte[] keys = new byte[]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
133, 98, 72, 143, 17, 162, 187, 83, 7, 15, 179, 199, 247
};
private static byte[] vectors = new byte[]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9, 139, 23, 177, 213, 63, 90, 129, 53, 122, 187, 190, 32, 10, 135, 138, 92
};
![](/Images/OutliningIndicators/InBlock.gif)
// 加密串
public static string EncryptString(string Source)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ASCIIEncoding textConverter = new ASCIIEncoding();
RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
byte[] encrypted;
byte[] toEncrypt;
rc2CSP.EffectiveKeySize = 128;
// 生成加密器.
ICryptoTransform encryptor = rc2CSP.CreateEncryptor(keys, vectors);
// 加密数据
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
![](/Images/OutliningIndicators/InBlock.gif)
// 转换数据到字节表
toEncrypt = textConverter.GetBytes(Source);
![](/Images/OutliningIndicators/InBlock.gif)
// 执行加密
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
![](/Images/OutliningIndicators/InBlock.gif)
// 取加密过的字节流
encrypted = msEncrypt.ToArray();
![](/Images/OutliningIndicators/InBlock.gif)
// 返回结果
return Convert.ToBase64String(encrypted);
}
![](/Images/OutliningIndicators/InBlock.gif)
// 解密串
public static string DecryptString(string Source)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] fromEncrypt;
byte[] encrypted;
![](/Images/OutliningIndicators/InBlock.gif)
ASCIIEncoding textConverter = new ASCIIEncoding();
RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
rc2CSP.EffectiveKeySize = 128;
![](/Images/OutliningIndicators/InBlock.gif)
// 生成解密器
ICryptoTransform decryptor = rc2CSP.CreateDecryptor(keys, vectors);
![](/Images/OutliningIndicators/InBlock.gif)
// 取得源加密字节流
encrypted = System.Convert.FromBase64String(Source);
![](/Images/OutliningIndicators/InBlock.gif)
// 解密字节流
MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
![](/Images/OutliningIndicators/InBlock.gif)
fromEncrypt = new byte[encrypted.Length];
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
int n = 0;
foreach (byte b in fromEncrypt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (b != 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
n++;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
}
byte[] bs = new byte[n];
for (int i = 0; i < n; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bs[i] = fromEncrypt[i];
}
string s = textConverter.GetString(bs);
![](/Images/OutliningIndicators/InBlock.gif)
// 返回解密串
return s;
}
}
![](/Images/OutliningIndicators/None.gif)
posted on
2006-06-23 15:56
过江
阅读(
432)
评论()
编辑
收藏
举报