2016-08-03 5 views
1

Ich habe diesen Verschlüsselungscode, der kein Problem funktioniert. Ich kann den Text entschlüsseln, den es in einer anderen Sprache verschlüsselt, aber ich muss es nun in Java entschlüsseln.BadPadding Exception mit der gleichen Chiffre-Instanz

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; 
    } 

Versuchen, mit diesem Code zu entschlüsseln. Ich bin mit dem exakt gleichen Salz wie die Verschlüsselung und Weitergabe in der Kette der Verschlüssler als „Nachricht“

public static String decrypt(String message, String salt) throws InvalidAlgorithmParameterException { 
      SecretKeySpec key = null; 
      String string = null; 
      try { 
       if (message != null && !message.equals("")) { 
    String decoded = convertBase64ToMessage(message.getBytes(StandardCharsets.UTF_8)); 
        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(decoded.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; 
     } 

Aber ich bekomme diese Fehlermeldung erzeugt, da ich das gleiche Cipher Beispiel verwenden, wie ich tat, wenn es der Verschlüsselung Ich bin mir nicht sicher, warum ich die Nachricht nicht entschlüsseln kann.

javax.crypto.BadPaddingException: Given final block not properly padded 
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811) 
    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) 

Antwort

3

convertBase64ToMessage sollte kein String zurückkehren, sondern ein byte[], weil Chiffriertexte nicht durch eine druckbare Zeichenfolge (mit hoher Wahrscheinlichkeit) dargestellt werden.

+0

das hat es geschafft, danke! – wondergoat77

+0

Schön, dass du das gelöst hast! – pratnala