public static class CryptHelper
{
private static byte[] _userProfile = { 175, 232, 160, 205, 118, 189, 241, 143, 109, 91, 126, 212, 83, 36, 161, 146, 97, 142, 41, 173, 137, 54, 75, 112, 226, 120, 151, 174, 147, 121, 225, 127 };
private static byte[] _webEvent = { 27, 203, 38, 163, 110, 89, 90, 109, 183, 95, 178, 64, 88, 27, 221, 230 };
/// <summary>
/// 用Base64编码加密字符串
/// </summary>
/// <param name="plainText">要加密的字符串</param>
/// <returns>return 加密后的字符串</returns>
public static string encryptStringToBase64(string plainText)
{
if (plainText == null)
{
plainText = string.Empty;
}
MemoryStream msEncrypt = null;
CryptoStream csEncrypt = null;
StreamWriter swEncrypt = null;
RijndaelManaged aesAlg = null;
try
{
byte[] UserProfile = new byte[32];
for (int i = 0; i < 32; i++)
{
UserProfile[i] = (byte)(_userProfile[i] ^ TryUserProfile.RealUserProfile[i]);
}
byte[] WebEvent = new byte[16];
for (int i = 0; i < 16; i++)
{
WebEvent[i] = (byte)(_webEvent[i] ^ TryUserProfile.RealWebEvent[i]);
}
aesAlg = new RijndaelManaged();
aesAlg.Key = UserProfile;
aesAlg.IV = WebEvent;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
for (int i = 0; i < 32; i++)
{
UserProfile[i] = 0x0;
}
for (int i = 0; i < 16; i++)
{
WebEvent[i] = 0x0;
}
msEncrypt = new MemoryStream();
csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
swEncrypt = new StreamWriter(csEncrypt);
swEncrypt.Write(plainText);
aesAlg.Key = UserProfile;
aesAlg.IV = WebEvent;
}
catch (Exception)
{
// failed to encrypt the given plain text
return plainText;
}
finally
{
if (swEncrypt != null)
swEncrypt.Close();
if (csEncrypt != null)
csEncrypt.Close();
if (msEncrypt != null)
msEncrypt.Close();
if (aesAlg != null)
aesAlg.Clear();
}
string cipherText = Convert.ToBase64String(msEncrypt.ToArray());
return cipherText;
}
/// <summary>
/// 解密字符串用Base64
/// </summary>
/// <param name="cipherText">要解密的字符串</param>
/// <returns>return decryptString</returns>
public static string decryptStringFromBase64(string cipherText)
{
if (string.IsNullOrEmpty(cipherText))
{
return string.Empty;
}
MemoryStream msDecrypt = null;
CryptoStream csDecrypt = null;
StreamReader srDecrypt = null;
RijndaelManaged aesAlg = null;
string plaintext = string.Empty;
try
{
byte[] UserProfile = new byte[32];
for (int i = 0; i < 32; i++)
{
UserProfile[i] = (byte)(_userProfile[i] ^ TryUserProfile.RealUserProfile[i]);
}
byte[] WebEvent = new byte[16];
for (int i = 0; i < 16; i++)
{
WebEvent[i] = (byte)(_webEvent[i] ^ TryUserProfile.RealWebEvent[i]);
}
aesAlg = new RijndaelManaged();
aesAlg.Key = UserProfile;
aesAlg.IV = WebEvent;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
for (int i = 0; i < 32; i++)
{
UserProfile[i] = 0x0;
}
for (int i = 0; i < 16; i++)
{
WebEvent[i] = 0x0;
}
// there's no way to detect if the string is encrypted except using some special prefix/suffix
msDecrypt = new MemoryStream(Convert.FromBase64String(cipherText));
csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
srDecrypt = new StreamReader(csDecrypt);
plaintext = srDecrypt.ReadToEnd();
aesAlg.Key = UserProfile;
aesAlg.IV = WebEvent;
}
catch (Exception)
{
return cipherText;
}
finally
{
try
{
if (srDecrypt != null)
srDecrypt.Close();
if (csDecrypt != null)
csDecrypt.Close();
if (msDecrypt != null)
msDecrypt.Close();
if (aesAlg != null)
aesAlg.Clear();
}
catch (Exception)
{
}
}
return plaintext;
}
/// <summary>
///
/// </summary>
public static class TryUserProfile
{
private static byte[] _userProfile = { 179, 102, 160, 120, 29, 20, 247, 182, 177, 19, 95, 136, 54, 73, 255, 209, 42, 52, 38, 7, 235, 96, 19, 157, 210, 83, 233, 221, 149, 248, 162, 39 };
public static byte[] RealUserProfile
{
get
{
return _userProfile;
}
}
private static byte[] _webEvent = { 69, 248, 233, 86, 33, 35, 92, 48, 59, 111, 156, 182, 13, 241, 143, 229 };
public static byte[] RealWebEvent
{
get
{
return _webEvent;
}
}
}
}