标签:return String java DES crypto js Cipher import
需求:登录的时候WEB或APP将数据加密后传给JAVA后端,后端接收到数据解密后得到数据进行处理。
eg:
明文:12345678 密文:PofrPuMcG5CiXuyR5B5ysQ==
一、java端
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.nio.charset.StandardCharsets; import java.security.SecureRandom; import java.util.Base64; /** * 作者: 唐婉 * 时间: 2022/8/5 11:23 * 描述: DES加密解密算法 */ public class BearDesUtil { /*秘钥 保持一致才能保证加解密的值一样 长度为8的倍数*/ public static final String SECRET_KEY = "79#t7b87#4e2&61c7*4%*a3!@@f4290f"; /** * DES加密 * * @param plaintext 明文 * @return 密文 */ public static String encrypt(String plaintext) { try { byte[] byteContent = plaintext.getBytes(StandardCharsets.UTF_8); DESKeySpec desKey = new DESKeySpec(SECRET_KEY.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, securekey, new SecureRandom()); byte[] byteFinal = cipher.doFinal(byteContent); return Base64.getMimeEncoder().encodeToString(byteFinal); } catch (Exception e) { e.printStackTrace(); return ""; } } /** * DES解密 * * @param ciphertext 密文 * @return 明文 */ public static String decrypt(String ciphertext) { try { byte[] byteContent = Base64.getMimeDecoder().decode(ciphertext);//--- DESKeySpec desKey = new DESKeySpec(SECRET_KEY.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secureKey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, secureKey, new SecureRandom()); return new String(cipher.doFinal(byteContent), "UTF-8"); } catch (Exception e) { e.printStackTrace(); return ""; } } }
二、android端
import android.annotation.SuppressLint; import android.os.Build; import android.util.Base64; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import androidx.annotation.RequiresApi; /** * Auth:Vincent * Time:2022/08/05 * Desc: */ public class DESUtil { /*秘钥*/ public static final String SECRET_KEY = "79#t7b87#4e2&61c7*4%*a3!@@f4290f"; /** * DES加密 * * @param plaintext 明文 * @return 密文 */ public static String encrypt(String plaintext) { try { byte[] byteContent = plaintext.getBytes(StandardCharsets.UTF_8); DESKeySpec desKey = new DESKeySpec(SECRET_KEY.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, securekey, new SecureRandom()); byte[] byteFinal = cipher.doFinal(byteContent); return Base64.encodeToString(byteFinal, Base64.DEFAULT); } catch (Exception e) { e.printStackTrace(); return ""; } } /** * DES解密 * * @param ciphertext 密文 * @return 明文 */ public static String decrypt(String ciphertext) { try { byte[] byteContent = Base64.decode(ciphertext, Base64.DEFAULT); DESKeySpec desKey = new DESKeySpec(SECRET_KEY.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secureKey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, secureKey, new SecureRandom()); return new String(cipher.doFinal(byteContent), "UTF-8"); } catch (Exception e) { e.printStackTrace(); return ""; } } }
三、js
这里只提供加密,解密写法和上面都差不多,一般用不到,所以等哪天闲了或者心情特别好的时候还记得这回事的话再加。
import cryptoJs from 'crypto-js'; export function encryptByDES(message, key) { var keyHex = cryptoJs.enc.Utf8.parse(key); var encrypted = cryptoJs.DES.encrypt(message, keyHex, { mode : cryptoJs.mode.ECB, padding : cryptoJs.pad.Pkcs7 }); return encrypted.toString(); } <!-- 调用 一般随机生成key把密码和和key都传给后端,这里及上面为了一致都写死--> encryptByDES(要加密的密码密码, '79#t7b87#4e2&61c7*4%*a3!@@f4290f');
标签:return,String,java,DES,crypto,js,Cipher,import 来源: https://www.cnblogs.com/smiles365/p/16621073.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。