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

3
  • 1
    Possible duplicate of Apache Commons Codec with Android: could not find method Commented Mar 17, 2017 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 ) Commented Mar 17, 2017 at 11:44
  • Possible duplicate of Android - decodeBase64 crashes App Commented Mar 17, 2017 at 11:55

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

2 Comments

I am sorry but this didn't work.Beside, I am trying to decode not encode.
This helped a lot , it seems I was mistakenly putting the wrong import: org.apache.commons.codec.binary.Base64 .. now it works
2
public static String toBase64(String value){
    if (value == null)
        value = "";
    return Base64.encodeToString(value.trim().getBytes(), android.util.Base64.DEFAULT);
}

Comments

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;
        }

1 Comment

Yes, but it is not working .. at the moment I am trying to understand why ! .. meanwhile I posted the original problem I am having
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;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.