- 这个安全键盘的SM2国密,后台解密怎么一直解不出来
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/safe-password.html 先解码16进制然后就sm2解密直接报错。但是先 ans.1之后再sm2解密就能解出来。但是为什么解不干净。就是有部分乱码数据,很奇怪。 jdk8
2024-09-20 - 小程序安全键盘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; }
2024-09-11