I have been trying to encrypt with the Cipher class for a while now and I'm still getting issues. I'm having problems with the padding and with the byte array size. I receive this exception "Input length must be multiple of 8 when decrypting with padded cipher" This is the code I wrote:
public CipherClass() {
try {
cipher = Cipher.getInstance(CIPHER_ALGO);
secKey = SecretKeyFactory.getInstance(CIPHER_ALGO).generateSecret(new DESedeKeySpec(new BigInteger(KEY, 16).toByteArray()));
}
catch(GeneralSecurityException e){
e.printStackTrace();
}
}
private String encrypt(String text){
try {
cipher.init(Cipher.ENCRYPT_MODE, secKey);
Base64 encoder = new Base64();
return new String(encoder.encode(cipher.doFinal(text.getBytes())));
}
catch(GeneralSecurityException e) {
e.printStackTrace();
}
return "";
}
private String decrypt(String text) {
try {
cipher.init(Cipher.DECRYPT_MODE, secKey);
Base64 decoder = new Base64();
return new String(decoder.decode(cipher.doFinal(text.getBytes())));
}
catch (Exception e) {
e.printStackTrace();
}
return "";
}
I also tried using Hex instead of Base64 and I'm receiving this exception: "Given final block not properly padded"
please help me out, to find error. thanks in advance.