Android 无法在运行时从 JAR 中找到方法

Android cannot find method from JAR on runtime

我一直在开发一个使用 AES 加密的应用程序,它需要一个用于 Base 64 编码和解码的 JAR。

我按照本教程 this 导入 JAR(可能是我的问题,因为他们似乎不知道自己在做什么)。逐行编译很高兴,但是在 phone 上运行时我收到错误消息

 Could not find method org.apache.commons.codec.binary.Base64.decodeBase64, referenced from method com.login.tools.Encoder.decrypt

我的应用程序 gradle 脚本确实引用了 JAR

compile project(':commons-codec-1.10')

应该调用 commons-codec-1.10 gradle 脚本

configurations.create("default")
artifacts.add("default", file('commons-codec-1.10.jar'))

调用class本身有点像

import org.apache.commons.codec.binary.Base64;
.
.
.
public static String decrypt(String strToDecrypt)
    setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt))));

使用 Gradle 依赖项,而不是 .jar:

dependencies {
    compile 'commons-codec:commons-codec:1.10'
}

参见:http://mvnrepository.com/artifact/commons-codec/commons-codec/1.10

此外,我看到您正在尝试使用 org.apache.commons.codec.binary.Base64.decodeBase6。 Android 拥有自己的 Base64 库:

http://developer.android.com/reference/android/util/Base64.html

我遇到了同样的问题。但是这个解决方案对我有用。希望对你有帮助。

如果要解密字符串,请使用以下代码。您也可以使用

android.util.Base64

而不是

org.apache.commons.codec.binary.Base64

package com.myApp.security;

import  android.util.Base64;

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

public class Security {
    public static String encrypt(String input, String key){
      byte[] crypted = null;
      try{
        SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
          Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
          cipher.init(Cipher.ENCRYPT_MODE, skey);
          crypted = cipher.doFinal(input.getBytes());
        }catch(Exception e){
            System.out.println(e.toString());
        }
        return new String(Base64.encode(crypted, Base64.DEFAULT));
    }

    public static String decrypt(String input, String key){
        byte[] output = null;
        try{
          SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
          Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
          cipher.init(Cipher.DECRYPT_MODE, skey);
          //output = cipher.doFinal(Base64.decodeBase64(input));
          output = cipher.doFinal(Base64.decode(input, Base64.DEFAULT));
        }catch(Exception e){
          System.out.println(e.toString());
        }
        return new String(output);
    }   
}

直接在您的 class 中使用这些加密和解密方法。

String key = "Dhruv349emaeGtuL";
String data = "example";
Log.e("Decrypt_Key", Security.decrypt(Security.encrypt(data, key), key));
Log.e("Encrypted Data", Security.encrypt(data, key));