11

I trying implement algorithm AES 128 in Android but it doesn't work, the problem is import javax.xml.bind.DatatypeConverter;

DatatypeConverter.parseHexBinary(key) and DatatypeConverter.printBase64Binary(finalData)

Does an alternative exist?

My method:

private static final String ALGORIT = "AES";

public static String encryptHackro(String plaintext, String key)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, IOException, DecoderException {


    byte[] raw = DatatypeConverter.parseHexBinary(key);

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance(ALGORITMO);
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    byte[] cipherText = cipher.doFinal(plaintext.getBytes(""));
    byte[] iv = cipher.getIV();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(iv);
    outputStream.write(cipherText);

    byte[] finalData = outputStream.toByteArray();

    String encodedFinalData = DatatypeConverter.printBase64Binary(finalData);

    return encodedFinalData;

}

I see others answers, but I can't implement a solution.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
David Hackro
  • 3,652
  • 6
  • 41
  • 61

2 Answers2

15

Solution

I solved my problem using

compile 'commons-codec:commons-codec:1.3'

and I use android.util.Base64 for Android

incompatible / replacement

DatatypeConverter.parseHexBinary 
org.apache.commons.codec.binary.Hex.decodeHex(key.toCharArray());




DatatypeConverter.printBase64Binary(finalData);
android.util.Base64.encodeToString(finalData, 16) 



DatatypeConverter.parseBase64Binary(encodedInitialData);
org.apache.commons.codec.binary.Hex.decodeHex(key.toCharArray());
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
David Hackro
  • 3,652
  • 6
  • 41
  • 61
  • 1
    Commons-codec 1.3 is not part of the Android API. What happens if Google changes the version of commons-codec in future versions of Android? It's just coincidence it works as version 1.3 just happens to match the version in Android's root class loader.. The case I'm trying to solve is when the base64 decode is used in a pure Java sub-project that is included into an Android App. It is very annoying of Google this.. – Tim P Jan 05 '17 at 12:37
  • 2
    This is not a reliable solution – Ege Kuzubasioglu Dec 22 '17 at 11:08
  • I solved using implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.1' – Amol Dadas Dec 24 '19 at 10:38
  • Why not use `org.apache.commons.codec.binary.Base64` ? – Simon Forsberg Jan 30 '20 at 19:58
6

In case you are looking to use DatatypeConverter like me in android please add this to build.gradle file:

implementation 'javax.xml.bind:jaxb-api:2.3.1'

This will add DatatypeConverter.

Reins
  • 1,109
  • 1
  • 17
  • 35
Vasudevan V
  • 61
  • 1
  • 1
  • 3
    Note that you must supply your own implementation (such as Xerces); Android does not ship with a default implementation. To add 'xerces' in android project use this dependency: implementation("xerces:xercesImpl:2.8.0") Source: mvnrepository.com/artifact/xerces/xercesImpl/2.8.0 – Piyush Kumar Mar 13 '22 at 15:07