Search in sources :

Example 1 with DomainParameters

use of com.github.zhenwei.core.asn1.x9.DomainParameters in project opb-sdk-java by bianjieai.

the class BCECUtils method buildECPublicKeyByPrivateKey.

/**
 * 根据EC私钥构造EC公钥
 *
 * @param priKey ECC私钥参数对象
 * @return
 */
public static ECPublicKeyParameters buildECPublicKeyByPrivateKey(ECPrivateKeyParameters priKey) {
    ECDomainParameters domainParameters = priKey.getParameters();
    ECPoint q = new FixedPointCombMultiplier().multiply(domainParameters.getG(), priKey.getD());
    return new ECPublicKeyParameters(q, domainParameters);
}
Also used : FixedPointCombMultiplier(org.bouncycastle.math.ec.FixedPointCombMultiplier) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECPoint(org.bouncycastle.math.ec.ECPoint) X9ECPoint(org.bouncycastle.asn1.x9.X9ECPoint) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters)

Example 2 with DomainParameters

use of com.github.zhenwei.core.asn1.x9.DomainParameters in project opb-sdk-java by bianjieai.

the class BCECUtils method createECPublicKeyParameters.

/**
 * @param xBytes           十六进制形式的公钥x分量,如果是SM2算法,应该是32字节
 * @param yBytes           十六进制形式的公钥y分量,如果是SM2算法,应该是32字节
 * @param curve            EC曲线参数,一般是固定的,如果是SM2算法的可参考{@link SM2#CURVE}
 * @param domainParameters EC Domain参数,一般是固定的,如果是SM2算法的可参考{@link SM2#DOMAIN_PARAMS}
 * @return
 */
public static ECPublicKeyParameters createECPublicKeyParameters(byte[] xBytes, byte[] yBytes, ECCurve curve, ECDomainParameters domainParameters) {
    final byte uncompressedFlag = 0x04;
    int curveLength = getCurveLength(domainParameters);
    xBytes = fixToCurveLengthBytes(curveLength, xBytes);
    yBytes = fixToCurveLengthBytes(curveLength, yBytes);
    byte[] encodedPubKey = new byte[1 + xBytes.length + yBytes.length];
    encodedPubKey[0] = uncompressedFlag;
    System.arraycopy(xBytes, 0, encodedPubKey, 1, xBytes.length);
    System.arraycopy(yBytes, 0, encodedPubKey, 1 + xBytes.length, yBytes.length);
    return new ECPublicKeyParameters(curve.decodePoint(encodedPubKey), domainParameters);
}
Also used : ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters) ECPoint(org.bouncycastle.math.ec.ECPoint) X9ECPoint(org.bouncycastle.asn1.x9.X9ECPoint)

Example 3 with DomainParameters

use of com.github.zhenwei.core.asn1.x9.DomainParameters in project anttu.code.github.io by anTtutu.

the class SM2Utils method decrypt.

/**
 * SM2解密算法 - 私钥是字符串
 * @param privateKey        私钥
 * @param cipherData        密文数据
 * @return
 */
public String decrypt(String privateKey, String cipherData) {
    byte[] cipherDataByte = Hex.decode(cipherData);
    // 获取一条SM2曲线参数
    X9ECParameters sm2ECParameters = GMNamedCurves.getByName("sm2p256v1");
    // 构造domain参数
    ECDomainParameters domainParameters = new ECDomainParameters(sm2ECParameters.getCurve(), sm2ECParameters.getG(), sm2ECParameters.getN());
    BigInteger privateKeyD = new BigInteger(privateKey, 16);
    ECPrivateKeyParameters privateKeyParameters = new ECPrivateKeyParameters(privateKeyD, domainParameters);
    SM2Engine sm2Engine = new SM2Engine();
    sm2Engine.init(false, privateKeyParameters);
    String result = null;
    try {
        byte[] arrayOfBytes = sm2Engine.processBlock(cipherDataByte, 0, cipherDataByte.length);
        result = new String(arrayOfBytes, CHARSET);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : SM2Engine(org.bouncycastle.crypto.engines.SM2Engine) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) BigInteger(java.math.BigInteger)

Example 4 with DomainParameters

use of com.github.zhenwei.core.asn1.x9.DomainParameters in project jmulticard by ctt-gob-es.

the class ECUtil method getDomainParameters.

public static ECDomainParameters getDomainParameters(ProviderConfiguration configuration, X962Parameters params) {
    ECDomainParameters domainParameters;
    if (params.isNamedCurve()) {
        ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
        if (ecP == null) {
            Map extraCurves = configuration.getAdditionalECParameters();
            ecP = (X9ECParameters) extraCurves.get(oid);
        }
        domainParameters = new ECNamedDomainParameters(oid, ecP);
    } else if (params.isImplicitlyCA()) {
        org.bouncycastle.jce.spec.ECParameterSpec iSpec = configuration.getEcImplicitlyCa();
        domainParameters = new ECDomainParameters(iSpec.getCurve(), iSpec.getG(), iSpec.getN(), iSpec.getH(), iSpec.getSeed());
    } else {
        X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
        domainParameters = new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
    }
    return domainParameters;
}
Also used : ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) ECNamedDomainParameters(org.bouncycastle.crypto.params.ECNamedDomainParameters) Map(java.util.Map) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 5 with DomainParameters

use of com.github.zhenwei.core.asn1.x9.DomainParameters in project LinLong-Java by zhenwei1108.

the class ECUtil method getDomainParameters.

public static ECDomainParameters getDomainParameters(ProviderConfiguration configuration, X962Parameters params) {
    ECDomainParameters domainParameters;
    if (params.isNamedCurve()) {
        ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
        if (ecP == null) {
            Map extraCurves = configuration.getAdditionalECParameters();
            ecP = (X9ECParameters) extraCurves.get(oid);
        }
        domainParameters = new ECNamedDomainParameters(oid, ecP);
    } else if (params.isImplicitlyCA()) {
        com.github.zhenwei.provider.jce.spec.ECParameterSpec iSpec = configuration.getEcImplicitlyCa();
        domainParameters = new ECDomainParameters(iSpec.getCurve(), iSpec.getG(), iSpec.getN(), iSpec.getH(), iSpec.getSeed());
    } else {
        X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
        domainParameters = new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
    }
    return domainParameters;
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) Map(java.util.Map) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Aggregations

X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)3 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)2 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)2 DHParameter (com.github.zhenwei.core.asn1.pkcs.DHParameter)2 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 DomainParameters (com.github.zhenwei.core.asn1.x9.DomainParameters)2 ValidationParams (com.github.zhenwei.core.asn1.x9.ValidationParams)2 DHParameters (com.github.zhenwei.core.crypto.params.DHParameters)2 DHValidationParameters (com.github.zhenwei.core.crypto.params.DHValidationParameters)2 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)2 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)2 DHDomainParameterSpec (com.github.zhenwei.provider.jcajce.spec.DHDomainParameterSpec)2 ECParameterSpec (com.github.zhenwei.provider.jce.spec.ECParameterSpec)2 Map (java.util.Map)2 X9ECPoint (org.bouncycastle.asn1.x9.X9ECPoint)2 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)2 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)2 ECPoint (org.bouncycastle.math.ec.ECPoint)2 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)1