2017-08-09 75 views
1

我得到了一個帶有OAEP SHA-256填充的RSA公鑰和RSA私鑰。我試圖簡單地加密一個隨機字符串,然後解密它來聲明結果等於原來的。BadPaddingException RSA加密然後在java中解密

這是公鑰我一直在考慮:

-----BEGIN RSA PUBLIC KEY----- 
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq8tiBtDmkeS/ruY3rrkq 
dz6Lc6XWFRbI/GjPtIokrtpM+Ujyv6wX8TBqY8e03gzh+eE7VUyEVPapDnceAqgJ 
ZQah2h+N5AEQKqNDM4/1to1V0F+m1ISR7CIUU8dvU+bPk67DU5VkEtLxf+mW8/es 
hy0u6oSB04WCDPNnh+9GDF7tN7lOzBH1FxKPWIb5Gqg6GoXS+KgvQhYEk0TajIBk 
+mefzTv1D4HJlhFYybgY+/p3k4P2kM5HnbEtoVpz/PL9+94YVp5RBHTQHmk/3SAX 
RkPP34IjY6LZqTJGWQGxc64Qci54ZJsq4wSTXvfdHgUWz7eX+v1jA+GLjFExNcYZ 
nQIDAQAB 
-----END RSA PUBLIC KEY----- 

這是私鑰我一直在考慮:

-----BEGIN PRIVATE KEY----- 
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDAUQiHdHuMBCnF 
P/7RQoRWrp62dGWQLDLP9l+3KUMlSPheN2R5jHDZ/WEGJGF2WYKxaQvv5XCocdnt 
uxVZTNM1PVyduokJaJzrSIj+jGWDd4hTWvVoS3vhds8u6W0jD3M4ayrF6c7NYuHc 
tE5YLLIgK1DTn+ZZCr3VGbScISDJ/WPx3+1YTNQDvm+T7MfjhetJqqzWIsunMUiw 
nQOQvkdjCVWpk+L10SaMrezixtIS5wfpOTjGIZ0w5VunvtsPxg1TkuvGURa9S3rS 
7D3uhDPY+V8UDMpBPso3j7TGD5axlBJcpavWeF8qde3sWztmqUFQ3JDypZWGSAe2 
d8e4mY65AgMBAAECggEAal8nwYxbHZnb5L892U7aVfuly7Nbzb+0pzRVwsBu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pfL0yY7WAONvhfFB0Dpsz52gGDCYRvJekGRz6jOlKDuXJ+Mm1AX4BaufMETI5 
QseZxtVPIn+BXm0A1x8w/DifmE1JqQZmPCQPOh3eLx5nSn9LKGIbMgd17mfH3HhU 
8WX2mzWjmRA3C/CzdGfCKSk= 
-----END PRIVATE KEY----- 

我讀從PEM文件「crypt_pkcs8此鍵。 pem「,但是將公鑰恢復爲來自gRPC調用的字節數組。

這裏是我的Java代碼:

public class Encryption{ 
public static void main(String args){ 
    ByteString publicKey = client.getPublicKey(nonce).getRSAEncryptionKey(); 

    String strKeyPEM = ""; 
    BufferedReader br = new BufferedReader(new FileReader("crypt_pkcs8.pem")); 
    String line; 
    while ((line = br.readLine()) != null) { 
     strKeyPEM += line+"\n"; 
    } 
    br.close(); 
    byte[] pt; 
    try { 
     pt = "h".getBytes("UTF-8"); 
     byte[] ct =encrypt(publicKey, pt); 
     byte[] response= decrypt(strKeyPEM, ct); 

     assertEquals(pt, response); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
public static byte[] encrypt(ByteString rawKey ,byte[] message){ 
    String strippedKey=stripKey(rawKey.toStringUtf8()); 

    byte[] keyBytes = Base64.getDecoder().decode(strippedKey); 
    System.out.println(keyBytes.length); 

    Cipher cipher_RSA; 
    try { 
     cipher_RSA = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"); 
     KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
     X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); 
     PublicKey pk = keyFactory.generatePublic(spec); 
     System.out.println(pk.getAlgorithm()+" format : "+pk.getFormat()); 

     cipher_RSA.init(Cipher.ENCRYPT_MODE, pk); 
     return cipher_RSA.doFinal(message); 
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | 
      BadPaddingException | InvalidKeyException | InvalidKeySpecException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return null; 
    } 

} 

public static byte[] decrypt(String rawKey ,byte[] message){ 
    String strippedKey=stripPrivateKey(rawKey); 

    byte[] keyBytes = Base64.getDecoder().decode(strippedKey); 
    System.out.println(keyBytes.length); 

    Cipher cipher_RSA; 
    try { 
     cipher_RSA = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"); 
     KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
     PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); 
     PrivateKey pk = keyFactory.generatePrivate(spec); 
     System.out.println(pk.getAlgorithm()+" format : "+pk.getFormat()); 

     cipher_RSA.init(Cipher.DECRYPT_MODE, pk); 
     return cipher_RSA.doFinal(message); 
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | 
      BadPaddingException | InvalidKeyException | InvalidKeySpecException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return null; 
    } 
} 
public static String stripKey(String key){ 
    key = key.replace("-----BEGIN RSA PUBLIC KEY-----\n", ""); 
    key = key.replace("-----END RSA PUBLIC KEY-----", ""); 
    key = key.replace("\n", ""); 
    return key; 
} 

public static String stripPrivateKey(String key){ 
    key = key.replace("-----BEGIN PRIVATE KEY-----", ""); 
    key = key.replace("-----END PRIVATE KEY-----", ""); 
    key = key.replace("\n", ""); 
    return key; 
} 


} 

我找不到任何明顯的錯誤與我的actaul代碼,所以我相信這是與鑰匙不匹配的事情。

我還注意到公鑰和私鑰的頁眉/頁腳是不同的。從網上查看這是因爲公鑰是PKCS#8格式。

我需要將公鑰更改爲pkcs#8格式嗎?如果是的話,最簡單的方法是什麼?

我也被告知我應該能夠從上面給出的私鑰中提取公鑰(以pkcs8格式)。這可以輕鬆完成嗎?

回答

2

如果你能得到一個有效的公鑰,那麼這個密鑰不是PKCS#8格式,它將以Java所期望的X.509(SubjectPublicKeyInfo)格式。 但是,你的公鑰和私鑰確實不是匹配。您可以使用回答here從私鑰文件創建正確的公鑰文件。


Java不直接包含代碼來檢索從RSAPrivateKey公用指數沒有CRT參數。但是,如果您只需要一個Java解決方案,則可以將PrivateKey甚至進一步轉換爲RSAPrivateCrtKey,其中包含getPublicExponent方法來檢索公共指數。然後,您可以使用RSA KeyFactory使用RSAPublicKeySpec生成公鑰。


您還可以解析PEM文件以使用例如Java語言檢索Java中的公鑰。 Bouncy Castle API。但是如果你這樣做的話,期望學習曲線陡峭。

+0

不要忘記從詹姆斯upvote [這個答案](https://stackoverflow.com/a/24768779/589259)! –