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