兩個字符串是私鑰和公鑰是:字符串轉換爲私人和公共密鑰(RSA)
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........." ;
我已經閱讀了其他問題,並試圖解決它們,但沒有工作....我有一個公鑰和私鑰都作爲字符串..我需要將它們轉換爲'鑰匙',但我不斷得到java.security.spec.InvalidKeySpecException:java.security.InvalidKeyException ..我generatePublic和generatePrivate函數..也是關鍵是隻是爲了測試,因此,如果他人知道其他人可以...
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;
}
除去換行'+「/ N 「' – Jens
仍然是相同的問題..我得到java.security.spec.InvalidKeySpecException:java.security.InvalidKeyException:我將公鑰更改爲:」MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDH + wPrKYG1KVlzQUVtBghR8n9dzcShSZo0 + 3KgyVdOea7Ei7vQ1U4wRn1zlI5rSqH DzFitblmqnB2anzVvdQxLQ3UqEBKBfMihnLgCSW8Xf7MCH + DSGHNvBg2xSNhcfEmnbLPLnbuz4ySn1UB0lH2eqxy50zstxhTY0binD9Y + rwIDAQAB」 –
我有我自己試了一下用公鑰和它工作 – Jens