Search in sources :

Example 11 with WeGooProvider

use of com.github.zhenwei.provider.jce.provider.WeGooProvider 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 12 with WeGooProvider

use of com.github.zhenwei.provider.jce.provider.WeGooProvider 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)

Example 13 with WeGooProvider

use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.

the class KeyPairTest method covertKeyPair.

@Test
public void covertKeyPair() throws Exception {
    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()));
    PublicKey publicKey = builder.convertPublicKey(keyPair.getPublic().getEncoded());
    PrivateKey privateKey = builder.convertPrivateKey(keyPair.getPrivate().getEncoded());
    System.out.println("公钥:" + Hex.toHexString(publicKey.getEncoded()));
    System.out.println("私钥:" + Hex.toHexString(privateKey.getEncoded()));
}
Also used : KeyBuilder(com.github.zhenwei.sdk.builder.KeyBuilder) BCRSAPublicKey(com.github.zhenwei.provider.jcajce.provider.asymmetric.rsa.BCRSAPublicKey) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) Test(org.junit.Test)

Example 14 with WeGooProvider

use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.

the class KeyPairTest method genRsa1024Key.

@Test
public void genRsa1024Key() throws BaseWeGooException {
    KeyBuilder builder = new KeyBuilder(new WeGooProvider());
    KeyPair keyPair = builder.buildKeyPair(KeyPairAlgEnum.RSA_1024);
    System.out.println(Base64Util.encode(keyPair.getPrivate().getEncoded()));
    System.out.println(Base64Util.encode(keyPair.getPublic().getEncoded()));
    BCRSAPublicKey aPublic = (BCRSAPublicKey) keyPair.getPublic();
    // 大素数
    System.out.println("modulus:" + aPublic.getModulus().toString());
    BigInteger mod = aPublic.getModulus().mod(BigInteger.valueOf(128));
    System.out.println(mod);
    // 指数
    System.out.println("exponent:" + aPublic.getPublicExponent().intValue());
}
Also used : KeyBuilder(com.github.zhenwei.sdk.builder.KeyBuilder) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) BigInteger(java.math.BigInteger) BCRSAPublicKey(com.github.zhenwei.provider.jcajce.provider.asymmetric.rsa.BCRSAPublicKey) Test(org.junit.Test)

Example 15 with WeGooProvider

use of com.github.zhenwei.provider.jce.provider.WeGooProvider in project LinLong-Java by zhenwei1108.

the class KeyStoreTest method genJks.

@Test
public void genJks() throws Exception {
    KeyStoreBuilder builder = new KeyStoreBuilder();
    KeyBuilder keyBuilder = new KeyBuilder(new WeGooProvider());
    KeyPair keyPair = keyBuilder.buildKeyPair(KeyPairAlgEnum.SM2_256);
    P10Builder p10Builder = new P10Builder("C=CN,CN=demo,O=zhenwei", keyPair.getPublic(), keyPair.getPrivate(), null);
    byte[] certificate = CertBuilder.generateCertificate("C=CN,CN=demo,O=zhenwei", "C=CN,CN=demo", keyPair.getPublic(), keyPair.getPrivate());
    System.out.println("证书:" + Base64Util.encode(certificate));
    System.out.println(Hex.toHexString(keyPair.getPrivate().getEncoded()));
    X509Certificate cert = CertBuilder.getInstance(certificate).getCert();
    byte[] jks = builder.genJks(keyPair.getPrivate(), "test", "123123", new Certificate[] { cert });
    System.out.println(Hex.toHexString(jks));
}
Also used : KeyBuilder(com.github.zhenwei.sdk.builder.KeyBuilder) KeyPair(java.security.KeyPair) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) KeyStoreBuilder(com.github.zhenwei.sdk.builder.KeyStoreBuilder) P10Builder(com.github.zhenwei.sdk.builder.P10Builder) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Aggregations

WeGooProvider (com.github.zhenwei.provider.jce.provider.WeGooProvider)18 KeyBuilder (com.github.zhenwei.sdk.builder.KeyBuilder)11 Test (org.junit.Test)10 KeyPair (java.security.KeyPair)6 WeGooCryptoException (com.github.zhenwei.core.exception.WeGooCryptoException)4 BCRSAPublicKey (com.github.zhenwei.provider.jcajce.provider.asymmetric.rsa.BCRSAPublicKey)3 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)2 KeyPairAlgEnum (com.github.zhenwei.core.enums.KeyPairAlgEnum)2 BaseWeGooException (com.github.zhenwei.core.exception.BaseWeGooException)2 WeGooKeyException (com.github.zhenwei.core.exception.WeGooKeyException)2 P10Builder (com.github.zhenwei.sdk.builder.P10Builder)2 SignBuilder (com.github.zhenwei.sdk.builder.SignBuilder)2 KeyStore (java.security.KeyStore)2 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)2 X500Name (com.github.zhenwei.core.asn1.x500.X500Name)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 CertBuilder (com.github.zhenwei.sdk.builder.CertBuilder)1 CipherBuilder (com.github.zhenwei.sdk.builder.CipherBuilder)1 HashBuilder (com.github.zhenwei.sdk.builder.HashBuilder)1