要加密连接字符串?
.net有个自带的命名空间using System.Security.Cryptography;以下为加密一个字符串的代码:
private const string SECRET_KEY="6@CMico&";
//加密解密密钥
/// <summary>
/// <newpara>作用:加密一个字符串。</newpara>
/// <param name="ToEncrypt">需要加密的字符串。</param>
/// <param name="SecretKey">加密所用的密钥。</param>
/// <returns>加密后的结果字符串</returns>
/// </summary>
public static string Encrypt(string ToEncrypt,string SecretKey)
{
byte[] byteToEncypt = Encoding.UTF8.GetBytes(ToEncrypt); DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(SecretKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(SecretKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write); //Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(byteToEncypt, 0, byteToEncypt.Length);
cs.FlushFinalBlock(); //Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
.net有个自带的命名空间using System.Security.Cryptography;以下为加密一个字符串的代码:
private const string SECRET_KEY="6@CMico&";
//加密解密密钥
/// <summary>
/// <newpara>作用:加密一个字符串。</newpara>
/// <param name="ToEncrypt">需要加密的字符串。</param>
/// <param name="SecretKey">加密所用的密钥。</param>
/// <returns>加密后的结果字符串</returns>
/// </summary>
public static string Encrypt(string ToEncrypt,string SecretKey)
{
byte[] byteToEncypt = Encoding.UTF8.GetBytes(ToEncrypt); DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(SecretKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(SecretKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write); //Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(byteToEncypt, 0, byteToEncypt.Length);
cs.FlushFinalBlock(); //Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
/// <newpara>作用:解密一个字符串。</newpara>
/// <param name="ToDecrypt">需要解密的字符串。</param>
/// <param name="SecretKey">解密密钥。</param>
/// <returns>解密后的结果字符串</returns>
/// </summary>
public static string Decrypt(string ToDecrypt, string SecretKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[ToDecrypt.Length / 2];
for(int x = 0; x < ToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(ToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
} //Create the crypto objects
des.Key = ASCIIEncoding.ASCII.GetBytes(SecretKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(SecretKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write); //Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock(); //Get the decrypted data back from the memory stream
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
ret.Append((char)b);
}
return ret.ToString();
}
向md5,ssl...