Search in sources :

Example 6 with WeGooProvider

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

the class KeyBuilder method convertPublicKey.

/**
 * @param [publicKey]
 * @return java.security.PublicKey
 * @author zhangzhenwei
 * @description 公钥转换  byte[] to {@link PublicKey}
 * @date 2022/2/11 22:34
 * @since 1.0
 */
public PublicKey convertPublicKey(byte[] publicKey) throws WeGooKeyException {
    try {
        SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo.getInstance(publicKey);
        if (keyInfo == null) {
            throw new WeGooKeyException(IExceptionEnum.params_err);
        }
        X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
        KeyFactory factory = KeyFactory.getInstance(keyInfo.getAlgorithm().getAlgorithm().toString(), new WeGooProvider());
        return factory.generatePublic(spec);
    } catch (WeGooCryptoException e) {
        throw e;
    } catch (Exception e) {
        throw new WeGooKeyException(KeyExceptionMessageEnum.structure_public_key_err, e);
    }
}
Also used : WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) BaseWeGooException(com.github.zhenwei.core.exception.BaseWeGooException)

Example 7 with WeGooProvider

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

the class KeyStoreBuilder method gen.

public byte[] gen(String type, PrivateKey privateKey, String alias, String passWd, Certificate[] certChain) throws WeGooCryptoException {
    try {
        KeyStore store = KeyStore.getInstance(type, new WeGooProvider());
        store.load(null);
        store.setKeyEntry(alias, privateKey, passWd.toCharArray(), certChain);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        store.store(outputStream, passWd.toCharArray());
        return outputStream.toByteArray();
    } catch (Exception e) {
        throw new WeGooCryptoException(CryptoExceptionMassageEnum.generate_jks_err, e);
    }
}
Also used : WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyStore(java.security.KeyStore) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException)

Example 8 with WeGooProvider

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

the class KeyStoreBuilder method parse.

public KeyStore parse(String type, String passWd, byte[] jks) throws WeGooCryptoException {
    try {
        KeyStore store = KeyStore.getInstance(type, new WeGooProvider());
        ByteArrayInputStream stream = new ByteArrayInputStream(jks);
        store.load(stream, passWd.toCharArray());
        return store;
    } catch (Exception e) {
        throw new WeGooCryptoException(CryptoExceptionMassageEnum.parse_jks_err, e);
    }
}
Also used : WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) ByteArrayInputStream(java.io.ByteArrayInputStream) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) KeyStore(java.security.KeyStore) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException)

Example 9 with WeGooProvider

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

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

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