【问题标题】:Create ASN.1 from two big integers从两个大整数创建 ASN.1
【发布时间】:2017-08-02 21:02:12
【问题描述】:

我有一个使用 HSM 的 Java 程序,它与本机 API 一起为我提供了 ECDSA 签名的 R 和 S 值,这只是两个大整数。我需要获取这些整数并创建 ASN.1 编码。知道我该怎么做吗?我确实启动并运行了 BouncyCastle,但我不熟悉可用的选项。

【问题讨论】:

    标签: java bouncycastle asn.1 ecdsa


    【解决方案1】:

    一个小例子来说明:

    import org.bouncycastle.asn1.ASN1Integer;
    import org.bouncycastle.asn1.DERSequence;
    
    import javax.xml.bind.DatatypeConverter;
    import java.math.BigInteger;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            BigInteger r = new BigInteger("29128391823901823918293108120938102381912839182390182391829310812093810238199");
            BigInteger s = new BigInteger("38663726871681756650018917824777578348866372687168175665001891782477757834811");
    
            ASN1Integer asn1R = new ASN1Integer(r);
            ASN1Integer asn1S = new ASN1Integer(s);
    
            DERSequence seq = new DERSequence(new ASN1Integer[]{asn1R, asn1S});
            byte[] encoded = seq.getEncoded();
            System.out.println(DatatypeConverter.printHexBinary(encoded));
        }
    }
    

    【讨论】:

    • 如果您以某种方式获取字节而不是整数,请确保正确设置符号位,例如通过使用new BigInteger(1, [byte array]),其中 1 表示应采用无符号编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 2017-03-15
    • 2011-04-07
    相关资源
    最近更新 更多