3

I am using this method to decrypt my incoming messages:

private static String decrypt(String key, String initVector, String dataToDecrypt) {
    try {
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

        String safeString = dataToDecrypt.replace('-', '+').replace('_', '/');
        byte[] decodedString = Base64.decodeBase64(safeString);

        byte[] original = cipher.doFinal(decodedString);

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

However, my Android app crashes, showing the following exception:

java.lang.NoSuchMethodError: No static method decodeBase64(Ljava/lang/String;)[B in class Lorg/apache/commons/codec/binary/Base64; or its super classes (declaration of 'org.apache.commons.codec.binary.Base64' appears in /system/framework/ext.jar)

accordingly, the method decodeBase64 takes base64string, but I pass string. Here comes my question:

How to convert String to base64string ?!

Please note that I am trying to DECODE not ENCODE. Almost all the solutions provided are for the encoding part which is not my worry.

P.S.: I am developing an Android-app on Android-Studio

McLan
  • 2,552
  • 9
  • 51
  • 85
  • 1
    Possible duplicate of [Apache Commons Codec with Android: could not find method](http://stackoverflow.com/questions/2047706/apache-commons-codec-with-android-could-not-find-method) – Kiskae Mar 17 '17 at 11:20
  • @Kiskae : with my all respect, this is definitely not a duplicate. I am asking a very different question. He ia asking : "Android: could not find method, how to solve?" .. Meanwhile, I am asking about a very precise matter which is "how to convert string to base64string" .. if the source of the problem isthe same, that does not mean the question is duplicated. I have to say, this is unfair (the downvotes, the close request and the duplicate question charge. But, by the end of the day I just want to learn and get better. Thanks ) – McLan Mar 17 '17 at 11:44
  • Possible duplicate of [Android - decodeBase64 crashes App](http://stackoverflow.com/questions/27276539/android-decodebase64-crashes-app) – Jean-Baptiste Yunès Mar 17 '17 at 11:55

4 Answers4

6

try this:

Base64.encodeToString(mStringToEncode.getBytes(), Base64.NO_WRAP)

it exist many encoding mode use autocompletion to see more Base64.NO_WRAP, Base64.CRLF, etc...

you need to import package:

import android.util.Base64;
Toukea Tatsi
  • 189
  • 1
  • 5
  • I am sorry but this didn't work.Beside, I am trying to decode not encode. – McLan Mar 17 '17 at 11:31
  • This helped a lot , it seems I was mistakenly putting the wrong import: `org.apache.commons.codec.binary.Base64` .. now it works – McLan Mar 17 '17 at 13:55
2
public static String toBase64(String value){
    if (value == null)
        value = "";
    return Base64.encodeToString(value.trim().getBytes(), android.util.Base64.DEFAULT);
}
Peter R
  • 384
  • 1
  • 11
1

Have you check this answer

        // decode data from base 64
        private static byte[] decodeBase64(String dataToDecode)
        {
            byte[] dataDecoded = Base64.decode(dataToDecode, Base64.DEFAULT);
            return dataDecoded;
        }

       //enconde data in base 64
        private static byte[] encodeBase64(byte[] dataToEncode)
        {
            byte[] dataEncoded = Base64.encode(dataToEncode, Base64.DEFAULT);
            return dataEncoded;
        }
Community
  • 1
  • 1
ziLk
  • 3,120
  • 21
  • 45
  • Yes, but it is not working .. at the moment I am trying to understand why ! .. meanwhile I posted the original problem I am having – McLan Mar 17 '17 at 11:20
1

Try this code

private static String encryptNew(String key, String initVector, String dataToEncrypt) throws Exception{

        byte[] plainTextbytes = dataToEncrypt.getBytes("UTF-8");
        byte[] keyBytes = key.getBytes("UTF-8");
        byte[] IvkeyBytes = initVector.getBytes("UTF-8");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(IvkeyBytes);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        plainTextbytes = cipher.doFinal(plainTextbytes);
        return Base64.encodeToString(plainTextbytes, Base64.DEFAULT);
    }

    private static String decrypt(String key, String initVector, String dataToDecrypt) {
            try {

                byte[] cipheredBytes = Base64.decode(dataToDecrypt, Base64.DEFAULT);
                byte[] keyBytes = key.getBytes("UTF-8");
                byte[] IvkeyBytes = initVector.getBytes("UTF-8");

                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
                SecretKeySpec secretKeySpecy = new SecretKeySpec(keyBytes, "AES");
                IvParameterSpec ivParameterSpec = new IvParameterSpec(IvkeyBytes);
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);
                cipheredBytes = cipher.doFinal(cipheredBytes);

                return new String(cipheredBytes,"UTF-8");
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            return null;
        }
Akash Jagtap
  • 404
  • 2
  • 8