mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-03 04:24:05 +08:00
Compare commits
2 Commits
1415200b1a
...
0403c7c693
Author | SHA1 | Date | |
---|---|---|---|
|
0403c7c693 | ||
|
cf8231017d |
@ -8,6 +8,7 @@ yexuejc-base 更新记录
|
||||
1. [FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)增加读取大文件自定义方法和单纯读取方法
|
||||
2. JsonUtil 增加objToMap;优化obj2Json
|
||||
3. DateUtil 标准化日期时间的转换函数
|
||||
4. [AES.java](src/main/java/com/yexuejc/base/encrypt/AES.java) 兼容ECB(虽然不再建议利用)
|
||||
---
|
||||
|
||||
#### version :1.5.2-jre11
|
||||
|
@ -93,7 +93,10 @@ public class AES {
|
||||
byte[] plaintext = new byte[plaintextLength];
|
||||
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
|
||||
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
|
||||
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||
IvParameterSpec ivspec = null;
|
||||
if(!algorithm.name.contains("ECB")){
|
||||
ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||
}
|
||||
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
|
||||
byte[] encrypted = cipher.doFinal(plaintext);
|
||||
return Base64.getEncoder().encodeToString(encrypted);
|
||||
@ -115,7 +118,10 @@ public class AES {
|
||||
byte[] encrypted = Base64.getDecoder().decode(data);
|
||||
Cipher cipher = Cipher.getInstance(algorithm.name);
|
||||
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
|
||||
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||
IvParameterSpec ivspec = null;
|
||||
if(!algorithm.name.contains("ECB")){
|
||||
ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||
}
|
||||
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
|
||||
byte[] original = cipher.doFinal(encrypted);
|
||||
return new String(original, charset).trim();
|
||||
|
55
src/test/java/com/yexuejc/base/encrypt/AESTest.java
Normal file
55
src/test/java/com/yexuejc/base/encrypt/AESTest.java
Normal file
@ -0,0 +1,55 @@
|
||||
package com.yexuejc.base.encrypt;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
public class AESTest {
|
||||
|
||||
@Test
|
||||
public void testEncrypt() throws Exception {
|
||||
String data = "Hello World!";
|
||||
AES aes = AES.builder()
|
||||
.setAlgorithm(AES.ALGORITHM.AES_CBC_PKCS5Padding)
|
||||
.setKey("hj7x89H$yuBI0456")
|
||||
.setIv("NIfb&95GUY86Gfgh")
|
||||
.setCharset(StandardCharsets.UTF_8);
|
||||
String encrypted = aes.encrypt(data);
|
||||
assertNotNull(encrypted);
|
||||
assertFalse(encrypted.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecrypt() throws Exception {
|
||||
String data = "SGVsbG8gV29ybGQh";
|
||||
AES aes = AES.builder()
|
||||
.setAlgorithm(AES.ALGORITHM.AES_CBC_PKCS5Padding)
|
||||
.setKey("hj7x89H$yuBI0456")
|
||||
.setIv("NIfb&95GUY86Gfgh")
|
||||
.setCharset(StandardCharsets.UTF_8);
|
||||
String decrypted = aes.decrypt(data);
|
||||
assertNotNull(decrypted);
|
||||
assertFalse(decrypted.isEmpty());
|
||||
assertEquals("Hello World!", decrypted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncryptAndDecrypt() throws Exception {
|
||||
String data = "张三";
|
||||
AES aes = AES.builder()
|
||||
.setAlgorithm(AES.ALGORITHM.AES_OFB_ISO10126Padding)
|
||||
.setKey("hj7x89H$yuBI0456")
|
||||
.setIv("NIfb&95GUY86Gfgh")
|
||||
.setCharset(StandardCharsets.UTF_8);
|
||||
String encrypt = aes.encrypt(data);
|
||||
System.out.println("加密:" + encrypt);
|
||||
String decrypt = aes.decrypt(encrypt);
|
||||
System.out.println("解密:" + decrypt);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user