需求:登录的时候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');
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/282036.html