Search in sources :

Example 1 with KeyBuilder

use of com.github.zhenwei.sdk.builder.KeyBuilder 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)

Example 2 with KeyBuilder

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

the class CertTest method genCert.

public Certificate genCert() throws Exception {
    KeyBuilder keyBuilder = new KeyBuilder(new WeGooProvider());
    KeyPair keyPair = keyBuilder.buildKeyPair(KeyPairAlgEnum.RSA_1024);
    byte[] certificate = CertBuilder.generateCertificate("O=zhenwei,CN=wegoo,C=CN", "O=zhenwei,CN=wegoo,C=CN", keyPair.getPublic(), keyPair.getPrivate());
    CertBuilder builder = CertBuilder.getInstance(certificate);
    return builder.getCert();
}
Also used : KeyBuilder(com.github.zhenwei.sdk.builder.KeyBuilder) KeyPair(java.security.KeyPair) CertBuilder(com.github.zhenwei.sdk.builder.CertBuilder) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider)

Example 3 with KeyBuilder

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

the class KeyPairTest method genSM2Key.

@Test
public void genSM2Key() throws BaseWeGooException {
    KeyBuilder builder = new KeyBuilder(new WeGooProvider());
    KeyPair keyPair = builder.buildKeyPair(KeyPairAlgEnum.SM2_256);
    System.out.println(Base64Util.encode(keyPair.getPrivate().getEncoded()));
    System.out.println(Base64Util.encode(keyPair.getPublic().getEncoded()));
    SignBuilder signBuilder = new SignBuilder(new WeGooProvider());
    byte[] signatureSourceData = signBuilder.signatureSourceData(SignAlgEnum.SM3_WITH_SM2, keyPair.getPrivate(), "sadfadf".getBytes(StandardCharsets.UTF_8));
    System.out.println(Base64Util.encode(signatureSourceData));
}
Also used : KeyBuilder(com.github.zhenwei.sdk.builder.KeyBuilder) SignBuilder(com.github.zhenwei.sdk.builder.SignBuilder) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) Test(org.junit.Test)

Example 4 with KeyBuilder

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

the class KeyPairTest method getRealKey.

@Test
public void getRealKey() throws BaseWeGooException {
    KeyBuilder builder = new KeyBuilder(new WeGooProvider());
    KeyPair keyPair = builder.buildKeyPair(KeyPairAlgEnum.SM2_256);
    System.out.println("公钥:" + Hex.toHexString(keyPair.getPublic().getEncoded()));
    System.out.println("私钥:" + Hex.toHexString(keyPair.getPrivate().getEncoded()));
    // 获取裸公钥
    byte[] realPublicKey = builder.getRealPublicKey(keyPair.getPublic());
    System.out.println(Hex.toHexString(realPublicKey));
    // 获取裸私钥
    byte[] realPrivateKey = builder.getRealPrivateKey(keyPair.getPrivate());
    System.out.println(Hex.toHexString(realPrivateKey));
}
Also used : KeyBuilder(com.github.zhenwei.sdk.builder.KeyBuilder) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) Test(org.junit.Test)

Example 5 with KeyBuilder

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

the class KeyPairTest method genSm4Key.

@Test
public void genSm4Key() throws BaseWeGooException {
    WeGooProvider weGooProvider = new WeGooProvider();
    Key key = new KeyBuilder(weGooProvider).buildKey(KeyEnum.SM4_128);
    System.out.println(Base64Util.encode(key.getEncoded()));
    System.out.println("key len :" + key.getEncoded().length);
}
Also used : KeyBuilder(com.github.zhenwei.sdk.builder.KeyBuilder) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) BCRSAPublicKey(com.github.zhenwei.provider.jcajce.provider.asymmetric.rsa.BCRSAPublicKey) Test(org.junit.Test)

Aggregations

WeGooProvider (com.github.zhenwei.provider.jce.provider.WeGooProvider)11 KeyBuilder (com.github.zhenwei.sdk.builder.KeyBuilder)11 Test (org.junit.Test)10 KeyPair (java.security.KeyPair)6 BCRSAPublicKey (com.github.zhenwei.provider.jcajce.provider.asymmetric.rsa.BCRSAPublicKey)3 P10Builder (com.github.zhenwei.sdk.builder.P10Builder)2 SignBuilder (com.github.zhenwei.sdk.builder.SignBuilder)2 CertBuilder (com.github.zhenwei.sdk.builder.CertBuilder)1 CipherBuilder (com.github.zhenwei.sdk.builder.CipherBuilder)1 HashBuilder (com.github.zhenwei.sdk.builder.HashBuilder)1 KeyStoreBuilder (com.github.zhenwei.sdk.builder.KeyStoreBuilder)1 CertExtension (com.github.zhenwei.sdk.builder.params.CertExtension)1 DigestParams (com.github.zhenwei.sdk.builder.params.DigestParams)1 BigInteger (java.math.BigInteger)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1