Question

I have Java code that works. But in Android, it doesn't work. Actually I have to encrypt a string in java send it via https to android phone and decrypt it there. i am using apache commons codec for base64. Any advice why I get a BadPaddingException? does the crypto.cipher differ for java and android

public class Cryptography {
private static final String CRYPTOGRAPHY_ALGO_DES = "DES";

private static Cipher cipher = null;
private static DESKeySpec keySpec = null;
private static SecretKeyFactory keyFactory = null;

public static String encrypt(String inputString, String commonKey)
        throws InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException {

    String encryptedValue = "";
    SecretKey key = getSecretKey(commonKey);

    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] inputBytes = inputString.getBytes();
    byte[] outputBytes = cipher.doFinal(inputBytes);

    encryptedValue =  new String(Base64.encodeBase64(outputBytes));
    return encryptedValue;
}

public static String decrypt(String encryptedString, String commonKey)
    throws InvalidKeyException, IllegalBlockSizeException,
    BadPaddingException, IOException {

String decryptedValue = "";
encryptedString = encryptedString.replace(' ', '+');

SecretKey key = getSecretKey(commonKey);

cipher.init(Cipher.DECRYPT_MODE, key); 

byte[] decodeBytes=Base64.decodeBase64(encryptedString.getBytes());

cipher.update(decodeBytes);
byte[] recoveredBytes = cipher.doFinal( );
System.out.println(" recovered bytes\t" + recoveredBytes);
decryptedValue = new String(recoveredBytes);
System.out.println(" decryptedvalue **strong text**\t" + decryptedValue);
return decryptedValue;

}

private static SecretKey getSecretKey(String secretPassword) {
SecretKey key = null;
try {
    cipher = Cipher.getInstance(CRYPTOGRAPHY_ALGO_DES);
    keySpec = new DESKeySpec(secretPassword.getBytes("UTF8"));
    keyFactory = SecretKeyFactory.getInstance(CRYPTOGRAPHY_ALGO_DES);
    key = keyFactory.generateSecret(keySpec);
} catch (Exception e) {
    e.printStackTrace();
    System.out.println("Error in generating the secret Key");
}
return key;
}

}

when i execute all these function in java there's no problem. but when i run the encryption in java and decryption in android, the decryption gives exception

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top