4

I'm making a small cryptor class for an app that a friend and i are making, all it has to do (currently) is decrypt and encrypt strings of data. but i keep getting the following exception when trying to decrypt: avax.crypto.IllegalBlockSizeException: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length i think it has something to do with the way Im decoding/encoding the base64 strings, but I've tried different ways and I always get the same problem. here is the code:

public class Cryptor {
private static Cryptor _instance = null;
private static Object mutex = new Object();
String symKeyString;
SecretKey symKey;
String keyString;
Cipher cipher;
String plainText;
String cipherText;
byte[] plainByte;
SecretKey originalKey;
SecretKey key;
KeyGenerator keyGen;
byte[] cipherByte;

private Cryptor() {
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static Cryptor getInstance() {
    if (_instance == null) {
        synchronized (mutex) {
            if (_instance == null)
                _instance = new Cryptor();
        }
    }
    return _instance;

}

public String generateSymmetricKey() {

    try {
        keyGen = KeyGenerator.getInstance("AES");
        symKey = keyGen.generateKey();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    symKeyString = convertKeyToString(symKey);
    return symKeyString;
}


public String symEncrypt(String keyStr, String plainTextInput) {

    key = convertStringToKey(keyStr);

    try {
        cipher.init(Cipher.ENCRYPT_MODE, key);


        cipherByte = cipher.doFinal(plainTextInput.getBytes());

    } catch (Exception e) {
        e.printStackTrace();
    }
    cipherText = Base64.encodeToString(cipherByte, Base64.DEFAULT);
    return cipherText;
}


public String symDecrypt(String keyStr, String cipherText) {
    key = convertStringToKey(keyStr);

    try {
        cipher.init(Cipher.DECRYPT_MODE, key);

        plainByte = cipher.doFinal(cipherText.getBytes()); //here is where the exception is thrown!

    } catch (Exception e) {
        e.printStackTrace();
    }

    plainText = Base64.encodeToString(plainByte, Base64.DEFAULT);
    return plainText;
}


private String convertKeyToString(SecretKey key) {


    keyString = Base64.encodeToString(key.getEncoded(), Base64.DEFAULT);

    return keyString;
}

private SecretKey convertStringToKey(String keyStr) {
    byte[] decodedKey = Base64.decode(keyStr, Base64.DEFAULT);
    originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
    return originalKey;
}
}
duggu
  • 37,851
  • 12
  • 116
  • 113
Louis
  • 61
  • 1
  • 2
  • 1
    Seeing that you base64-encode output from the cipher, you could start with base64-decoding data before you pass it into `Cipher.doFinal()` – Oleg Estekhin Jan 30 '15 at 09:01
  • @OlegEstekhin This seems to be the answer. If it is the answer, then why is this question off topic? – Maarten Bodewes Jan 31 '15 at 14:33
  • This question is in the re-open queue, but I believe it should remain off-topic because questions on Stack Overflow should put more effort into providing SSCCE samples; this is more of a debug-my-code feel (though I'm glad somebody could help the OP) – Dave Jan 31 '15 at 16:34

0 Answers0