【问题标题】:ECC ASN1 signature verification failureECC ASN1签名验证失败
【发布时间】:2016-06-03 11:46:06
【问题描述】:

我有 48 字节的 ECC secp192r1 签名,它在其他环境中工作:

byte[] signature = new byte[]{(byte)0x08, (byte)0x33, (byte)0x6B, (byte)0x27, (byte)0xBC, (byte)0x29, (byte)0x64, (byte)0x36, (byte)0x70, (byte)0x08, (byte)0x97, (byte)0x4F, (byte)0xA8, (byte)0xD7, (byte)0x1F, (byte)0x4D, (byte)0x05, (byte)0xF5, (byte)0xB2, (byte)0x0F, (byte)0x15, (byte)0x5D, (byte)0x68, (byte)0x61, (byte)0xB3, (byte)0x2B, (byte)0x0E, (byte)0xA9, (byte)0xFB, (byte)0x37, (byte)0xF1, (byte)0xD4, (byte)0x70, (byte)0xEA, (byte)0x2B, (byte)0xCA, (byte)0x53, (byte)0x9D, (byte)0x11, (byte)0xE7, (byte)0x26, (byte)0x37, (byte)0x92, (byte)0x73, (byte)0xDE, (byte)0x95, (byte)0x6C, (byte)0x4A};

它被编码为 ASN1 格式 ~54 字节长度:

    byte[] sig1 = Arrays.copyOfRange(signature, 0, 24);
    byte[] sig2 = Arrays.copyOfRange(signature, 24, 48);

    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(new BigInteger(sig1)));
    v.add(new ASN1Integer(new BigInteger(sig2)));

    byte[] javaSig = new DERSequence(v).getEncoded();

但是,当我尝试验证数据时,它失败了。将签名编码为 ASN1 是否正确?

从ASN1到48字节格式的签名解码也存在同样的问题:

    ASN1InputStream input = new ASN1InputStream(signed);
    ASN1Primitive item = input.readObject();

    ASN1Sequence s = (ASN1Sequence)item;
    BigInteger[] items = new BigInteger[2];

    items[0] = ((ASN1Integer)s.getObjectAt(0)).getValue();
    items[1] = ((ASN1Integer)s.getObjectAt(1)).getValue();

    byte[] itBytes0 = items[0].toByteArray();
    byte[] itBytes1 = items[1].toByteArray();

【问题讨论】:

    标签: java cryptography digital-signature bouncycastle elliptic-curve


    【解决方案1】:

    byte[] 创建BigInteger 时的一个常见问题是在Java 上默认情况下所有数字都被解释为singed:

    BigInteger(byte[] val)

    将包含 BigInteger 的二进制补码表示的字节数组转换为 BigInteger。

    我不知道您使用的算法是否需要有符号或无符号数,但是值得一提的是明确强制 Java 将字节数组解释为无符号数:

    v.add(new ASN1Integer(new BigInteger(1, sig1)));
    v.add(new ASN1Integer(new BigInteger(1, sig2)));
    

    【讨论】:

    • 有助于创建PublicKey但不影响签名验证。
    【解决方案2】:

    从 ECDSA 切换到 NONEwithECDSA:

    Signature sig = Signature.getInstance("NONEwithECDSA", "BC");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 2013-10-28
      • 2020-11-02
      • 2016-01-13
      • 2011-07-09
      相关资源
      最近更新 更多