Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why do i get an "IllegalBlockSizeException"?

843810Sep 22 2002 — edited Jun 27 2003
I have a piece of code which encrypts a string and stores it into a txt file. But when i come to decrypt the file i get the following error

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length
(with padding) not multiple of 8 bytes
at com.sun.crypto.provider.DESCipher.a(DashoA6275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at javax.crypto.Cipher.doFinal(DashoA6275)
at javamailerprogram.DES.decrypt(DES.java:110)
at javamailerprogram.DES.main(DES.java:120)

This is my code

package javamailerprogram;

import java.security.*;
import java.security.*;
import javax.crypto.*;
import java.io.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;
import java.security.interfaces.*;

public class DES {

static SecretKey newKey;
static String ENCRYPTION_TYPE = "DES";
static int KEY_SIZE = 56;
static SecretKeySpec skeySpec;
static SecretKeySpec spec;
static Cipher fileCipher;


public DES(){
Provider sunJce = new com.sun.crypto.provider.SunJCE();
Security.addProvider(sunJce);

try {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
KeyGenerator keyGen = KeyGenerator.getInstance(ENCRYPTION_TYPE);
keyGen.init(KEY_SIZE);

ObjectInputStream in = new ObjectInputStream(new FileInputStream("c:/des.key"));

SecretKey bfSKey = (SecretKey)in.readObject();
byte[] raw = bfSKey.getEncoded();
skeySpec = new SecretKeySpec(raw, ENCRYPTION_TYPE);
} catch (Exception e) {
e.printStackTrace();
}
}

public void encrypt(String fileName, String fileNameTo) throws Exception {
byte[] encryptedText;

BufferedReader d
= new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
StringBuffer line = new StringBuffer("");
String lines = "";

while((lines = d.readLine()) !=null){
line.append(lines);
}

fileCipher = Cipher.getInstance(ENCRYPTION_TYPE);
fileCipher.init(Cipher.ENCRYPT_MODE, skeySpec);

encryptedText = fileCipher.doFinal(line.toString().getBytes());

FileOutputStream out = new FileOutputStream(fileNameTo);
out.write(encryptedText);
System.out.println("Done");
}

public String encryptMessage(String textToEncrypt, String fileNameTo) throws Exception {
byte[] encryptedText;

fileCipher = Cipher.getInstance(ENCRYPTION_TYPE);
fileCipher.init(Cipher.ENCRYPT_MODE, skeySpec);

encryptedText = fileCipher.doFinal(textToEncrypt.getBytes());

FileOutputStream out = new FileOutputStream(fileNameTo);
out.write(encryptedText);
System.out.println("Done");
return fileNameTo;
}

public String encryptMessage(String textToEncrypt) throws Exception {
byte[] encryptedText;

fileCipher = Cipher.getInstance(ENCRYPTION_TYPE);
fileCipher.init(Cipher.ENCRYPT_MODE, skeySpec);

encryptedText = fileCipher.doFinal(textToEncrypt.getBytes());
System.out.println("Done");
return new String(encryptedText);

}


public String decrypt(String fileName) throws Exception {
byte[] decryptedText;
fileCipher = Cipher.getInstance(ENCRYPTION_TYPE);

BufferedReader d
= new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));

StringBuffer line = new StringBuffer("");
String lines = "";

while((lines = d.readLine()) !=null){
line.append(lines);
}
System.out.println(line.length());

byte[] theDecryptedText = line.toString().getBytes();

fileCipher.init(Cipher.DECRYPT_MODE, skeySpec);
decryptedText = fileCipher.doFinal(theDecryptedText);
return new String(decryptedText);
}

public static void main(String[] args) throws Exception {
new DES();

//new DES().encrypt("c:\\encr.txt", "C:\\Encr2.txt");
new DES().encryptMessage("dsgfkdsfdsfudsagifugsadfuigsalidguflasgdfulagsdgfldsagfldsu", "C:\\Encr3.txt");

System.out.println(new DES().decrypt("C:\\Encr3.txt"));
} // end main
} // end SourceViewer2
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 25 2003
Added on Sep 22 2002
14 comments
253 views