gpt4 book ai didi

java - 验证 RSA 私钥和公钥是否匹配

转载 作者:行者123 更新时间:2023-12-01 12:26:59 60 4
gpt4 key购买 nike

我已经加载了两个 key ,我想在使用其中一个 key 签署某些内容后验证它们,但我遇到了困难。我最后得到“已验证:错误”,没有任何错误。有人可以指出错误吗?

package fliesigning;

import static fliesigning.FlieSigning.verifySig;
import java.io.*;
import java.nio.ByteBuffer;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.math.BigInteger;
import java.security.Provider;
import java.security.Security;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;

public class Signing {
private static final String BEGIN_RSA_PRIVATE_KEY = "<PRIVATE KEY>";
private static final String BEGIN_RSA_PUBLIC_KEY = "<PUBLIC KEY>";

public static void main(String[] args) throws Exception {
// Remove the first and last lines
String privKeyPEM = BEGIN_RSA_PRIVATE_KEY.replace("-----BEGIN RSA PRIVATE KEY-----\n", "");
privKeyPEM = privKeyPEM.replace("-----END RSA PRIVATE KEY-----", "");
System.out.println(privKeyPEM);

String publicKeyPEM = BEGIN_RSA_PUBLIC_KEY.replace("-----BEGIN PUBLIC KEY-----\n", "");
publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", "");
System.out.println(publicKeyPEM);

// Base64 decode the data
Base64 b64 = new Base64();
byte [] encoded = b64.decode(privKeyPEM);
byte [] encoded_pub = b64.decode(publicKeyPEM);

// PKCS8 decode the encoded RSA private key
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encoded);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privKey = kf.generatePrivate(privateKeySpec);

X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encoded_pub);
KeyFactory pk = KeyFactory.getInstance("RSA");
PublicKey publicKey = pk.generatePublic(publicKeySpec);

// Display the results
System.out.println(privKey);
String file = "qwerty";
byte[] fileBytes = file.getBytes();
byte[] digitalSignature = signData(fileBytes, privKey);
System.out.println("SIGNATURE MADE");
boolean verified;
verified = verifySig(fileBytes, publicKey, digitalSignature);
System.out.println("verified: " + verified) ;
}

public static byte[] signData(byte[] data, PrivateKey key) throws Exception {
Signature signer = Signature.getInstance("SHA256withRSA");
signer.initSign(key);
signer.update(data);
return (signer.sign());
}

public static boolean verifySig(byte[] data, PublicKey key, byte[] sig) throws Exception {
Signature signer = Signature.getInstance("SHA256withRSA");
signer.initVerify(key);
signer.update(data);
return (signer.verify(sig));
}
}

最佳答案

您的代码看起来工作正常,一定是您的 key 实际上不匹配。我使用以下方法创建了一些测试 key :

  • openssl genrsa -out priv.pem
    (创建基本 RSA 私钥)

  • openssl rsa -in priv.pem -pubout -out pub.pem
    (提取公钥)

  • openssl pkcs8 -in priv.pem -out pk8.pem -topk8 -nocrypt
    (将私钥转换为未加密的 PKCS#8 格式)

这给了我两个要测试的文件:pk8.pempub.pem。我稍微更改了您的代码,以便开始和结束标记为 -----BEGIN PRIVATE KEY----------END PRIVATE KEY----- .

验证成功。

关于java - 验证 RSA 私钥和公钥是否匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26256781/

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