Search in sources :

Example 1 with WeGooKeyException

use of com.github.zhenwei.core.exception.WeGooKeyException in project LinLong-Java by zhenwei1108.

the class KeyBuilder method buildKey.

/**
 * @param [keyEnum]
 * @return java.security.Key
 * @author zhangzhenwei
 * @description 生成对称密钥
 * @date 2022/2/11 22:34
 * @since 1.0
 */
public Key buildKey(KeyEnum keyEnum) throws BaseWeGooException {
    try {
        val generator = KeyGenerator.getInstance(keyEnum.getAlg(), provider);
        generator.init(keyEnum.getKeyLen(), new SecureRandom());
        return generator.generateKey();
    } catch (Exception e) {
        throw new WeGooKeyException(KeyExceptionMessageEnum.generate_key_err, e);
    }
}
Also used : lombok.val(lombok.val) WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) BaseWeGooException(com.github.zhenwei.core.exception.BaseWeGooException)

Example 2 with WeGooKeyException

use of com.github.zhenwei.core.exception.WeGooKeyException in project LinLong-Java by zhenwei1108.

the class KeyBuilder method buildKeyPair.

/**
 * @param [keyPairEnum]
 * @return java.security.KeyPair
 * @author zhangzhenwei
 * @description 生成非对称密钥对
 * @date 2022/2/11 22:35
 * @since 1.0
 */
public KeyPair buildKeyPair(KeyPairAlgEnum keyPairEnum) throws WeGooKeyException {
    try {
        var generator = KeyPairGenerator.getInstance(keyPairEnum.getAlg(), provider);
        if (keyPairEnum == KeyPairAlgEnum.SM2_256) {
            // SM2 算法曲线
            var name = GMNamedCurves.getName(GMObjectIdentifiers.sm2p256v1);
            var sm2Spec = new ECGenParameterSpec(name);
            generator.initialize(sm2Spec, new SecureRandom());
        } else {
            generator.initialize(keyPairEnum.getKeyLen());
        }
        return generator.generateKeyPair();
    } catch (Exception e) {
        throw new WeGooKeyException(KeyExceptionMessageEnum.generate_keypair_err, e);
    }
}
Also used : WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) lombok.var(lombok.var) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) BaseWeGooException(com.github.zhenwei.core.exception.BaseWeGooException)

Example 3 with WeGooKeyException

use of com.github.zhenwei.core.exception.WeGooKeyException in project LinLong-Java by zhenwei1108.

the class KeyBuilder method convertPrivateKey.

/**
 * @param [privateKey]
 * @return java.security.PrivateKey
 * @author zhangzhenwei
 * @description 私钥转换  byte[]  to  {@link PrivateKey}
 * @date 2022/2/11 22:34
 * @since 1.0
 */
public PrivateKey convertPrivateKey(byte[] privateKey) throws Exception {
    try {
        PrivateKeyInfo info = PrivateKeyInfo.getInstance(privateKey);
        if (info == null) {
            throw new WeGooKeyException(IExceptionEnum.params_err);
        }
        KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
        KeyFactory factory = KeyFactory.getInstance(algEnum.getAlg(), new WeGooProvider());
        return factory.generatePrivate(spec);
    } catch (WeGooCryptoException e) {
        throw e;
    } catch (Exception e) {
        throw new WeGooKeyException(KeyExceptionMessageEnum.structure_private_key_err, e);
    }
}
Also used : WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) KeyPairAlgEnum(com.github.zhenwei.core.enums.KeyPairAlgEnum) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) BaseWeGooException(com.github.zhenwei.core.exception.BaseWeGooException)

Example 4 with WeGooKeyException

use of com.github.zhenwei.core.exception.WeGooKeyException in project LinLong-Java by zhenwei1108.

the class KeyBuilder method getRealPrivateKey.

/**
 * @param [privateKey]
 * @return byte[]
 * @author zhangzhenwei
 * @description 获取裸私钥
 * @date 2022/2/11 23:06
 * @since 1.0
 */
public byte[] getRealPrivateKey(byte[] privateKey) throws WeGooCryptoException {
    try {
        PrivateKeyInfo info = PrivateKeyInfo.getInstance(privateKey);
        if (info == null) {
            throw new WeGooKeyException(IExceptionEnum.params_err);
        }
        KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
        // SM2 算法
        if (algEnum.getAlg().equals(KeyPairAlgEnum.SM2_256.getAlg())) {
            DLSequence dlSequence = (DLSequence) DLSequence.fromByteArray(privateKey);
            byte[] priKeys = ((DEROctetString) dlSequence.getObjectAt(2)).getOctets();
            dlSequence = (DLSequence) DLSequence.fromByteArray(priKeys);
            DEROctetString derPriKey = (DEROctetString) dlSequence.getObjectAt(1);
            return derPriKey.getOctets();
        } else {
            return info.getPrivateKey().getOctets();
        }
    } catch (WeGooKeyException e) {
        throw e;
    } catch (Exception e) {
        throw new WeGooKeyException(KeyExceptionMessageEnum.parse_private_key_err, e);
    }
}
Also used : WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) DLSequence(com.github.zhenwei.core.asn1.DLSequence) KeyPairAlgEnum(com.github.zhenwei.core.enums.KeyPairAlgEnum) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) BaseWeGooException(com.github.zhenwei.core.exception.BaseWeGooException)

Example 5 with WeGooKeyException

use of com.github.zhenwei.core.exception.WeGooKeyException 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)

Aggregations

BaseWeGooException (com.github.zhenwei.core.exception.BaseWeGooException)5 WeGooCryptoException (com.github.zhenwei.core.exception.WeGooCryptoException)5 WeGooKeyException (com.github.zhenwei.core.exception.WeGooKeyException)5 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)2 KeyPairAlgEnum (com.github.zhenwei.core.enums.KeyPairAlgEnum)2 WeGooProvider (com.github.zhenwei.provider.jce.provider.WeGooProvider)2 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 DLSequence (com.github.zhenwei.core.asn1.DLSequence)1 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)1 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 lombok.val (lombok.val)1 lombok.var (lombok.var)1