Kairos
Kairos

Reputation: 159

Base64 Encoding in Java, using android.util.Base64

I know, there are lots of threads about this subject - I've read most of them, but none of them gave me the right answer.

I have the following code:

import android.util.Base64;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Crypter {

    public static void main(String[] args) {
        String data = "Arnab C";
        final String enc = DarKnight.getEncrypted(data);
        System.out.println("Encrypted : " + enc);
        System.out.println("Decrypted : " + DarKnight.getDecrypted(enc));
    }

    static class DarKnight {

        private static final String ALGORITHM = "AES";

        private static final byte[] SALT = "tHeApAcHe6410111".getBytes();// THE KEY MUST BE SAME
        private static final String X = DarKnight.class.getSimpleName();

        static String getEncrypted(String plainText) {

            if (plainText == null) {
                return null;
            }

            Key salt = getSalt();

            try {
                Cipher cipher = Cipher.getInstance(ALGORITHM);
                cipher.init(Cipher.ENCRYPT_MODE, salt);
                byte[] encodedValue = cipher.doFinal(plainText.getBytes());
                return Base64.encode(encodedValue,Base64.DEFAULT);


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

            throw new IllegalArgumentException("Failed to encrypt data");
        }

        public static String getDecrypted(String encodedText) {

            if (encodedText == null) {
                return null;
            }

            Key salt = getSalt();
            try {
                Cipher cipher = Cipher.getInstance(ALGORITHM);
                cipher.init(Cipher.DECRYPT_MODE, salt);
                byte[] decodedValue = Base64.decode(encodedText, Base64.DEFAULT);
                byte[] decValue = cipher.doFinal(decodedValue);
                return new String(decValue);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        static Key getSalt() {
            return new SecretKeySpec(SALT, ALGORITHM);
        }

    }

}

I copied this from Encryption Between PHP & Java - but changed

import com.sun.org.apache.xml.internal.security.utils.Base64;

to

import android.util.Base64;

because the apache-version does not work in Java8.

Due to that change, I had to add a flag to both Base64.decode and Base64.encode. Works fine in decode:

byte[] decodedValue = Base64.decode(encodedText, Base64.DEFAULT);

But when adding a flag to Base64.encode, something strange happens:

When I write "return Base64.encode(", Android Studio tells me it requires a byte[] input and an int flags. So I guess, I simply can use the variable encodedValue as the first argument, since that is a byte[]. As the next argument, I can use Base64.DEFAULT, which is an int with value 0. But Android Studio does not agree: incompatible types, Required: java.lang.String, found: byte[].

Why does Android Studio require a String, when it first says it requires a byte[] ??

Actually, the "why" isn't that important, more important is: how do I fix this?

Any help would be appreciated.

Upvotes: 0

Views: 2181

Answers (1)

laalto
laalto

Reputation: 152817

Use encodeToString() rather than encode(). The first returns a String as expected by your function return type, the latter returns a byte[].

Documentation: https://developer.android.com/reference/android/util/Base64

Upvotes: 1

Related Questions