0

I encountered a problem decoding text files.

Encryption works, but when the program is restarted, decryption throws an exception. The same similar Exception was thrown with apache.common.Base64. Maybe someone knows how to solve this?

private String decrypt(String encrypted) throws Exception {
    Cipher cipher = getCipher(Cipher.DECRYPT_MODE);
    //byte[] plainBytes = cipher.doFinal(Base64.decodeBase64(encrypted));

    /*this line throw an exception*/                                      
    byte[] plainBytes = cipher.doFinal(Base64.getDecoder().decode(encrypted));

    return new String(plainBytes);
}

private Cipher getCipher(int cipherMode) throws Exception {
    String encryptionAlgorithm = "AES";
    SecretKeySpec keySpecification = new SecretKeySpec(
            encryptionKey.getBytes("UTF-8"), encryptionAlgorithm);
    Cipher cipher = Cipher.getInstance(encryptionAlgorithm);
    cipher.init(cipherMode, keySpecification);

    return cipher;
}




Exception in thread "main" java.lang.NullPointerException
at java.util.Base64$Decoder.decode(Base64.java:549)
at edmaker.EdMaker.decrypt(EdMaker.java:148)

Listening on javadebug Not able to submit breakpoint MethodBreakpoint [fsiteratetest.FsIterateTest$1].visitFile '(Ljava/nio/file/Path;LBasicFileAttributes;)Ljava/nio/file/FileVisitResult;', reason: Breakpoint belongs to disabled source root 'C:\Users\win7p\Documents\NetBeansProjects\FsIterateTest\src'. See Window/Debugging/Sources. User program running User program stopped Debugger stopped on uncompilable source code. User program running User program finished

loadP
  • 404
  • 1
  • 4
  • 15
  • 1
    Debugging and breakpoints do wonders. Check what exactly is null. Could `encrypted` (most likely since that `decodeBase64` function already throws the nullpointer exception), or `getCipher()` returned something null therefore `cipher`is null. – Maximilian Gerhardt Feb 06 '16 at 22:32
  • Obviously `encrypted` is null. – user207421 Feb 06 '16 at 22:59
  • Somehow I know what is a NullPointerException, but I can't find the exceptional error in the code above. Maybe I should post the whole code ? Okay I should also take a look at debugging if I can. – loadP Feb 06 '16 at 23:37
  • Yes it is that encrypted is null. However I can't find the problem in BufferedReader when the String lines are parsed to the method. Thanks – loadP Feb 07 '16 at 16:33

0 Answers0