加密
public string Encrypt(string pToEncrypt, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
provider.Key = ASCIIEncoding.ASCII.GetBytes(key);
provider.IV = ASCIIEncoding.ASCII.GetBytes(key);
MemoryStream stream = new MemoryStream();
CryptoStream crypto = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
crypto.Write(inputByteArray, 0, inputByteArray.Length);
crypto.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach(byte b in stream.ToArray)
{
builder.AppendFormat("{0:X2}", b);
}
return builder.ToString();
} /******************************************************************
* 密码解密
* ******************************************************************/ public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray());
}非常感谢
public string Encrypt(string pToEncrypt, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
provider.Key = ASCIIEncoding.ASCII.GetBytes(key);
provider.IV = ASCIIEncoding.ASCII.GetBytes(key);
MemoryStream stream = new MemoryStream();
CryptoStream crypto = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
crypto.Write(inputByteArray, 0, inputByteArray.Length);
crypto.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach(byte b in stream.ToArray)
{
builder.AppendFormat("{0:X2}", b);
}
return builder.ToString();
} /******************************************************************
* 密码解密
* ******************************************************************/ public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray());
}非常感谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货