recommended

The ECB approach is recommended

CBC mode still has a problem, the back end did not decrypt successfully

use

Front-end encryption interface parameters

The backend decrypts the parameters and returns normal data

Under the bag

npm i -S crypto-js
Copy the code

Aes ECB encryption mode

Create a new crypto. Js file and build the crypto class

import CryptoJS from 'crypto-js'; Class CryptoFile {constructor () {// Constructor () {this.key = cryptojs.enc.utf8.parse ('CRYPTOJSKEY00000'); Parse ('CRYPTOJSKEY00000'); // 16 bits this.iv = cryptojs.enc.utf8.parse ('CRYPTOJSKEY00000'); } // Encrypt encrypt(word) {let words = cryptojs.enc.utf8.parse (word); let encrypted = CryptoJS.AES.encrypt(words, this.key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.toString(); } // Decrypt (word) {let decrypt = cryptojs.aes. decrypt(word, this.key, {mode: cryptojs.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); } } export default new CryptoFile()Copy the code

Front end use

The introduction of class

import crypto from './crypto';
Copy the code

Use it where it is needed

We can test it ourselves, see if it’s encrypted, decrypted

Let data = 'Data you encrypt' let encryptData = crypto.encrypt(json.stringify (data)); // Let decryptData = crypto.decrypt(encryptData) // Decrypted dataCopy the code

Backend Java usage

Java does not support Pkcs7, so you need to set up the JAR package yourself

package com.CryptPacket; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Base64; /** * AES encryption and decryption tool * AES-128: Key and iv are both 16 bytes, 16*8=128bit, Aes-128 */ public class AESCrypt {/** * AES ECB encryption * @param message the string to be encrypted * @param key key * @return Return encrypted ciphertext, Public static String encryptECB(String message, String key) { final String cipherMode = "AES/ECB/PKCS5Padding"; final String charsetName = "UTF-8"; try { byte[] content = new byte[0]; content = message.getBytes(charsetName); // byte[] keyByte = key.getBytes(charsetName); SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES"); Cipher cipher = Cipher.getInstance(cipherMode); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] data = cipher.doFinal(content); final Base64.Encoder encoder = Base64.getEncoder(); final String result = encoder.encodeToString(data); return result; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; } /** * AES ECB decrypt @param messageBase64 ciphertext, base64 encoding @param key key, Public static String decryptECB(String messageBase64, String key) { final String cipherMode = "AES/ECB/PKCS5Padding"; final String charsetName = "UTF-8"; try { final Base64.Decoder decoder = Base64.getDecoder(); byte[] messageByte = decoder.decode(messageBase64); // byte[] keyByte = key.getBytes(charsetName); SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES"); Cipher cipher = Cipher.getInstance(cipherMode); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] content = cipher.doFinal(messageByte); String result = new String(content, charsetName); return result; } catch (Exception e) { e.printStackTrace(); } return null; } public static void Test() {String key = "1234567890123456"; String iv = "1234567890000000"; String MSG = "This is a test this is a test this is a test "; { String encrypt = AESCrypt.encryptCBC(msg, key, iv); System.out.println(encrypt); String decryptStr = AESCrypt.decryptCBC(encrypt, key, iv); System.out.println(decryptStr); } { String encrypt = AESCrypt.encryptECB(msg, key); System.out.println(encrypt); String decryptStr = AESCrypt.decryptECB(encrypt, key); System.out.println(decryptStr); }}}Copy the code

Aes CBC encryption mode

The front end builds the Crypto class

import CryptoJS from 'crypto-js'; Class CryptoFile {constructor () {// Constructor () {this.key = cryptojs.enc.utf8.parse ('CRYPTOJSKEY00000'); Parse ('CRYPTOJSKEY00000'); // 16 bits this.iv = cryptojs.enc.utf8.parse ('CRYPTOJSKEY00000'); } // Encrypt encrypt(word) {let words = cryptojs.enc.utf8.parse (word); let encrypted = CryptoJS.AES.encrypt(words, this.key, { iv: this.iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString().toUpperCase(); } // Decrypt (word) {let encryptedHexStr = cryptojs.enc.hex-parse (word); let words = CryptoJS.enc.Base64.stringify(encryptedHexStr); let decrypt = CryptoJS.AES.decrypt(words, this.key, { iv: this.iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); } } export default new CryptoFile()Copy the code

Front end use

The introduction of class

import crypto from './crypto';
Copy the code

Use it where it is needed

We can test it ourselves, see if it’s encrypted, decrypted

Let data = 'Data you encrypt' let encryptData = crypto.encrypt(json.stringify (data)); // Let decryptData = crypto.decrypt(encryptData) // Decrypted dataCopy the code

Backend Java usage

Java does not support Pkcs7, so you need to download the jar package or set it yourself

package com.CryptPacket; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Base64; /** * AES encryption and decryption tool * AES-128: Key and iv are both 16 bytes, 16*8=128bit, Aes-128 */ public class AESCrypt {/** * AES CBC encryption * @param message string to be encrypted * @param key key * @param iv iv, * @return Returns the encrypted ciphertext. Public static String encryptCBC(String message, String key, String iv) { final String cipherMode = "AES/CBC/PKCS5Padding"; final String charsetName = "UTF-8"; try { byte[] content = new byte[0]; content = message.getBytes(charsetName); // byte[] keyByte = key.getBytes(charsetName); SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES"); // byte[] ivByte = iv.getBytes(charsetName); IvParameterSpec ivSpec = new IvParameterSpec(ivByte); Cipher cipher = Cipher.getInstance(cipherMode); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); byte[] data = cipher.doFinal(content); final Base64.Encoder encoder = Base64.getEncoder(); final String result = encoder.encodeToString(data); return result; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; } /** * AES CBC decrypt * @param messageBase64 ciphertext, base64 encoding * @param key, same as encryption * @param iv iv, @return Decrypted data */ public static String decryptCBC(String messageBase64, String key, String iv) { final String cipherMode = "AES/CBC/PKCS5Padding"; final String charsetName = "UTF-8"; try { final Base64.Decoder decoder = Base64.getDecoder(); byte[] messageByte = decoder.decode(messageBase64); // byte[] keyByte = key.getBytes(charsetName); SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES"); // byte[] ivByte = iv.getBytes(charsetName); IvParameterSpec ivSpec = new IvParameterSpec(ivByte); Cipher cipher = Cipher.getInstance(cipherMode); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] content = cipher.doFinal(messageByte); String result = new String(content, charsetName); return result; } catch (Exception e) { e.printStackTrace(); } return null; } public static void Test() {String key = "1234567890123456"; String iv = "1234567890000000"; String MSG = "This is a test this is a test this is a test "; { String encrypt = AESCrypt.encryptCBC(msg, key, iv); System.out.println(encrypt); String decryptStr = AESCrypt.decryptCBC(encrypt, key, iv); System.out.println(decryptStr); } { String encrypt = AESCrypt.encryptECB(msg, key); System.out.println(encrypt); String decryptStr = AESCrypt.decryptECB(encrypt, key); System.out.println(decryptStr); }}}Copy the code

The resources

AES encryption and decryption, JS front-end and Java back-end implementation

Front-end CryptoJS library – A guide to Using CryptoJS

Powerful front-end encryption/decryption JS library Crypto-JS uses parsing

Aes encrypts and decrypts Cryptojs and Java