using
System;
using System.IO;
using System.Xml;
using System.Text;
using System.Security.Cryptography;
/**/ /// <summary>
/// Summary description for Encryption64
/// </summary>
public class Encryption64
{
const string DESKey = "AQWSEDRF";
const string DESIV = "HGFEDCBA";
public static string DESDecrypt(string stringToDecrypt)//Decrypt the content
{
byte[] key;
byte[] IV;
byte[] inputByteArray;
try
{
key = Convert2ByteArray(DESKey);
IV = Convert2ByteArray(DESIV);
int len = stringToDecrypt.Length;
// stringToDecrypt =stringToDecrypt.Replace(" ","+");
inputByteArray = Convert.FromBase64String(stringToDecrypt);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8; return encoding.GetString(ms.ToArray());
}
catch (System.Exception ex)
{
throw ex;
}
}
public static string DESEncrypt(string stringToEncrypt)// Encrypt the content
{
byte[] key;
byte[] IV;
byte[] inputByteArray;
try
{
key = Convert2ByteArray(DESKey);
IV = Convert2ByteArray(DESIV);
inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
string value = Convert.ToBase64String(ms.ToArray());
// value = value.Replace("+"," ");
return value;
}
catch (System.Exception ex)
{
throw ex;
}
}
static byte[] Convert2ByteArray(string strInput)
{
int intCounter; char[] arrChar;
arrChar = strInput.ToCharArray();
byte[] arrByte = new byte[arrChar.Length];
for (intCounter = 0; intCounter <= arrByte.Length - 1; intCounter++)
arrByte[intCounter] = Convert.ToByte(arrChar[intCounter]);
return arrByte;
}
}
using System.IO;
using System.Xml;
using System.Text;
using System.Security.Cryptography;
/**/ /// <summary>
/// Summary description for Encryption64
/// </summary>
public class Encryption64
{
const string DESKey = "AQWSEDRF";
const string DESIV = "HGFEDCBA";
public static string DESDecrypt(string stringToDecrypt)//Decrypt the content
{
byte[] key;
byte[] IV;
byte[] inputByteArray;
try
{
key = Convert2ByteArray(DESKey);
IV = Convert2ByteArray(DESIV);
int len = stringToDecrypt.Length;
// stringToDecrypt =stringToDecrypt.Replace(" ","+");
inputByteArray = Convert.FromBase64String(stringToDecrypt);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8; return encoding.GetString(ms.ToArray());
}
catch (System.Exception ex)
{
throw ex;
}
}
public static string DESEncrypt(string stringToEncrypt)// Encrypt the content
{
byte[] key;
byte[] IV;
byte[] inputByteArray;
try
{
key = Convert2ByteArray(DESKey);
IV = Convert2ByteArray(DESIV);
inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
string value = Convert.ToBase64String(ms.ToArray());
// value = value.Replace("+"," ");
return value;
}
catch (System.Exception ex)
{
throw ex;
}
}
static byte[] Convert2ByteArray(string strInput)
{
int intCounter; char[] arrChar;
arrChar = strInput.ToCharArray();
byte[] arrByte = new byte[arrChar.Length];
for (intCounter = 0; intCounter <= arrByte.Length - 1; intCounter++)
arrByte[intCounter] = Convert.ToByte(arrChar[intCounter]);
return arrByte;
}
}
以上代码大部分时间运行是正常的,但是加密得出的字符串如果包含"+",用 Request.QueryString接收,"+"字符会漏掉,
解密的时候就会报 Invalid length for a Base-64 char array异常,所以加密后要替换下
string
value
=
Convert.ToBase64String(ms.ToArray());
value = value.Replace( " + " , " " );
解密时就要替换回来
value = value.Replace( " + " , " " );
stringToDecrypt
=
stringToDecrypt.Replace(
"
"
,
"
+
"
);
inputByteArray = Convert.FromBase64String(stringToDecrypt);
inputByteArray = Convert.FromBase64String(stringToDecrypt);
希望对您有帮助,弄了好久才发现的。。。