2

I have to encrypt a String but the app doesn't reach the encrypt method, it crashes on load.
I'm using Apache Commons Codec library.

private EditText txtPass = (EditText)findViewById(R.id.txtPass);
public String key = txtPass.getText().toString();
public byte[] key_Array = org.apache.commons.codec.binary.Base64.decodeBase64(key);

For some reason, the app crashes in the third line.

My logcat.

12-03 14:03:31.441  23420-23420/com.example.cristiano.automacao V/ActivityThread﹕ Class path: /data/app/com.example.cristiano.automacao-2.apk, JNI path: /data/data/com.example.cristiano.automacao/lib
12-03 14:03:31.591  23420-23420/com.example.cristiano.automacao W/dalvikvm﹕ VFY: unable to resolve static method 8974: Lorg/apache/commons/codec/binary/Base64;.decodeBase64 (Ljava/lang/String;)[B
12-03 14:03:31.601  23420-23420/com.example.cristiano.automacao W/dalvikvm﹕ VFY: unable to resolve static method 8984: Lorg/apache/commons/codec/binary/Base64;.encodeBase64String ([B)Ljava/lang/String;
12-03 14:03:31.611  23420-23420/com.example.cristiano.automacao W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41c7b438)

Any clue about that?

Updated

I changed the code to this:

public static String key = "1234";
public static byte[] key_Array = decodeBase64(key);

But now I got other error

java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.decodeBase64
sudodev
  • 353
  • 1
  • 6
  • 18
  • Why are you using the full name "org.apache.commons.codec.binary.Base64.decodeBase64" ? You have to import the library, and simply use "decodeBase64(key);". – Wildcopper Dec 03 '14 at 16:24
  • I changed it. But the problem persists. I think the problem is on Static methods. Check logcat: "unable to resolve static method 8974" – sudodev Dec 03 '14 at 16:37

3 Answers3

2

Try this:

// 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;
        }
Ludger
  • 362
  • 3
  • 16
0

Simply create an object of Base64 and use it to encode or decode in Android, when using org.apache.commons.codec.binary.Base64 library

To Encode

Base64 ed=new Base64();

String encoded=new String(ed.encode("Hello".getBytes()));

Replace "Hello" with the text to be encoded in String Format.

To Decode

Base64 ed=new Base64();

String decoded=new String(ed.decode(encoded.getBytes()));

Here encoded is the String variable to be decoded

Community
  • 1
  • 1
Karan
  • 1
  • 5
0

you are giving a string as an input you should give key.getBytes() to the decode methos

khoshrang
  • 146
  • 11