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));
}
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();
}
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));
}
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));
}
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);
}
Aggregations