收藏
回答

小程序安全键盘sm2解密结果为什么有空字符?

小程序文档是这么说的,解析后得出时间戳密码hash字符串

可为什么得出的结果是这样的?

是代码哪里有问题吗?求大佬解答。

public static String decryptHex(String hex) {
    SM2 sm2 = SmUtil.sm2(priKey, pubKey);
    byte[] hex1 = Hex.decode(hex);
    // 解码ANS.1
    byte[] ans1 = changeAsn1ToC1C3C2(hex1); 
    // 解密
    byte[] decrypt = sm2.decrypt(ans1, KeyType.PrivateKey);
    boolean equals = Arrays.equals(decrypt, last);
    last = decrypt;
    String s1 = new String(decrypt, StandardCharsets.UTF_8);
    String s2 = new String(decrypt, StandardCharsets.ISO_8859_1);
    String s3 = new String(decrypt, StandardCharsets.US_ASCII);
    String s4 = new String(decrypt, StandardCharsets.UTF_16);
    String s5 = new String(decrypt, StandardCharsets.UTF_16BE);
    String s6 = new String(decrypt, StandardCharsets.UTF_16LE);
    String[] split = s1.split("\0");
    return "";
} 

public static byte[] changeAsn1ToC1C3C2(byte[] asn1) {
    ASN1InputStream aIn = new ASN1InputStream(asn1);
    ASN1Sequence seq = null;
    try {
        seq = (ASN1Sequence) aIn.readObject();
        BigInteger x = ASN1Integer.getInstance(seq.getObjectAt(0)).getValue();
        BigInteger y = ASN1Integer.getInstance(seq.getObjectAt(1)).getValue();
        byte[] c3 = ASN1OctetString.getInstance(seq.getObjectAt(2)).getOctets();
        byte[] c2 = ASN1OctetString.getInstance(seq.getObjectAt(3)).getOctets();

        ECPoint c1Point =  GMNamedCurves.getByName("sm2p256v1").getCurve().createPoint(x, y);
        byte[] c1 = c1Point.getEncoded(false);
        return ArrayUtil.addAll(c1, c3, c2);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }
    return null;
}
回答关注问题邀请回答
收藏
登录 后发表内容