DES的加密模式具体可以参照DES算法的几种加密模式和填补方式
来看代码
前端部分:
js引入的是crypto-js,具体函数如下:
// DES CBC模式加密
//加密内容、秘钥、向量
function encryptByDES(message,key,iv) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = CryptoJS.enc.Utf8.parse(iv);
encrypted = CryptoJS.DES.encrypt(message, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
}
);
return encrypted.ciphertext.toString();
}
//DES CBC模式解密
function decryptByDESModeEBC(ciphertext) {
//把私钥转换成UTF - 8编码的字符串
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = CryptoJS.enc.Utf8.parse(iv);
// direct decrypt ciphertext
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(ciphertext)
}, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
引入的CryptoJS 模块有core.js、cipher-core.js以及tripledes.js
由于后台选用的C#加密方式加密出来的字符串是Base64模式的,所以前端加密前需要将十六进制字符串转为base64,这里我用到了Heron-Chogori博主的方法,具体代码如下
//十六进制字符串转为base64
function sha1_to_base64(sha1)
{
var digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64_rep = "";
var cnt = 0;
var bit_arr = 0;
var bit_num = 0;
for(var n = 0; n < sha1.length; ++n)
{
if(sha1[n] >= 'A' && sha1[n] <= 'Z')
{
ascv = sha1.charCodeAt(n) - 55;
}
else if(sha1[n] >= 'a' && sha1[n] <= 'z')
{
ascv = sha1.charCodeAt(n) - 87;
}
else
{
ascv = sha1.charCodeAt(n) - 48;
}
bit_arr = (bit_arr << 4) | ascv;
bit_num += 4;
if(bit_num >= 6)
{
bit_num -= 6;
base64_rep += digits[bit_arr >>> bit_num];
bit_arr &= ~(-1 << bit_num);
}
}
if(bit_num > 0)
{
bit_arr <<= 6 - bit_num;
base64_rep += digits[bit_arr];
}
var padding = base64_rep.length % 4;
if(padding > 0)
{
for(var n = 0; n < 4 - padding; ++n)
{
base64_rep += "=";
}
}
return base64_rep;
}
C#部分
使用的是Chen Jian博主的方法,代码如下:
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string EncryptDES(string encryptString, string encryptKey)
{
try
{
//将字符转换为UTF - 8编码的字节序列
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Encoding.UTF8.GetBytes(iv);
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
//用指定的密钥和初始化向量创建CBC模式的DES加密标准
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
dCSP.Mode = CipherMode.CBC;
dCSP.Padding = PaddingMode.PKCS7;
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()); //将内存流转写入字节数组并转换为string字符
}
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
{
//将字符转换为UTF - 8编码的字节序列
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
byte[] rgbIV = Encoding.UTF8.GetBytes(iv);
byte[] inputByteArray = Convert.FromBase64String(decryptString);
//用指定的密钥和初始化向量使用CBC模式的DES解密标准解密
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
dCSP.Mode = CipherMode.CBC;
dCSP.Padding = PaddingMode.PKCS7;
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;
}
}
基本实现了前端加密出的字符串与后台解密出的一致。
推荐一在线加密网站 嘤嘤嘤
网友评论