Encryption/decryption of password.
843810Sep 21 2004 — edited Nov 3 2006Hi,
I am having problem with password encryption/decryption. I can successfully encrypt but when I do the decryption I am getting few exceptions like IllegalBlockSizeException, BadPaddingException. Below is the code which I have written and the exception I am getting when I am decrypting the encrypted password.
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.*;
import java.io.*;
public class PwdEncryption
{
Cipher ecipher;
Cipher dcipher;
SecretKey key = null;
KeyGenerator desKeyGen;
protected static String KEYGEN_STR = "";
public PwdEncryption()
{
try
{
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecretKey desSecretKey = keyGen.generateKey();
byte[] bytes = desSecretKey.getEncoded();
DataInputStream dis = null;
try
{
FileInputStream fis = new FileInputStream("C:/DESKey.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
int count = 0;
String result = null;
StringBuffer buf = new StringBuffer();
while((result = dis.readLine()) != null)
{
buf.append(result);
count++;
}
KEYGEN_STR = buf.toString();
}
catch(FileNotFoundException ex)
{ }
finally
{
if(dis != null)
{
try
{
dis.close();
}
catch (IOException ioe)
{ }
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
private Key getKey()
{
try
{
byte[] bytes = getbytes(KEYGEN_STR);
DESKeySpec pass = new DESKeySpec(bytes);
SecretKeyFactory sKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey sKey = sKeyFactory.generateSecret(pass);
return sKey;
}
catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}
private byte[] getbytes(String str)
{
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
StringTokenizer sTokenizer = new StringTokenizer(str, "-", false);
while(sTokenizer.hasMoreTokens())
{
try
{
byteOutputStream.write(sTokenizer.nextToken().getBytes());
}
catch(IOException ex)
{
}
}
byteOutputStream.toByteArray());
return byteOutputStream.toByteArray();
}
public String encrypt(String sourceStr)
{
try
{
// Get secret key
Key key = getKey();
ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
ecipher.init(Cipher.ENCRYPT_MODE, key);
byte[] enc = ecipher.doFinal((new String(sourceStr)).getBytes("UTF-8"));
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
}
catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}
public String decrypt(String sourceStr)
{
try
{
// Get secret key
Key key = getKey();
dcipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
dcipher.init(Cipher.DECRYPT_MODE, key);
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(sourceStr);
//Decrypt data in a single step
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF-8");
}
catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}
}
javax.crypto.IllegalBlockSizeException: Input length (with padding) not multiple of 8 bytes
at com.sun.crypto.provider.SunJCE_h.a(DashoA6275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at javax.crypto.Cipher.doFinal(DashoA6275)
Can anyone please help me with the code so that it would work. It is very urgent.
Thanks