why bad padding exception???
843811Apr 17 2009 — edited Apr 17 2009hi guys
here i am trying to decrypt the already encrypted string by one different encrpytion algo.
in the example i have encrypted the string by des and then i tried to decrypt it using blowfish but it gives as the output null instead of a random string...because of a bad padding exception(check line 86). help to remove the exception.
if we try to decrypt the des encrypted string by des again it give n padding exception. why with blowfish???
// CIPHER / GENERATORS
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.KeyGenerator;
// KEY SPECIFICATIONS
import java.security.spec.KeySpec;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEParameterSpec;
// EXCEPTIONS
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
public class StringEncrypter {
Cipher ecipher;
Cipher dcipher;
StringEncrypter(SecretKey key, String algorithm) {
try {
ecipher = Cipher.getInstance(algorithm);
dcipher = Cipher.getInstance(algorithm);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (NoSuchPaddingException e) {
System.out.println("EXCEPTION: NoSuchPaddingException");
} catch (NoSuchAlgorithmException e) {
System.out.println("EXCEPTION: NoSuchAlgorithmException");
} catch (InvalidKeyException e) {
System.out.println("EXCEPTION: InvalidKeyException");
}
}
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
return null;
}
public String decrypt(String str) {
try {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
} catch (BadPaddingException e) {
System.out.println("BAd padding excception");
} catch (IllegalBlockSizeException e) {
System.out.println("IllegalBlockSizeException");
} catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException");
} catch (IOException e) {
System.out.println("IOException");
}
return null;
}
public static void testUsingSecretKey() {
try {
String secretString = "code cant be decrypted!";
SecretKey desKey = KeyGenerator.getInstance("DES").generateKey();
SecretKey blowfishKey = KeyGenerator.getInstance("Blowfish").generateKey();
StringEncrypter desEncrypter = new StringEncrypter(desKey, desKey.getAlgorithm());
StringEncrypter blowfishEncrypter = new StringEncrypter(blowfishKey, blowfishKey.getAlgorithm());
String desEncrypted = desEncrypter.encrypt(secretString);
String desDecrypted = desEncrypter.decrypt(desEncrypted);
String blowfishDecrypted = blowfishEncrypter.decrypt(desEncrypted);
System.out.println(desKey.getAlgorithm() + " Encryption algorithm");
System.out.println(" Original String : " + secretString);
System.out.println(" Encrypted String : " + desEncrypted);
System.out.println(" Decrypted String : " + desDecrypted);
System.out.println();
System.out.println(blowfishKey.getAlgorithm() + " Encryption algorithm");
System.out.println(" Original String : " + desEncrypted);
System.out.println(" Decrypted String : " + blowfishDecrypted);
System.out.println();
} catch (NoSuchAlgorithmException e) {
}
}
public static void main(String[] args) {
testUsingSecretKey();
}
}