JavaScript与JAVA相互实现DES加解和解密处理
程序员文章站
2022-06-19 17:44:19
javascript 用CryptoJS实现DES加解密处理 有关 CryptoJS, 请 参考:https://github.com/sytelus/CryptoJS/tree/master/componentsA. JAVA 部分package com.aerchi.Utility;import java.security.Key;import java.security.spec.AlgorithmParameterSpec;import javax.crypto.C......
JavaScript与JAVA相互实现DES加解密处理
有关JavaScript的 CryptoJS, 请 参考: https://github.com/sytelus/CryptoJS/tree/master/components
A. JAVA 部分
package com.aerchi.Utility;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import android.util.Base64;
/**
* 使用DES加密和解密工具类
*
* @author Administrator
*
*/
public class DesUtility
{
private Key key;// 密钥的key值
private byte[] DESkey;
// e2fd7b1n
private byte[] DESIV = { 'e', '2', 'f', 'd', '7', 'b', '1', 'n' };
private AlgorithmParameterSpec iv = null;// 加密算法的参数接口
public DesUtility()
{
try
{
this.DESkey = "&da0f@4d".getBytes("UTF-8");// 设置密钥
DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
iv = new IvParameterSpec(DESIV);// 设置向量
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
key = keyFactory.generateSecret(keySpec);// 得到密钥对象
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 加密String 明文输入密文输出
*
* @param inputString
* 待加密的明文
* @return 加密后的字符串
*/
public String getEnc(String inputString)
{
byte[] byteMi = null;
byte[] byteMing = null;
String outputString = "";
try
{
byteMing = inputString.getBytes("UTF-8");
byteMi = this.getEncCode(byteMing);
byte[] temp = Base64.encode(byteMi, Base64.DEFAULT);
outputString = new String(temp);
//outputString=new String(byteMi, "UTF8");
}
catch (Exception e)
{
}
finally
{
byteMing = null;
byteMi = null;
}
return outputString;
}
/**
* 解密String 以密文输入明文输出
*
* @param inputString
* 需要解密的字符串
* @return 解密后的字符串
*/
public String getDec(String inputString)
{
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try
{
byteMi = Base64.decode(inputString.getBytes(), Base64.DEFAULT);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
}
catch (Exception e)
{
}
finally
{
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param bt
* 待加密的字节码
* @return 加密后的字节码
*/
private byte[] getEncCode(byte[] bt)
{
byte[] byteFina = null;
Cipher cipher;
try
{
// 得到Cipher实例
cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byteFina = cipher.doFinal(bt);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param bt
* 待解密的字节码
* @return 解密后的字节码
*/
private byte[] getDesCode(byte[] bt)
{
Cipher cipher;
byte[] byteFina = null;
try
{
// 得到Cipher实例
cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byteFina = cipher.doFinal(bt);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
cipher = null;
}
return byteFina;
}
}
B: JavaScript
关于引入文件, 请把 https://github.com/sytelus/CryptoJS/tree/master/rollups 全部复制到本地, 并引入 rollups/tripledes.js 文件
1. ECB 加解密
//需要引入 CryptoJS里的tripledes.js 和 mode-ecb.js文件
<script type="text/javascript">
var DesECBUtil = {
key: '&da0f@4d',//加密密钥
//加密
encrypt: function(message) {
let keyHex = CryptoJS.enc.Utf8.parse(this.key);
let encrypted = CryptoJS.DES.encrypt(message, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
},
//解密
decrypt: function(cipherStr) {
let keyHex = CryptoJS.enc.Utf8.parse(this.key);
// direct decrypt cipherStr
let decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(cipherStr)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
};
let ori_pw_str = 'adx3650', pw_str, en_pw_str;
//加密
en_pw_str = DesECBUtil.encrypt(ori_pw_str);
//解密
pw_str = DesECBUtil.decrypt(en_pw_str);
console.log(`原始: ${ori_pw_str} , 加密: '${en_pw_str}' , 解密: '${pw_str}'`);
//原始: adx3650 , 加密: 'AnFhzXnYEjk=' , 解密: 'adx3650'
</script>
2. ECB , CBC 加解密
//需要引入 CryptoJS里的tripledes.js 文件
<script type="text/javascript">
/**
* @Author: aerchi
* @Email: aerchi@gmail.com
* @Date: 2020-12-29 10:52:23
* @Description: Des ECB CBC 加解密工具
*/
var DesUtil = {
key: '&da0f@4d', //加密密钥
ivStr: 'e2fd7b1n', //矢量
ivArray: [0x65, 0x32, 0x66, 0x64, 0x37, 0x62, 0x31, 0x6E], //矢量16进制数组, 等同于 ivStr
// 16进制数组转换成字符串
getStrFromBytes: function(arr) {
let r = "";
for(let i=0; i<arr.length; i++){
r += String.fromCharCode(arr[i]);
}
// console.log(r);
return r;
},
//ECB加密
encryptECB: function(message) {
let keyHex = CryptoJS.enc.Utf8.parse(this.key); // 将秘钥转换为utf8格式
let encrypted = CryptoJS.DES.encrypt(message, this.keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
},
//ECB解密
decryptECB: function(cipherStr) {
let keyHex = CryptoJS.enc.Utf8.parse(this.key); // 将秘钥转换为utf8格式
// direct decrypt cipherStr
let decrypted = CryptoJS.DES.decrypt(
{ciphertext: CryptoJS.enc.Base64.parse(cipherStr)},
keyHex,
{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}
);
return decrypted.toString(CryptoJS.enc.Utf8);
},
//CBC加密
encryptCBC: function(str) {
let keyHex = CryptoJS.enc.Utf8.parse(this.key);
let ivHex = CryptoJS.enc.Utf8.parse(this.ivStr);
// let ivHex = CryptoJS.enc.Utf8.parse(this.getStrFromBytes(this.ivArray)); // 将向量装换位字符串再转为utf8
let encrypted = CryptoJS.DES.encrypt(str, // 因为Java加密时进行了Base64编码,所以此处解码
keyHex,
{
iv: ivHex,
mode: CryptoJS.mode.CBC, // 模式有很多种,由Java代码知道使用的是CBC
padding: CryptoJS.pad.Pkcs7 // 填充模式有很多种,但是Java用的Pkcs5,此处Pkcs7也是可以解密的
}
);
return encrypted.toString();
},
//CBC解密
decryptCBC: function(cipherStr) {
let keyHex = CryptoJS.enc.Utf8.parse(this.key);
let ivHex = CryptoJS.enc.Utf8.parse(this.ivStr);
// let ivHex = CryptoJS.enc.Utf8.parse(this.getStrFromBytes(this.ivArray)); // 将向量装换位字符串再转为utf8
// direct decrypt ciphertext
let decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(cipherStr) // 因为Java加密时进行了Base64编码,所以此处解码
}, keyHex, {
iv: ivHex,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
};
var pw = 'adx3650', // 待解密密文
enpw = 'GFFHGDZY8YU=', // 待解密密文
enStr, str;
enStr = DesUtil.encryptCBC(pw);
console.log(`原始: ${pw} , 解密: '${pw}' , 加密: '${enStr}'`);
//原始: adx3650 , 解密: 'adx3650' , 加密: 'GFFHGDZY8YU='
str = DesUtil.decryptCBC(enpw);
console.log(`原始: ${pw} , 加密: '${enpw}' , 解密: '${str}'`);
//原始: adx3650 , 加密: 'GFFHGDZY8YU=' , 解密: 'adx3650'
</script>
完整示例参考: https://download.csdn.net/download/aerchi/13979644
乐意黎
2020-12-29
本文地址:https://blog.csdn.net/aerchi/article/details/111907403
上一篇: Maven Pom 文件中的隐式依赖导致Jar冲突问题
下一篇: 还有站在风中一脸凌乱的堂叔