无法使用unicode字符验证签名

kknvjkwl  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(166)

我试图通过提供一个字符串以及由不同代码创建的签名来验证签名。在验证时,如果字符串不包含unicode字符,则可以正常工作,但如果字符串包含unicode字符,则会失败。我需要一些帮助来解决这个问题。
代码段1-创建签名

public static String sign(String plainText, PrivateKey privateKey) throws Exception {
        Signature privateSignature = Signature.getInstance(Constants.SHA256WITH_RSA);
        privateSignature.initSign(privateKey);
        privateSignature.update(plainText.getBytes(Constants.UTF_8));
        System.out.println("Payload before Encoding::"+plainText);
        byte[] signature = privateSignature.sign();

        return Base64.getEncoder().encodeToString(signature);
    }

代码段2-验证程序

const crypto = require('crypto');
const fs = require('fs');

const getSignatureVerifyResult = () => {

  const signatureText = `cnPiCPt0Wh8c/QLz38CJlk3pNdTHP9TVVrBlEwtkpmRZ8Csmis/uTdu57wi+Ldsm/IExzJa+lue61JoKhE1rYpcmD2FzXkNJlwskkOL1m9LAfu9nkuTgUjoBmAFaOwmHJWwyQyB3UhW53HFhHj26oE1iRNLBMKRW/9anltzmIMy2KNmvWHrDfKwDLTiEBBjEgbv6byB68u16svm3wcu9J1yMQ6qJkGyOP5+KczTnyzEUg76C9zUAoAyNzIjTUKNxKpJWUweUzngU7vvLpsavupPapQjgqkLjSaUiFPQdZR2Do7HggeVPiccSZG148qLv10ndvzm3nR5KZh8/BPnENg==`
  const input = `{"data":{"message":{"message_parts":[{"text":{"content":"\u003e"}}]}}}`

  const publicKeyStr = fs.readFileSync('public_key.txt', { encoding : 'utf8' })

  const verifier = crypto.createVerify('RSA-SHA256')
  verifier.update(input)

  const publicKeyBuf = Buffer.from(publicKeyStr, 'utf-8')
  const signatureBuf = Buffer.from(signatureText, 'base64')
  const result = verifier.verify(publicKeyBuf, signatureBuf)

  return result

}

console.log(getSignatureVerifyResult());

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题