加密/解密操作类
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
/**//// <summary>
/// Security加密/解密
/// </summary>
public class Security
{
private static readonly string _StringKey = "a2Nds9Ke"; //加密所需8位密匙
/**//// <summary>
/// 加密字符串
/// </summary>
/// <param name="QueryString">需要加密的字符串</param>
/// <returns>加密后的字符串</returns>
public static string EncryptString(string QueryString)
{
return Encrypt(QueryString, _StringKey);
}
/**//// <summary>
/// 解密字符串
/// </summary>
/// <param name="QueryString">需要解密的字符串</param>
/// <returns>解密后的字符串</returns>
public static string DecryptString(string QueryString)
{
return Decrypt(QueryString, _StringKey);
}
/**//// <summary>
/// 加密字符串,自定义8位密匙
/// 加密密匙必须与解密密匙相同
/// </summary>
/// <param name="pToEncrypt">需要加密的字符串</param>
/// <param name="sKey">8位字符密匙</param>
/// <returns>加密后的字符串</returns>
protected static string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //把字符串放到byte数组中
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
MemoryStream ms = new MemoryStream(); //使得输入密码必须输入英文文本
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
/**//// <summary>
/// 解密字符串,自定义8位密匙
/// 加密密匙必须与解密密匙相同
/// </summary>
/// <param name="pToDecrypt">需要解密的字符串</param>
/// <param name="sKey">8位字符密匙</param>
/// <returns>解密后的字符串</returns>
protected static 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(); //建立StringBuild对象,createDecrypt使用的是流对象,必须把解密后的文本变成流对象
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
}
一个Java的加密解密类转成C#的加密解密类(最注意保持结果一致)
/// <summary>
/// 字符串DES加密及解密类
/// </summary>
public class secret_string
{
//默认密钥向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/**/
/**/
/**/
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串 </param>
/// <param name="encryptKey">加密密钥,要求为8位 </param>
/// <returns>加密成功返回加密后的字符串,失败返回源串 </returns>
public static string EncryptDES(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return encryptString;
}
}
/**/
/**/
/**/
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串 </param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同 </param>
/// <returns>解密成功返回解密后的字符串,失败返源串 </returns>
public static string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
}
我用的这个des加密解密的密钥字符有受到限制,使用有些密钥加密后接不能解密,如123456aaa,就不能解密!有没有密钥不限制字符的解密类!
//=======================================================================================
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Security.Cryptography; using System.Collections; namespace Deking { class DesEncryption { //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// <summary> /// DES加密字符串 /// </summary> /// <param name="encryptString">待加密的字符串</param> /// <param name="encryptKey">加密密钥,要求为8位</param> /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> public static string EncryptDES(string encryptString, string encryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } } /// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string DecryptDES(string decryptString, string decryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } } } public void DesEncrypt(string sInputFilename, string sOutputFilename, string key) //加密解密类 { string sKey; FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); if (key.Length >= 8) sKey = key.Substring(0, 8); else { sKey = key.PadRight(8, '1'); } DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write); byte[] bytearrayinput = new byte[fsInput.Length]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Close(); fsInput.Close(); fsEncrypted.Close(); System.IO.File.Delete(sInputFilename); } public void DesDecrypt(string sInputFilename, string sOutputFilename, string key) { string sKey; DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); // // 转换key为8位 // if (key.Length >= 8) sKey = key.Substring(0, 8); else { sKey = key.PadRight(8, '1'); } DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //Set initialization vector. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); FileStream fswrite = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write); ICryptoTransform desdecrypt = DES.CreateDecryptor(); CryptoStream cryptostreamDecr = new CryptoStream(fswrite, desdecrypt, CryptoStreamMode.Write); byte[] bytearrayinput = new byte[fsread.Length]; fsread.Read(bytearrayinput, 0, bytearrayinput.Length); cryptostreamDecr.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostreamDecr.Close(); fswrite.Close(); fsread.Close(); System.IO.File.Delete(sInputFilename); } } } |