看到网上很多说要换jar,注意该问题。
好像jdk1.8.0_162及以后是不需要替换的。
public
String
encryyptstr
(
byte
[
]
iv
,
String
key
,
String
text
)
{
String
algorithm
=
"AES"
;
String
transform
=
"AES/CBC/PKCS5Padding"
;
Cipher
cipher
=
null
;
String
result
=
""
;
try
{
SDK
.
getLogAPI
(
)
.
getLogger
(
LoginAdapter
.
class
)
.
info
(
"key256之后为:"
+
key
)
;
byte
[
]
keyarr
=
autoKey
(
getSHA256Str
(
key
)
)
;
System
.
out
.
println
(
new
String
(
Base64
.
encode
(
keyarr
)
)
)
;
SecretKeySpec
keySpec
=
new
SecretKeySpec
(
keyarr
,
algorithm
)
;
cipher
=
Cipher
.
getInstance
(
transform
)
;
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
keySpec
,
new
IvParameterSpec
(
iv
)
)
;
byte
[
]
cipherData
=
cipher
.
doFinal
(
text
.
getBytes
(
"UTF-8"
)
)
;
result
=
new
String
(
Base64
.
encode
(
cipherData
)
)
;
}
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
(
)
;
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
(
)
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
System
.
out
.
println
(
result
)
;
return
result
;
private
static
byte
[
]
autoKey
(
byte
[
]
key
)
{
byte
[
]
bytes
=
new
byte
[
32
]
;
for
(
int
i
=
0
;
i
<
32
;
i
++
)
{
bytes
[
i
]
=
0
;
if
(
key
.
length
>=
32
)
{
System
.
arraycopy
(
key
,
0
,
bytes
,
0
,
32
)
;
}
else
{
System
.
arraycopy
(
key
,
0
,
bytes
,
0
,
key
.
length
)
;
return
bytes
;
public
byte
[
]
getSHA256Str
(
String
str
)
{
MessageDigest
messageDigest
;
byte
[
]
encdeStr
=
null
;
try
{
messageDigest
=
MessageDigest
.
getInstance
(
"SHA-256"
)
;
encdeStr
=
messageDigest
.
digest
(
str
.
getBytes
(
"UTF-8"
)
)
;
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
(
)
;
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
(
)
;
return
encdeStr
;
看到网上很多说要换jar,注意该问题。好像jdk1.8.0_162及以后是不需要替换的。//加密 public String encryyptstr(byte[] iv,String key,String text){ String algorithm = "AES"; String transform = "AES/CBC/PKCS5Padding";// Cipher cipher = null; String result =
let key = '9815c9e69268bc2fc2085c1lc6q75a42';
let charset = 'utf8';
let cipherEncoding = 'base64';
let iv = '9183296589LSCQEG'.
package com.handler;
import
java
.io.UnsupportedEncodingException;
import
java
.security.Key;
import
java
.security.Security;
import
java
x.crypto.Cipher;
import
java
x.crypto.KeyGenerator;
在之前尝试前端使用过 crypto-js,但是一直报 Malformed UTF-8 data ,尝试了百度搜索第一页的所有方案无效,于是尝试在 crypto-js 库 github 的 issue 寻找方案,但这个问题在 2020年3月3号被提出,到了目前还没有被解决。无奈之下只能在 github上查找相关的库,直到找到了 https://github.com/TCWTEAM/CrypJS.git,本文的前端代码由 CrypJS 修改而来
key 的长度
key 的长度需要注意一下,为 16 位时
import
java
x.crypto.Cipher;
import
java
x.crypto.spec.IvParameterSpec;
import
java
x.crypto.spec.SecretKeySpec;
public class
AES
CBC
{
private static final String ALGORITHM = "
AES
/
CBC
/PKCS5Padding";
private static final byte[] IV = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
public static byte[] encrypt(byte[] key, byte[] data) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, "
AES
");
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(IV));
return cipher.doFinal(data);
public static byte[] decrypt(byte[] key, byte[] data) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, "
AES
");
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(IV));
return cipher.doFinal(data);
public static void main(String[] args) throws Exception {
byte[] key = "1234567890123456".getBytes();
byte[] data = "Hello World!".getBytes();
byte[] encryptedData = encrypt(key, data);
System.out.println("Encrypted Data: " + new String(encryptedData));
byte[] decryptedData = decrypt(key, encryptedData);
System.out.println("Decrypted Data: " + new String(decryptedData));
在此示例代码中,我们使用
AES
-
CBC
算法进行加密和解密。首先,我们定义了算法名称和IV(初始化向量)。然后,我们定义了两个方法encrypt()和decrypt()来
实现
加密和解密操作。在这些方法中,我们创建一个SecretKeySpec对象,该对象包含密钥。然后,我们创建一个Cipher对象,该对象使用encrypt()方法时传入的密钥、IV和算法进行初始化。然后我们调用doFinal()方法来加密或解密数据。在main()方法中,我们使用一个简单的字符串作为密钥和数据来测试我们的加密和解密方法。