2013-03-15 30 views
0

我與的Java解密

javax.crypto.Cipher 

當我寫的代碼,該行的一個問題

Cipher cipher; 
    byte[] bytes = null; 

    try 
    { 
     cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.DECRYPT_MODE, generateAESKey128b(key)); 
     bytes = cipher.doFinal(input.getBytes("UTF-8")); 
    } 
    catch (NoSuchAlgorithmException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (NoSuchPaddingException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (InvalidKeyException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (UnsupportedEncodingException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IllegalBlockSizeException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (BadPaddingException e) 
    { 
     e.printStackTrace(); 
    } 

控制檯給我這個錯誤

javax.crypto.IllegalBlockSizeException 
Input length must be multiple of 16 when  
decrypting with padded cipher 
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) 
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) 
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..) 
at javax.crypto.Cipher.doFinal(DashoA13*..) 
at it.unitn.se.gym.backend.utils.Security.AES128Decode(Security.java:109) 
at it.unitn.se.gym.backend.utils.Security.decode_AES128_Base64(Security.java:96) 
at it.unitn.se.gym.backend.WebService.main(WebService.java:42) 
Exception in thread "main" java.lang.NullPointerException 
at it.unitn.se.gym.backend.utils.Security.decode_AES128_Base64(Security.java:97) 
at it.unitn.se.gym.backend.WebService.main(WebService.java:42) 

第2行代碼是正確的,但是當我將類型爲byte []的屬性「text」傳遞給doFinal函數時,它給我提供了錯誤。

有人能告訴我爲什麼嗎?

解決:

好了,問題就解決了

byte[] encrypted = UniversalBase64Encoder.decode(input); 
Cipher cipher = Cipher.getInstance("AES"); 
cipher.init(Cipher.DECRYPT_MODE, generateAESKey128b(key)); 
byte[] originalBytes = cipher.doFinal(encrypted); 

這是我寫的

+1

該例外說明您正在解密,但代碼表示您正在加密。向我們展示您的真實代碼。 – 2013-03-15 12:53:01

+0

[This](http://stackoverflow.com/a/1205272/2071828)可能會有所幫助 – 2013-03-15 12:53:47

+0

這個http://stackoverflow.com/questions/3954611/aes-encript-and-decript-problem-with-apache- base64問題和答案將幫助你。使用Base64庫 – 2013-03-15 12:54:20

回答

0

正確的代碼的問題是,你試圖解密沒有加密的字符串,並且這樣做違反了解密算法的假設(其輸入大小總是16的倍數)。

以下是加密並解密字符串的代碼塊。請注意,打印加密字符串時,即使輸入字符串不是,也是16字節。加密算法在輸入字符串加密之前填充16個字節的倍數。該16字節長的加密字符串現在是用於解密的有效輸入。

這個假設(即加密的結果是一個均勻的大小)是相當標準的。它不僅使解密/加密算法更容易編寫,而且還可以防止攻擊者知道您加密的東西的長度。

byte[] keyBytes = new byte[16]; 
keyBytes[0] = 1; 
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); 
String input = "hello"; 
Cipher cipher; 
byte[] bytes = null; 
cipher = Cipher.getInstance("AES"); 
cipher.init(Cipher.ENCRYPT_MODE, key); 
bytes = cipher.doFinal(input.getBytes("UTF-8")); 

System.out.println("Encoded: "+Arrays.toString(bytes)); 

cipher.init(Cipher.DECRYPT_MODE, key); 
byte[] decoded = cipher.doFinal(bytes); 

System.out.println("Decoded: "+new String(decoded, "UTF-8")); 
+1

因此,如果我有一個加密字符串(從某人傳遞),並且我有共享密鑰(加密和解密密鑰),那麼我如何解密該字符串? – 2013-03-19 15:18:33

+0

@DavidMartinelli就像我在最後3行中做的一樣。如果您從某人告訴您加密的字符串中獲得該錯誤,則以下情況之一爲真(1)實際上未加密(2)使用除您正在使用的算法之外的算法對其進行加密(3)您在嘗試解密之前已經對加密的字符串進行了破壞 – 2013-03-19 15:31:28

+0

好的,問題已解決。 OP編輯!非常感謝!!! – 2013-03-20 08:02:36