Search in sources :

Example 1 with KeyPairAlgEnum

use of com.github.zhenwei.core.enums.KeyPairAlgEnum in project LinLong-Java by zhenwei1108.

the class WeGooKeyProtector method recover.

public Key recover(EncryptedPrivateKeyInfo var1) throws UnrecoverableKeyException {
    AlgorithmId var7 = var1.getAlgorithm();
    if (!var7.getOID().toString().equals("1.3.6.1.4.1.42.2.17.1.1")) {
        throw new UnrecoverableKeyException("Unsupported key protection algorithm");
    } else {
        byte[] var8 = var1.getEncryptedData();
        byte[] var9 = new byte[20];
        System.arraycopy(var8, 0, var9, 0, 20);
        int var6 = var8.length - 20 - 20;
        int var4 = var6 / 20;
        if (var6 % 20 != 0) {
            ++var4;
        }
        byte[] var10 = new byte[var6];
        System.arraycopy(var8, 20, var10, 0, var6);
        byte[] var11 = new byte[var10.length];
        int var2 = 0;
        int var5 = 0;
        byte[] var3;
        for (var3 = var9; var2 < var4; var5 += 20) {
            this.md.update(this.passwdBytes);
            this.md.update(var3);
            var3 = this.md.digest();
            this.md.reset();
            if (var2 < var4 - 1) {
                System.arraycopy(var3, 0, var11, var5, var3.length);
            } else {
                System.arraycopy(var3, 0, var11, var5, var11.length - var5);
            }
            ++var2;
        }
        byte[] var12 = new byte[var10.length];
        for (var2 = 0; var2 < var12.length; ++var2) {
            var12[var2] = (byte) (var10[var2] ^ var11[var2]);
        }
        this.md.update(this.passwdBytes);
        Arrays.fill(this.passwdBytes, (byte) 0);
        this.passwdBytes = null;
        this.md.update(var12);
        var3 = this.md.digest();
        this.md.reset();
        for (var2 = 0; var2 < var3.length; ++var2) {
            if (var3[var2] != var8[20 + var6 + var2]) {
                throw new UnrecoverableKeyException("Cannot recover key");
            }
        }
        try {
            // return PKCS8Key.parseKey(new DerValue(var12));
            PrivateKeyInfo info = PrivateKeyInfo.getInstance(var12);
            if (info == null) {
                throw new UnrecoverableKeyException("Recover key can not null");
            }
            KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
            PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(var12);
            KeyFactory factory = KeyFactory.getInstance(algEnum.getAlg(), new WeGooProvider());
            return factory.generatePrivate(spec);
        } catch (Exception var14) {
            throw new UnrecoverableKeyException(var14.getMessage());
        }
    }
}
Also used : AlgorithmId(sun.security.x509.AlgorithmId) KeyPairAlgEnum(com.github.zhenwei.core.enums.KeyPairAlgEnum) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) EncryptedPrivateKeyInfo(sun.security.pkcs.EncryptedPrivateKeyInfo) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) IOException(java.io.IOException)

Example 2 with KeyPairAlgEnum

use of com.github.zhenwei.core.enums.KeyPairAlgEnum 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 3 with KeyPairAlgEnum

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

Aggregations

PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)3 KeyPairAlgEnum (com.github.zhenwei.core.enums.KeyPairAlgEnum)3 BaseWeGooException (com.github.zhenwei.core.exception.BaseWeGooException)2 WeGooCryptoException (com.github.zhenwei.core.exception.WeGooCryptoException)2 WeGooKeyException (com.github.zhenwei.core.exception.WeGooKeyException)2 WeGooProvider (com.github.zhenwei.provider.jce.provider.WeGooProvider)2 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)2 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 DLSequence (com.github.zhenwei.core.asn1.DLSequence)1 IOException (java.io.IOException)1 EncryptedPrivateKeyInfo (sun.security.pkcs.EncryptedPrivateKeyInfo)1 AlgorithmId (sun.security.x509.AlgorithmId)1