Search in sources :

Example 1 with CipherBuilder

use of com.github.zhenwei.sdk.builder.CipherBuilder in project LinLong-Java by zhenwei1108.

the class CipherTest method encDecDataTest.

@Test
public void encDecDataTest() throws BaseWeGooException {
    WeGooProvider provider = new WeGooProvider();
    CipherBuilder builder = new CipherBuilder(provider);
    KeyBuilder keyBuilder = new KeyBuilder(provider);
    // SM2加解密
    KeyPair keyPair = keyBuilder.buildKeyPair(KeyPairAlgEnum.SM2_256);
    byte[] data = "this is my encrypt data test".getBytes(StandardCharsets.UTF_8);
    IvParameterSpec spec = new IvParameterSpec(new byte[16]);
    byte[] encryptedData = builder.cipher(CipherAlgEnum.SM2, keyPair.getPublic(), data, spec, true);
    data = builder.cipher(CipherAlgEnum.SM2, keyPair.getPrivate(), encryptedData, spec, false);
    System.out.println("解密结果: " + new String(data));
    // RSA加解密
    keyPair = keyBuilder.buildKeyPair(KeyPairAlgEnum.RSA_2048);
    encryptedData = builder.cipher(CipherAlgEnum.RSA, keyPair.getPublic(), data, spec, true);
    data = builder.cipher(CipherAlgEnum.RSA, keyPair.getPrivate(), encryptedData, spec, false);
    System.out.println("解密结果: " + new String(data));
}
Also used : KeyBuilder(com.github.zhenwei.sdk.builder.KeyBuilder) KeyPair(java.security.KeyPair) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) IvParameterSpec(javax.crypto.spec.IvParameterSpec) CipherBuilder(com.github.zhenwei.sdk.builder.CipherBuilder) Test(org.junit.Test)

Aggregations

WeGooProvider (com.github.zhenwei.provider.jce.provider.WeGooProvider)1 CipherBuilder (com.github.zhenwei.sdk.builder.CipherBuilder)1 KeyBuilder (com.github.zhenwei.sdk.builder.KeyBuilder)1 KeyPair (java.security.KeyPair)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1 Test (org.junit.Test)1