gpt4 book ai didi

java - 将字符串转换为私钥和公钥 (RSA)

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

作为私钥和公钥的两个字符串是:

    static String Public =          
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDH+wPrKYG1KVlzQUVtBghR8n9d" + "/n" +
"zcShSZo0+3KgyVdOea7Ei7vQ1U4wRn1zlI5rSqHDzFitblmqnB2anzVvdQxLQ3Uq" + "/n" +
"EBKBfMihnLgCSW8Xf7MCH+DSGHNvBg2xSNhcfEmnbLPLnbuz4ySn1UB0lH2eqxy5" + "/n"+
"0zstxhTY0binD9Y+rwIDAQAB"+ "/n";
static String Private =
"MIICxjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIr5NQ/LYPG/UCAggA" +"/n"+
"MBQGCCqGSIb3DQMHBAiLh89iGSkmoASCAoBCpAo9/IzDE3yGhvWr9RgozE7revOo" +"/n"+
"V2OXmU+d0+WYAAx2GYVaUCbFVrmgiVmrbiTgLUMXAGIpvxQ2rzyIvRHW/RN3Gcky" +"/n"+
"qR/AwBatzixqrnoS4aD1/Ovjr4hwde4XHYbPEilZZuVAJFiznhy73qm/So4XghSY........." ;

我已经阅读了其他问题并尝试了他们的解决方案,但没有任何效果......我有一个公钥和私钥都作为字符串......我需要将它们转换为“Key”,但我不断收到 java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException ..at generatePublic 和 generatePrivate 函数.. key 也仅用于测试,因此,如果其他人知道它们就可以了...
      public static Key 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);

}


public static Key loadPrivateKey(String key64) throws GeneralSecurityException, IOException {
byte[] clear = Base64.getDecoder().decode(key64.getBytes());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey priv = fact.generatePrivate(keySpec);
Arrays.fill(clear, (byte) 0);
return priv;

}

最佳答案

删除字符串声明中的行 Feeds。这不是关键的一部分:

static String Public =          
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDH+wPrKYG1KVlzQUVtBghR8n9d" +
"zcShSZo0+3KgyVdOea7Ei7vQ1U4wRn1zlI5rSqHDzFitblmqnB2anzVvdQxLQ3Uq" +
"EBKBfMihnLgCSW8Xf7MCH+DSGHNvBg2xSNhcfEmnbLPLnbuz4ySn1UB0lH2eqxy5" +
"0zstxhTY0binD9Y+rwIDAQAB";
static String Private =
"MIICxjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIr5NQ/LYPG/UCAggA" +
"MBQGCCqGSIb3DQMHBAiLh89iGSkmoASCAoBCpAo9/IzDE3yGhvWr9RgozE7revOo" +
"V2OXmU+d0+WYAAx2GYVaUCbFVrmgiVmrbiTgLUMXAGIpvxQ2rzyIvRHW/RN3Gcky" +
"qR/AwBatzixqrnoS4aD1/Ovjr4hwde4XHYbPEilZZuVAJFiznhy73qm/So4XghSY........." ;

我已经用下面的代码试过了:
  public static void main(String[] args) throws GeneralSecurityException, IOException {
System.out.println(loadPublicKey(Public));

}

public static Key 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);

}

输出是:
Sun RSA public key, 1024 bits
modulus: 140431102839105138202102866401190456107365606715815288536913018579006717438700259314092212104831553250527764925385527697411165705192297577022746989837839401358787285684108054389360182109284048524426941021357601686464156659759470495649944686235380003772357268264646549523784880655065600797504478771675703688879
public exponent: 65537

关于java - 将字符串转换为私钥和公钥 (RSA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43372052/

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