gpt4 book ai didi

javascript - 在node js中对数据进行签名并在Java中进行验证

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:54 26 4
gpt4 key购买 nike

我正在使用 crypto 创建 key 对在nodejs中。

var { publicKey, privateKey } = generateKeyPairSync('rsa', {
modulusLength: 1024,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: ''
}
});

生成的公钥是:

"-----开始公钥-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCnwiV6Opd+3TZf9N1z9GVhxtY\nnqWiFRIsqUOR/vXXge8hbhgm5ITKfZtvyO92Lk+5Tl1Qdrb+OfNcKmBHkGUEdB ib\nXCNEXj7SOhQTBH0PnV++8+xJXoutsiJU9Z+sjMCFivbd5wgBizXcY8U9SQfYb+Iv\nNQ8FBL4nV8lMiwAp8QIDAQAB\n--- --结束公钥-----\n"

我正在尝试用 Java 签署消息并验证它。

我遇到的问题是将字符串 key (签名 key )解析为 Java 中的 PublicKey:

public static PublicKey loadPublicKey(String stored) throws GeneralSecurityException, IOException {
byte[] data = Base64.getDecoder().decode((stored.getBytes()));
X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
KeyFactory fact = KeyFactory.getInstance("RSA");
return fact.generatePublic(spec);
}

显示的错误是:

线程“main”java.lang.IllegalArgumentException中出现异常:非法base64字符a

如何验证消息?

提前谢谢

最佳答案

您应该删除输入String的第一行和第二行,因为它们不是base64 DER编码公钥的一部分。并且删除所有换行符 \n。给定您提供的输入字符串,您可以像这样解决它:

 public static void main(String[] args) throws GeneralSecurityException, IOException {
String pemString = "-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCnwiV6Opd+3TZf9N1z9GVhxtY\nnqWiFRIsqUOR/vXXge8hbhgm5ITKfZtvyO92Lk+5Tl1Qdrb+OfNcKmBHkGUEdBib\nXCNEXj7SOhQTBH0PnV++8+xJXoutsiJU9Z+sjMCFivbd5wgBizXcY8U9SQfYb+Iv\nNQ8FBL4nV8lMiwAp8QIDAQAB\n-----END PUBLIC KEY-----\n";
String base64 = toBase64DER(pemString);
PublicKey publicKey = loadPublicKey(base64);
}

public static PublicKey loadPublicKey(String stored) throws GeneralSecurityException {
byte[] data = Base64.getDecoder().decode(stored.getBytes());
X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
KeyFactory fact = KeyFactory.getInstance("RSA");
return fact.generatePublic(spec);
}

public static String toBase64DER(String pem) {
return pem.substring(0, pem.lastIndexOf("\n")) //remove last \n
.substring(pem.indexOf('\n') + 1) //remove first line
.substring(0, pem.lastIndexOf("\n") + 1) //remove last line
.replace("\n", ""); // remove all \n occurences
}

对于为您的方法准备 Base64 输入的更通用方法,您还可以使用类似以下内容的方法:

private static final String BEGIN = "-----BEGIN PUBLIC KEY-----";
private static final String END = "-----END PUBLIC KEY-----";

public static String toBase64DER(String pem) {
return pem.replace("\n", "") //remove all line breaks \n
.replace(BEGIN, "") // remove beginning of PEM
.replace(END, ""); //remove ending of PEM
}

以及签名:

Signature signature = Signature.getInstance("SHA256withRSA"); // place algorithm name here
signature.initVerify(publicKey); //add public key
signature.update("signedMessage".getBytes(StandardCharsets.UTF_8)); //set signed data
boolean verified = signature.verify("signatureBytes".getBytes(StandardCharsets.UTF_8)); //verify signature match

关于javascript - 在node js中对数据进行签名并在Java中进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58375043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com