小编典典

Android-DecodeBase64使App崩溃

java

我必须加密一个字符串,但应用程序未达到加密方法,它在加载时崩溃。
我正在使用Apache Commons Codec库。

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

由于某种原因,该应用程序在第三行崩溃。

我的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)

有什么线索吗?

更新

我将代码更改为此:

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

但是现在我又遇到了其他错误

java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.decodeBase64

阅读 260

收藏
2020-11-26

共1个答案

小编典典

试试这个:

// 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;
        }
2020-11-26