mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-08-05 18:29:31 +08:00
Compare commits
No commits in common. "0403c7c6936a2f82526cdf43b19184c52a115cda" and "1415200b1aa6e1f5b50bbea35e7b8fb07a8a5997" have entirely different histories.
0403c7c693
...
1415200b1a
@ -8,7 +8,6 @@ yexuejc-base 更新记录
|
|||||||
1. [FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)增加读取大文件自定义方法和单纯读取方法
|
1. [FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)增加读取大文件自定义方法和单纯读取方法
|
||||||
2. JsonUtil 增加objToMap;优化obj2Json
|
2. JsonUtil 增加objToMap;优化obj2Json
|
||||||
3. DateUtil 标准化日期时间的转换函数
|
3. DateUtil 标准化日期时间的转换函数
|
||||||
4. [AES.java](src/main/java/com/yexuejc/base/encrypt/AES.java) 兼容ECB(虽然不再建议利用)
|
|
||||||
---
|
---
|
||||||
|
|
||||||
#### version :1.5.2-jre11
|
#### version :1.5.2-jre11
|
||||||
|
@ -93,10 +93,7 @@ public class AES {
|
|||||||
byte[] plaintext = new byte[plaintextLength];
|
byte[] plaintext = new byte[plaintextLength];
|
||||||
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
|
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
|
||||||
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
|
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
|
||||||
IvParameterSpec ivspec = null;
|
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||||
if(!algorithm.name.contains("ECB")){
|
|
||||||
ivspec = new IvParameterSpec(iv.getBytes(charset));
|
|
||||||
}
|
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
|
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
|
||||||
byte[] encrypted = cipher.doFinal(plaintext);
|
byte[] encrypted = cipher.doFinal(plaintext);
|
||||||
return Base64.getEncoder().encodeToString(encrypted);
|
return Base64.getEncoder().encodeToString(encrypted);
|
||||||
@ -118,10 +115,7 @@ public class AES {
|
|||||||
byte[] encrypted = Base64.getDecoder().decode(data);
|
byte[] encrypted = Base64.getDecoder().decode(data);
|
||||||
Cipher cipher = Cipher.getInstance(algorithm.name);
|
Cipher cipher = Cipher.getInstance(algorithm.name);
|
||||||
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
|
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
|
||||||
IvParameterSpec ivspec = null;
|
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||||
if(!algorithm.name.contains("ECB")){
|
|
||||||
ivspec = new IvParameterSpec(iv.getBytes(charset));
|
|
||||||
}
|
|
||||||
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
|
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
|
||||||
byte[] original = cipher.doFinal(encrypted);
|
byte[] original = cipher.doFinal(encrypted);
|
||||||
return new String(original, charset).trim();
|
return new String(original, charset).trim();
|
||||||
|
@ -1,55 +0,0 @@
|
|||||||
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