57 lines
2.0 KiB
Java
57 lines
2.0 KiB
Java
package api;
|
|
|
|
import com.sunyard.chsm.enums.AlgMode;
|
|
import com.sunyard.chsm.param.SymDecryptReq;
|
|
import com.sunyard.chsm.param.SymDecryptResp;
|
|
import com.sunyard.chsm.param.SymEncryptReq;
|
|
import com.sunyard.chsm.param.SymEncryptResp;
|
|
import com.sunyard.chsm.utils.CodecUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.junit.jupiter.api.Assertions;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
/**
|
|
* @author liulu
|
|
* @since 2024/12/18
|
|
*/
|
|
@Slf4j
|
|
public class SymKeyTest extends BaseTest {
|
|
|
|
private static final String plain = "hjsu234127qikqwndqqw13412as324";
|
|
private final static byte[] iv = "ghwikdhj1234713v".getBytes();
|
|
|
|
@Test
|
|
public void testEncrypt() {
|
|
Long keyId = 1869562427636801538L;
|
|
|
|
SymEncryptReq symEncryptReq = new SymEncryptReq();
|
|
symEncryptReq.setKeyId(keyId);
|
|
symEncryptReq.setPlainData(CodecUtils.encodeBase64(plain.getBytes()));
|
|
symEncryptReq.setMode(AlgMode.ECB);
|
|
|
|
SymEncryptResp symEncryptResp = execute("/sym/encrypt", symEncryptReq, SymEncryptResp.class);
|
|
log.info("SymEncryptResp: {}", symEncryptResp);
|
|
Assertions.assertNotNull(symEncryptResp);
|
|
Assertions.assertTrue(StringUtils.hasText(symEncryptResp.getKeyIndex()));
|
|
byte[] cipherData = CodecUtils.decodeBase64(symEncryptResp.getCipherData());
|
|
Assertions.assertEquals(32, cipherData.length);
|
|
|
|
SymDecryptReq decryptReq = new SymDecryptReq();
|
|
decryptReq.setKeyId(keyId);
|
|
decryptReq.setKeyIndex(symEncryptResp.getKeyIndex());
|
|
decryptReq.setMode(AlgMode.ECB);
|
|
decryptReq.setCipherData(symEncryptResp.getCipherData());
|
|
SymDecryptResp decryptResp = execute("/sym/decrypt", decryptReq, SymDecryptResp.class);
|
|
log.info("SymDecryptResp: {}", decryptResp);
|
|
Assertions.assertNotNull(decryptResp);
|
|
String calPlain = new String(CodecUtils.decodeBase64(decryptResp.getPlainData()));
|
|
Assertions.assertEquals(plain, calPlain);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|