Hallo zusammen,
Ich habe mir eine Java-Klasse zum ver- und entschlüsseln geschrieben. Das Verschlüsseln klappt einwandfrei, allerdings habe ich beim Entschlüsseln Probleme. Hier erhalte ich eine IllegalBlockSizeException.
Mein Code:
und die Exception:
Hoffe jemand kann mir hier helfen
Gruß,
Dominik
Ich habe mir eine Java-Klasse zum ver- und entschlüsseln geschrieben. Das Verschlüsseln klappt einwandfrei, allerdings habe ich beim Entschlüsseln Probleme. Hier erhalte ich eine IllegalBlockSizeException.
Mein Code:
Java:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String password = "WMservice";
encryptPassword(password);
}
private static void encryptPassword(String password) {
Key key = loadKey();
if (key == null) {
storeKeyInFile();
key = loadKey();
}
String encryptString = "";
try {
Cipher cipher = Cipher.getInstance("AES");
byte[] encrypted = password.getBytes("UTF8");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptBytes = cipher.doFinal(encrypted);
encryptString = new String(encryptBytes);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(encryptString);
decryptPassword(encryptString);
}
private static void decryptPassword(String encryptString) {
try {
Key key = loadKey();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedBytes = cipher.doFinal(encryptString.getBytes());
String decryptString = new String(decryptedBytes);
System.out.println(decryptString);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
}
private static Key loadKey() {
Key key = null;
KeyStore ks = null;
try {
ks = KeyStore.getInstance("JCEKS");
InputStream readStream = new FileInputStream("C:\\key");
ks.load(readStream, "test".toCharArray());
key = ks.getKey("key", "test".toCharArray());
readStream.close();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
}
return key;
}
private static void storeKeyInFile() {
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
Key secretKey = keyGen.generateKey();
KeyStore ks = KeyStore.getInstance("JCEKS");
ks.load(null, "test".toCharArray());
ks.setKeyEntry("key", secretKey, "test".toCharArray(), null);
OutputStream writeStream = new FileOutputStream("C:\\key");
ks.store(writeStream, "test".toCharArray());
writeStream.close();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
und die Exception:
Code:
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at Test.decryptPassword(Test.java:57)
at Test.encryptPassword(Test.java:49)
at Test.main(Test.java:29)
Hoffe jemand kann mir hier helfen
Gruß,
Dominik