新增密码传输解密服务
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.ruoyi.framework.web.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
|
||||
class PasswordTransferCryptoServiceTest
|
||||
{
|
||||
private static final String KEY = "1234567890abcdef";
|
||||
|
||||
private PasswordTransferCryptoService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp()
|
||||
{
|
||||
service = new PasswordTransferCryptoService();
|
||||
ReflectionTestUtils.setField(service, "key", KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDecryptValidCipherText() throws Exception
|
||||
{
|
||||
String plain = service.decrypt(encrypt("admin123"));
|
||||
assertEquals("admin123", plain);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectInvalidCipherText()
|
||||
{
|
||||
assertThrows(ServiceException.class, () -> service.decrypt("not-base64"));
|
||||
}
|
||||
|
||||
private String encrypt(String plainText) throws Exception
|
||||
{
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES"));
|
||||
return Base64.getEncoder().encodeToString(cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user