org.bouncycastle.crypto.params.ParametersWithID Java Examples

The following examples show how to use org.bouncycastle.crypto.params.ParametersWithID. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SM2KeyExchangeUtil.java    From gmhelper with Apache License 2.0 6 votes vote down vote up
/**
 * @param initiator         true表示发起方,false表示响应方
 * @param keyBits           生成的密钥长度
 * @param confirmationTag   确认信息,如果是响应方可以为null;如果是发起方则应为响应方的s1
 * @param selfStaticPriv    己方固定私钥
 * @param selfEphemeralPriv 己方临时私钥
 * @param selfId            己方ID
 * @param otherStaticPub    对方固定公钥
 * @param otherEphemeralPub 对方临时公钥
 * @param otherId           对方ID
 * @return
 */
public static ExchangeResult calculateKeyWithConfirmation(boolean initiator, int keyBits, byte[] confirmationTag,
    ECPrivateKeyParameters selfStaticPriv, ECPrivateKeyParameters selfEphemeralPriv, byte[] selfId,
    ECPublicKeyParameters otherStaticPub, ECPublicKeyParameters otherEphemeralPub, byte[] otherId) {
    SM2KeyExchange exch = new SM2KeyExchange();
    exch.init(new ParametersWithID(
        new SM2KeyExchangePrivateParameters(initiator, selfStaticPriv, selfEphemeralPriv),
        selfId));
    byte[][] result = exch.calculateKeyWithConfirmation(
        keyBits,
        confirmationTag,
        new ParametersWithID(new SM2KeyExchangePublicParameters(otherStaticPub, otherEphemeralPub), otherId));
    ExchangeResult confirmResult = new ExchangeResult();
    confirmResult.setKey(result[0]);
    if (initiator) {
        confirmResult.setS2(result[1]);
    } else {
        confirmResult.setS1(result[1]);
        confirmResult.setS2(result[2]);
    }
    return confirmResult;
}
 
Example #2
Source File: Sm2KeyExchangeUtil.java    From littleca with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param initiator true表示发起方,false表示响应方
 * @param keyBits 生成的密钥长度
 * @param confirmationTag 确认信息,如果是响应方可以为null;如果是发起方则应为响应方的s1
 * @param selfStaticPriv 己方固定私钥
 * @param selfEphemeralPriv 己方临时私钥
 * @param selfId 己方ID
 * @param otherStaticPub 对方固定公钥
 * @param otherEphemeralPub 对方临时公钥
 * @param otherId 对方ID
 * @return
 */
public static ExchangeResult calculateKeyWithConfirmation(boolean initiator, int keyBits, byte[] confirmationTag,
    ECPrivateKeyParameters selfStaticPriv, ECPrivateKeyParameters selfEphemeralPriv, byte[] selfId,
    ECPublicKeyParameters otherStaticPub, ECPublicKeyParameters otherEphemeralPub, byte[] otherId) {
    SM2KeyExchange exch = new SM2KeyExchange();
    exch.init(new ParametersWithID(
        new SM2KeyExchangePrivateParameters(initiator, selfStaticPriv, selfEphemeralPriv),
        selfId));
    byte[][] result = exch.calculateKeyWithConfirmation(
        keyBits,
        confirmationTag,
        new ParametersWithID(new SM2KeyExchangePublicParameters(otherStaticPub, otherEphemeralPub), otherId));
    ExchangeResult confirmResult = new ExchangeResult();
    confirmResult.setKey(result[0]);
    if (initiator) {
        confirmResult.setS2(result[1]);
    } else {
        confirmResult.setS1(result[1]);
        confirmResult.setS2(result[2]);
    }
    return confirmResult;
}
 
Example #3
Source File: SM2KeyExchangeUtil.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
/**
 * @param initiator         true表示发起方,false表示响应方
 * @param keyBits           生成的密钥长度
 * @param selfStaticPriv    己方固定私钥
 * @param selfEphemeralPriv 己方临时私钥
 * @param selfId            己方ID
 * @param otherStaticPub    对方固定公钥
 * @param otherEphemeralPub 对方临时公钥
 * @param otherId           对方ID
 * @return 返回协商出的密钥,但是这个密钥是没有经过确认的
 */
public static byte[] calculateKey(boolean initiator, int keyBits,
    ECPrivateKeyParameters selfStaticPriv, ECPrivateKeyParameters selfEphemeralPriv, byte[] selfId,
    ECPublicKeyParameters otherStaticPub, ECPublicKeyParameters otherEphemeralPub, byte[] otherId) {
    SM2KeyExchange exch = new SM2KeyExchange();
    exch.init(new ParametersWithID(
        new SM2KeyExchangePrivateParameters(initiator, selfStaticPriv, selfEphemeralPriv),
        selfId));
    return exch.calculateKey(
        keyBits,
        new ParametersWithID(new SM2KeyExchangePublicParameters(otherStaticPub, otherEphemeralPub), otherId));
}
 
Example #4
Source File: SM2Sign.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * The new sm2 signature algorithm with better performance
 *
 * @param message
 * @param ecKeyPair
 * @return
 */
public static Sign.SignatureData sign2(byte[] message, ECKeyPair ecKeyPair) {

    SM2Signer sm2Signer = new SM2Signer();

    ECPrivateKeyParameters eCPrivateKeyParameters =
            new ECPrivateKeyParameters(ecKeyPair.getPrivateKey(), eCDomainParameters);

    sm2Signer.initWithCache(
            true,
            new ParametersWithID(new ParametersWithRandom(eCPrivateKeyParameters), identValue));

    org.bouncycastle.crypto.digests.SM3Digest sm3Digest =
            new org.bouncycastle.crypto.digests.SM3Digest();

    byte[] md = new byte[sm3Digest.getDigestSize()];
    sm3Digest.update(message, 0, message.length);
    sm3Digest.doFinal(md, 0);

    sm2Signer.update(md, 0, md.length);

    byte[] r = null;
    byte[] s = null;
    byte[] pub = null;

    try {
        BigInteger[] bigIntegers = sm2Signer.generateSignature2();

        pub = Numeric.toBytesPadded(ecKeyPair.getPublicKey(), 64);
        r = SM2Algorithm.getEncoded(bigIntegers[0]);
        s = SM2Algorithm.getEncoded(bigIntegers[1]);
    } catch (CryptoException e) {
        throw new RuntimeException(e);
    }

    return new Sign.SignatureData((byte) 0, r, s, pub);
}
 
Example #5
Source File: Sm2KeyExchangeUtil.java    From littleca with Apache License 2.0 3 votes vote down vote up
/**
 *
 * @param initiator true表示发起方,false表示响应方
 * @param keyBits 生成的密钥长度
 * @param selfStaticPriv 己方固定私钥
 * @param selfEphemeralPriv 己方临时私钥
 * @param selfId 己方ID
 * @param otherStaticPub 对方固定公钥
 * @param otherEphemeralPub 对方临时公钥
 * @param otherId 对方ID
 * @return 返回协商出的密钥,但是这个密钥是没有经过确认的
 */
public static byte[] calculateKey(boolean initiator, int keyBits,
    ECPrivateKeyParameters selfStaticPriv, ECPrivateKeyParameters selfEphemeralPriv, byte[] selfId,
    ECPublicKeyParameters otherStaticPub, ECPublicKeyParameters otherEphemeralPub, byte[] otherId) {
    SM2KeyExchange exch = new SM2KeyExchange();
    exch.init(new ParametersWithID(
        new SM2KeyExchangePrivateParameters(initiator, selfStaticPriv, selfEphemeralPriv),
        selfId));
    return exch.calculateKey(
        keyBits,
        new ParametersWithID(new SM2KeyExchangePublicParameters(otherStaticPub, otherEphemeralPub), otherId));
}