2016-08-03 109 views
0

我有這個加密代碼,這沒有問題。我可以解密用另一種語言加密的文本,但我現在需要用java解密它。使用AES/ECB/PKCS5Padding進行加密時不能解密 - 輸入長度必須是16的倍數

private static final String AES = "AES"; 
    private static final String CBC_BLOCK = "CBC"; 
    private static final String ECB_BLOCK = "ECB"; 
    private static final String PADDING = "PKCS5Padding"; 
    private static final String AES_CBC_PCKS5_CIPHER_CONFIG = AES + "/" + CBC_BLOCK + "/" + PADDING; 
    private static final String AES_ECB_PCKS5_CIPHER_CONFIG = AES + "/" + ECB_BLOCK + "/" + PADDING; 

public static String encryptInAesEcbPkcs5Padding(String salt, String message) { 
     String encryptedMessage = ""; 
     SecretKeySpec key = null; 
     try { 
      if (message != null && !message.equals("")) { 
       key = new SecretKeySpec(salt.getBytes(StandardCharsets.UTF_8), AES); 
       Cipher cipher = Cipher.getInstance(AES_ECB_PCKS5_CIPHER_CONFIG); 
       cipher.init(Cipher.ENCRYPT_MODE, key); 
       encryptedMessage = convertMessageToBase64(cipher.doFinal(message.getBytes(StandardCharsets.UTF_8))); 
      } 
     } catch (NoSuchAlgorithmException e) { 
      LOGGER.error(LogPreFix.ERROR + "No such algorithm [" + AES + "]", e); 
     } catch (NoSuchPaddingException e) { 
      LOGGER.error(LogPreFix.ERROR + "No such padding for algorithm [" + AES + "]", e); 
     } catch (IllegalBlockSizeException e) { 
      LOGGER.error(LogPreFix.ERROR + "Invalid block size for [" + AES + "/" + ECB_BLOCK + "]", e); 
     } catch (BadPaddingException e) { 
      LOGGER.error(LogPreFix.ERROR + "Invalid padding [" + PADDING + "]", e); 
     } catch (InvalidKeyException e) { 
      LOGGER.error("Invalid key [" + key + "]", e); 
     } 
     return encryptedMessage; 
    } 

試圖用此代碼解密。我使用的是完全相同的鹽作爲加密和傳遞的字符串中的加密創建的「消息」

public static String decrypt(String message, String salt) throws InvalidAlgorithmParameterException { 
     SecretKeySpec key = null; 
     String string = null; 
     try { 
      if (message != null && !message.equals("")) { 
       key = new SecretKeySpec(salt.getBytes(StandardCharsets.UTF_8), AES); 
       Cipher cipher = Cipher.getInstance(AES_ECB_PCKS5_CIPHER_CONFIG); 
       cipher.init(Cipher.DECRYPT_MODE, key); 
       byte[] decrypted = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8)); 
       string = new String(decrypted); 
      } 
     } catch (NoSuchAlgorithmException e) { 
      LOGGER.error(LogPreFix.ERROR + "No such algorithm [" + AES + "]", e); 
     } catch (NoSuchPaddingException e) { 
      LOGGER.error(LogPreFix.ERROR + "No such padding for algorithm [" + AES + "]", e); 
     } catch (IllegalBlockSizeException e) { 
      LOGGER.error(LogPreFix.ERROR + "Invalid block size for [" + AES + "/" + ECB_BLOCK + "]", e); 
     } catch (BadPaddingException e) { 
      LOGGER.error(LogPreFix.ERROR + "Invalid padding [" + PADDING + "]", e); 
     } catch (InvalidKeyException e) { 
      LOGGER.error("Invalid key [" + key + "]", e); 
     } 
     return string; 
    } 

,但我得到這個錯誤,我究竟做錯了什麼?似乎它應該工作。我試着用「=」填充加密的文本,直到它被16整除,但是返回一個錯誤的填充錯誤。

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher 
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750) 
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676) 
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313) 
    at javax.crypto.Cipher.doFinal(Cipher.java:2087) 
+1

這可能是有幫助的:http://stackoverflow.com/a/5760584/1416629 – pratnala

+2

爲什麼不在解密之前執行base64解碼? – Robert

+0

^這也是。做base64解碼並取回 – pratnala

回答

1

首先進行Base64解碼以反轉原始加密過程中執行的Base64編碼。

相關問題