ECB已经不推荐作为加密模式,这里仅供研究学习。
前端JS 加密解密
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
<script src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.1.1/crypto-js.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.1.0/aes.js"></script>
<script>
/**
* 加密
* @param encryptString 要加密的字符串
* @param key 秘钥
* @returns {string} 加密后的字符串
*/
function aesEncrypt(encryptString, key) {
var key = CryptoJS.enc.Utf8.parse(key);
var srcs = CryptoJS.enc.Utf8.parse(encryptString);
var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding });
return encrypted.toString();
}
/**
* 解密
* @param decryptString 要解密的字符串
* @param key 秘钥
* @returns {string} 解密后的字符串
*/
function aesDecrypt(decryptString, key) {
var key = CryptoJS.enc.Utf8.parse(key);
var decrypt = CryptoJS.AES.decrypt(decryptString, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding });
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
var key = "1234567890123456"; // 至少四位数字
var pazzword = "abcdefg012345678abcdefg012345678abcdefg012345678";
// aes 加密
pazzword = aesEncrypt(pazzword, key);
console.log('加密后:' + pazzword);
// 解密
console.log("解密后:" + aesDecrypt("pazzword", key));
</script>
与上面js代码对应的 Java的加密解密
package com.igetcool.yuwen.testcat.utls;标签:AES,加密,String,algorithm,key,import,CryptoJS,nopadding,ECB From: https://blog.51cto.com/u_15815563/5963000
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
public class Test {
public static void main(String[] args) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
System.out.println("abc");
String input = "abcdefg012345678abcdefg012345678abcdefg012345678abcdefg012345678abcdefg012345678abcdefg012345678";
// SecretKey key = Test.generateKey(128);
//
// String format = key.getFormat();
SecretKey key = new SecretKeySpec("1234567890123456".getBytes(), "AES");
IvParameterSpec ivParameterSpec = Test.generateIv();
String algorithm = "AES/ECB/NoPadding";
String cipherText = Test.encrypt(algorithm, input, key, null);
String plainText = Test.decrypt(algorithm, cipherText, key, null);
String mi = "cnFS6fnWBgut9+EsPd+8qHJxUun51gYLrffhLD3fvKhycVLp+dYGC6334Sw937yo";
String plainText2 = Test.decrypt(algorithm, mi, key, null);
String mi3 = "cnFS6fnWBgut9+EsPd+8qHJxUun51gYLrffhLD3fvKhycVLp+dYGC6334Sw937yocnFS6fnWBgut9+EsPd+8qHJxUun51gYLrffhLD3fvKhycVLp+dYGC6334Sw937yocnFS6fnWBgut9+EsPd+8qHJxUun51gYLrffhLD3fvKhycVLp+dYGC6334Sw937yocnFS6fnWBgut9+EsPd+8qHJxUun51gYLrffhLD3fvKhycVLp+dYGC6334Sw937yo";
String plainText3 = Test.decrypt(algorithm, mi3, key, null);
}
public static IvParameterSpec generateIv() {
byte[] iv = new byte[16];
new SecureRandom().nextBytes(iv);
return new IvParameterSpec(iv);
}
public static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(n);
SecretKey key = keyGenerator.generateKey();
return key;
}
public static String encrypt(String algorithm, String input, SecretKey key,
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] cipherText = cipher.doFinal(input.getBytes());
return Base64.getEncoder()
.encodeToString(cipherText);
}
public static String decrypt(String algorithm, String cipherText, SecretKey key,
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] plainText = cipher.doFinal(Base64.getDecoder()
.decode(cipherText));
return new String(plainText);
}
}