2014-12-03 170 views
2

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

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 
+0

? 你必须导入库,并简单地使用“decodeBase64(key);”。 – Wildcopper 2014-12-03 16:24:22

+0

我改变了它。但问题依然存在。我认为问题在于静态方法。检查logcat:“无法解析静态方法8974” – Cristiano 2014-12-03 16:37:40

回答

2

试试这个:

// 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; 
     } 
+0

谢谢,为我工作:) – murli 2017-05-08 09:40:38

0

只需创建的Base64的一个对象,并用它来进行编码或在机器人,解码使用org.apache.commons.codec.binary.Base64库时

编码

Base64的ED =新的Base64 ();

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

将“Hello”替换为以字符串格式编码的文本。

解码

Base64的ED =新的Base64();

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

这里编码的字符串变量为什么您使用的是全名“org.apache.commons.codec.binary.Base64.decodeBase64”解码