0

I decrypt a file but it is not displaying the decrypted string. It is also throwing an javax.crypto.IllegalBlockSizeException.

Here is my code:

File f=new File("C:/Users/User/Desktop/Test.txt");
int ch;

StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try {
    fin = new FileInputStream(f);
    while ((ch = fin.read()) != -1)
        strContent.append((char) ch);
    fin.close();
} 
catch (Exception e) {
    System.out.println(e);
}

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);

SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original =cipher.doFinal(strContent.toString().getBytes());

String originalString = new String(original);
JOptionPane.showMessageDialog(null,originalString.toString());
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • 2
    Can you then show the error? Without it, we cannot help. – skiwi Jan 08 '14 at 12:10
  • When you set a breakpoint on the `String originalString =...` line what does `original` contain? – Anders R. Bystrup Jan 08 '14 at 12:15
  • Exception in thread "main" javax.crypto.IllegalBlockSizeException – newlearner Jan 08 '14 at 12:26
  • You're ignoring character encodings in your code, for example you read bytes from the file and directly cast them to `char` and you're using `String.getBytes()` which will use the default character encoding, which might or might not do what you want. – Jesper Jan 08 '14 at 12:28
  • sorry i can't follow ur explaination.. – newlearner Jan 08 '14 at 12:31
  • 1
    If the file contains a piece of text that has been encrypted, it can't be read as text anymore. Encryption produces bytes, which don't represent valid characters, whatever the encoding you use. It's purely binary, and must be read as bytes, using an InputStream. Read http://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html – JB Nizet Jan 08 '14 at 12:32
  • I would guess that you haven't (successfully) decrypted the string. (Try decrypting the BYTES, not the STRING.) – Hot Licks Jan 08 '14 at 12:43

1 Answers1

0

An IllegalBlockSizeException occurs when the input data is not a multiple of the block-size (16 bytes in the case of AES).

This problem has been caused by your method of reading the decrypted data. As explained in the comments by Jesper and JB Nizet, encrypted data is unreadable as text. Trying to read it as a string (regardless of character set) isn't going to work.

So... ensure you are writing your file to disk as a byte stream and read it in the same manner. This should ensure you have exactly the right data (which will be the right length).

I'd recommend something like Apache commons-lang FileUtils.writeByteArrayToFile and FileUtils.readFileToByteArray, or write your own with the Java IO libraries.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • @newlearner That is not what this site is about. You need to fix your own code. I've given you enough information in my answer to get you started. – Duncan Jones Jan 08 '14 at 12:59