C# 中用DES 对称Key,IV 加密,前端crypto.js 解密
1.服务器端代码
#region ========加密========
/// <summary>
/// 加密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Encrypt(string Text)
{
return Encrypt(Text, DESKey);
}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.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); // {0:X2}大写的十六进制
}
return ret.ToString();
}
#endregion
#region ========解密========
/// <summary>
/// 解密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Decrypt(string Text)
{
if (!string.IsNullOrEmpty(Text))
{
return Decrypt(Text, DESKey);
}
else
{
return "";
}
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Text.Length / 2;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
#endregion
2.前端
encryptDes:function(input,key,iv){
var cipher = crypto.createCipheriv('des', new Buffer(key), new Buffer(iv));
var buf1 = cipher.update(input, 'utf8');
var buf2 = cipher.final();
var result = new Buffer(buf1.length + buf2.length);
buf1.copy(result);
buf2.copy(result, buf1.length);
return result.toString('hex').toUpperCase();
},
decryptDes:function(encrypt_text,iv,key){
var key = new Buffer(key);
var iv = new Buffer(iv ? iv : 0);
var decipher = crypto.createDecipheriv('des', key, iv);
decipher.setAutoPadding(true);
var txt = decipher.update(encrypt_text, 'hex', 'utf8');
txt += decipher.final('utf8');
return txt;
},
Str2Bytes:function (str) {
var bytes = new Array();
for (var i = 0; i < str.length; i++) {
var s = str.substr(i, 1);
var v = s.toString().charCodeAt();
bytes.push(v);
}
return bytes;
}
var crypto = require('crypto');
var temp=MD5(key).toString().substring(0, 8).toUpperCase();
var KEY = this.Str2Bytes(temp); // [ 1, 2, 3, 4, 5, 6, 7, 8 ];
var IV =KEY; // [ 1, 2, 3, 4, 5, 6, 7, 8 ];
var encryted_content=this.encryptDes(content ,KEY,IV);
var decryted_content=this.decryptDes(encryted_content,IV,KEY);
var user_json=JSON.parse(decryted_content);