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!

Input length (with padding) not multiple of 8 bytes

843810Dec 11 2001 — edited Dec 12 2001
Hello!

I receive the next error while using my Bean:
javax.crypto.IllegalBlockSizeException: Input length (with padding) not multiple of 8 bytes

I didn't find the reason of error in this forum.
When I in JSP use getEncryptedString(String to encode) - it always works and return to the browser encoded string. But when I use getDecryptedString(String to decode), it works only if this encrypted string is not too long. So, if it's long (or maybee becourse of other reasons I don't know), I receive the Error on the top.

What's wrong in my Bean?

So, my code:

import java.security.*;
import java.io.*;
import javax.crypto.*;
import sun.misc.*;
import java.util.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class Encrypt{
//--------------------------------------------------------------------------------------------
public SecretKey getKeyFromFile() throws Exception{
SecretKey kk = null;
byte[] teb={0x7F,0x7E,0x59,0x5E,0x14,0x4C,0x6D,0x61};
SecretKeyFactory kf = SecretKeyFactory.getInstance ("DES");
DESKeySpec ks = new DESKeySpec(teb);
SecretKey kd = kf.generateSecret(ks);
return kd;
}
//--------------------------------------------------------------------------------------------------
public String getEncryptedString(String input)throws Exception{
//Security.addProvider( new com.sun.crypto.provider.SunJCE() );
Key key = null;
key = getKeyFromFile();
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[]inputBytes = input.getBytes("UTF8");
//Encode
byte[] outputBytes = cipher.doFinal(inputBytes);
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(outputBytes);
return base64;
}
//--------------------------------------------------------------------------------------------------
public String getDecryptedString(String input)throws Exception{
Key key=null;
try
{
key = getKeyFromFile();
//ObjectInputStream in=new ObjectInputStream(new FileInputStream("des.key"));
//key=(Key)in.readObject();
//in.close();
} catch(FileNotFoundException fnfe)
{
System.out.print("Error");
}
Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] raw = decoder.decodeBuffer(input);
byte[] stringBytes = cipher.doFinal(raw);
String result = new String(stringBytes,"UTF8");
return result;
}
}

-----------------------------------------------------------------------
JSP file:
<jsp:useBean id="enc" scope="request" class="Encrypt"/>

Key: <%= enc.getKeyFromFile(); %><BR>

Encrypted:<%= enc.getEncryptedString("java.lang.IllegalStateExceptionfj keofiefhieof dwdw dfwfef e f"); %><BR>

Decrypted:<%= enc.getDecryptedString("pA1ezk2oTtNWRlfiY/q/4SLDLvZWr4lpdWK2XhswXsxyux5Wt+76xXdoy77R/NMUQ5aB2mMBEKpv 2NcRHcFmVA=="); %>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 9 2002
Added on Dec 11 2001
2 comments
1,553 views