原文地址:cryptoJs DES_CBC_Pkcs7 转成 Java - 唯学而知 - 博客园
前端 DES 加密:
import cryptoJs from 'crypto-js'; // DES 加密 function encrypt(message, key, iv) { // 字符串转 16进制 const keyHex = cryptoJs.enc.Utf8.parse(key); const ivHex = cryptoJs.enc.Utf8.parse(iv); const option = { iv: ivHex, mode: cryptoJs.mode.CBC, padding: cryptoJs.pad.Pkcs7, }; // 加密 const encrypted = cryptoJs.DES.encrypt(message, keyHex, option); console.log( '------------encrypted------------', '\nmessage: ' + message, '\nkey: ' + key, '\niv: ' + iv, '\nkeyHex: ' + keyHex, '\nivHex: ' + ivHex, '\nencrypted: ' + encrypted, // 将 encrypted 转成 BASE64 字符串 '\nencrypted.ciphertext: ' + encrypted.ciphertext.toString() // 将 encrypted 转成十六进制字符串 ); return encrypted.ciphertext.toString(); } // 调用加密方法 let encrypted = encrypt('{"name":"xiaoming","id":"112233","sex":"0"}', '12345678', '12345678');
打印结果:
------------encrypted------------ message: {"name":"xiaoming","id":"112233","sex":"0"} key: 12345678 iv: 12345678 keyHex: 3132333435363738 ivHex: 3132333435363738 encrypted: nkzh3knwg2TwyS2Tmslfbk6a10DRqO/Sc2IMJ6abKXAsdkrR1z8X7LFJnCRbtV+s encrypted.ciphertext: 9e4ce1de49f08364f0c92d939ac95f6e4e9ad740d1a8efd273620c27a69b29702c764ad1d73f17ecb1499c245bb55fac
对应的 Java 代码为:
/** * 算法类型 */ private final static String ALGORITHM_DES = "DES"; /** * 加密算法/加密模式/填充类型 */ private static final String CIPHER_DES_MODE_CBC_PKCS5PADDING = "DES/CBC/PKCS5Padding"; /** * 字符编码 */ public static final String CHARSET_NAME = "UTF-8"; /** * 字节数组转成16进制字符串 * * @param bytes * @return */ public static String byte2hex(byte[] bytes) { StringBuffer sb = new StringBuffer(bytes.length * 2); String tmp = ""; for (int n = 0; n < bytes.length; n++) { // 整数转成十六进制表示 tmp = (Integer.toHexString(bytes[n] & 0XFF)); if (tmp.length() == 1) { sb.append("0"); } sb.append(tmp); } return sb.toString().toUpperCase(); } /** * DES 加密 * * @param data * @param key * @param iv * @return * @throws Exception */ public static String encryptCbcPkcs5padding(String data, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance(CIPHER_DES_MODE_CBC_PKCS5PADDING); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), ALGORITHM_DES); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] result = cipher.doFinal(data.getBytes(CHARSET_NAME)); // String encrypted = new sun.misc.BASE64Encoder().encode(result); // System.out.println("encrypted: " + encrypted); String ciphertext = byte2hex(result); return ciphertext.toLowerCase(); }
前端 DES 解密:
function decrypt(ciphertext, key, iv) { const keyHex = cryptoJs.enc.Utf8.parse(key); const ivHex = cryptoJs.enc.Utf8.parse(iv); // 解密 const decrypted = cryptoJs.DES.decrypt( { ciphertext: cryptoJs.enc.Hex.parse(ciphertext), }, keyHex, { iv: ivHex, mode: cryptoJs.mode.CBC, padding: cryptoJs.pad.Pkcs7, }, ); console.log( '------------decrypted------------', '\nciphertext: ' + ciphertext, '\nkey: ' + key, '\niv: ' + iv, '\nkeyHex: ' + keyHex, '\nivHex: ' + ivHex, '\ndecrypted.toString(cryptoJs.enc.Utf8): ' + decrypted.toString(cryptoJs.enc.Utf8) ); return decrypted.toString(cryptoJs.enc.Utf8); }
打印结果:
------------decrypted------------ ciphertext: 9e4ce1de49f08364f0c92d939ac95f6e4e9ad740d1a8efd273620c27a69b29702c764ad1d73f17ecb1499c245bb55fac key: 12345678 iv: 12345678 keyHex: 3132333435363738 ivHex: 3132333435363738 decrypted.toString(cryptoJs.enc.Utf8): {"name":"xiaoming","id":"112233","sex":"0"}
对应的 Java 代码为:
/** * DES 解密 * * @param dataEncode * @param key * @param iv * @return * @throws Exception */ public static String decryptCbcPkcs5padding(String dataEncode, String key, String iv) throws Exception { byte[] encrypted = Convert.hexToBytes(dataEncode); Cipher cipher = Cipher.getInstance(CIPHER_DES_MODE_CBC_PKCS5PADDING); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), ALGORITHM_DES); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); return new String(cipher.doFinal(encrypted)); }
- 基于VueCli自定义创建项目
- IDEA项目名称后面出现中括号,模块Modules的名子和文件夹名称不同,可以右键修改名称也可以在File->Project Structure 修改Modules的Name(快捷键ctrl+Shift+Alt+s)
- 去年最火的 JS 开源项目「GitHub 热点速览」
- 用jacoco统计JAVA项目测试代码覆盖率
- 分享uniapp移动端和h5项目如何使用谷歌地图
- Pycharm项目文件变黄
- 【Leetcode1949. 坚定的友谊】使用MySQL在无向图中寻找{"CompleteTripartite", {1, 1, 3}}这个pattern
- java项目(jar包)配置为开机自启 jar包bat脚本启动和配置为开机自启
- Python中出现"No module named 'requests'"的图文解决办法
- web项目启动时dubbo报错: No provider available for the service com.davidhu.shopguide.api.service.UserEventService from the url zookeeper ://localhost:2181/org.apache.du
本栏目推荐文章