[SOLVED]Cryptography Problem (BadPaddingException)
Hello there,
I´ve got a prblem to encrypt and decrypt a String.
I always get a BadPaddingException.
Where is the failure I probably made?
Thanks for your help.
Code:
public class KryptTest2 {
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};
int count=20;
//....main and some stuff
// Schlüssel erzeugen (create key from password)
private SecretKey makeKeyFromPassword(String passwort) {
PBEKeySpec pbeKeySpec = new PBEKeySpec(passwort.toCharArray());
try{
SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
return pbeKey;
}
catch(NoSuchAlgorithmException e){
return null;}
catch(InvalidKeySpecException e1){
return null;
}
}
// Verschluesselung und Entschluesselung (encrypt and decrypt)
private String verschlüsseln(String nachricht, SecretKey secretKey) {
try{
PBEParameterSpec pbeParamSpec= new PBEParameterSpec(salt, count);
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParamSpec);
byte[] ciphertext= pbeCipher.doFinal(nachricht.getBytes());
return new String(ciphertext);
}
catch (Exception e){
return null;
}
}
private String entschlüsseln(String nachricht, SecretKey secretKey) {
try{
PBEParameterSpec pbeParamSpec= new PBEParameterSpec(salt, count);
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParamSpec);
byte[] ciphertext= pbeCipher.doFinal(nachricht.getBytes());
return new String(ciphertext);
}
catch (Exception e){
return null;
}
}