【问题标题】:No static method encodeBase64String([B)Ljava/lang/String; in class Lorg/apache/commons/codec/binary/Base64; or its super classes没有静态方法 encodeBase64String([B)Ljava/lang/String;在 Lorg/apache/commons/codec/binary/Base64 类中;或其超类
【发布时间】:2020-12-30 04:38:36
【问题描述】:

实际上,我正在尝试在我的 android 应用程序中添加 Aes 加密和解密功能。在加密期间,它会使我的 motoG5 S plus 设备崩溃,但它在我的 One Plus 设备中正常工作。

这是我的代码:AesUtil.Java

public class AesUtil {
private  int keySize = 256;
private  int iterationCount =  1000 ;
private  Cipher cipher;

private final String salt = "36e8fc9a6adf090665f459a7ad1b864d";
private final String iv = "ab00b7ea4e88500f2f0a17a7b5c7bcb1";
private final String passphrase = "ArknRQxD1YgaSFRHrjYazX7JMrlRxTERdkQx0dhENVlz";

public AesUtil() {
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    }
    catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw fail(e);
    }
}

public String encrypt(String plaintext) {
    try {
        SecretKey key = generateKey(salt, passphrase);
        byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, iv, plaintext.getBytes("UTF-8"));
        return Base64.encodeBase64String(encrypted);
    }
    catch (UnsupportedEncodingException e) {
        throw fail(e);
    }
}




public String decrypt(String salt, String iv, String passphrase, String ciphertext) {
    try {
        SecretKey key = generateKey(salt, passphrase);
        byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv, base64(ciphertext));
        return new String(decrypted, "UTF-8");
    } catch (Exception e) {
        throw fail(e);
    }
}

private byte[] doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes) {
    try {
        cipher.init(encryptMode, key, new IvParameterSpec(hex(iv)));
        return cipher.doFinal(bytes);
    } catch (InvalidKeyException
            | InvalidAlgorithmParameterException
            | IllegalBlockSizeException
            | BadPaddingException e) {
        throw fail(e);
    }
}

public SecretKey generateKey(String salt, String passphrase) {
    try {
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), iterationCount, keySize);
        SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
        return key;
    }
    catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        return null;
    }
}

public static byte[] base64(String str) {
    return Base64.decodeBase64(str);
}

public static byte[] hex(String str) {
    try {
        return Hex.decodeHex(str.toCharArray());
    }
    catch (DecoderException e) {
        throw new IllegalStateException(e);
    }
}

private IllegalStateException fail(Exception e) {
    e.printStackTrace();
    return null;
} }

在我的主要活动中调用函数:

String encryptedText=AesUtil().encrypt("codeinsidecoffee")

Moto G5s Plus 内部的错误日志:

java.lang.NoSuchMethodError: 没有静态方法 encodeBase64String([B)Ljava/lang/String;在 Lorg/apache/commons/codec/binary/Base64 类中;或其超类('org.apache.commons.codec.binary.Base64' 的声明出现在 /system/framework/org.apache.http.legacy.boot.jar 中) 在 com.justcodenow.bynfor.utils.AesUtil.encrypt(AesUtil.java:40)

【问题讨论】:

    标签: java android kotlin encryption aes


    【解决方案1】:

    方法 1: 此方法从 Apache Commons Codec 1.4 版开始可用。如果手机操作系统只有旧版本的 Codec 包,该方法将不可用。或者,我们可以使用旧版本中存在的方法。

    代替:

    String encodedString = Base64.encodeBase64String(bytes);
    

    用途:

    String encodedString = new String(Base64.encodeBase64(bytes));
    

    对于解码,而不是:

    byte[] bytes = Base64.decodeBase64(encodedString);
    

    用途:

    byte[] bytes = Base64.decodeBase64(encodedString.getBytes()); 
    

    方法2:也可以使用下面的完整代码进行加解密。

    import android.util.Base64;
    import org.apache.commons.codec.DecoderException;
    import org.apache.commons.codec.binary.Hex;
    import javax.crypto.*;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.PBEKeySpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.KeySpec;
    
    public class AesUtil {
    private  int keySize = 256;
    private  int iterationCount =  1000 ;
    private  Cipher cipher;
    
    private final String salt = "36e8fc9a6adf090665f459a7ad1b864d";
    private final String iv = "ab00b7ea4e88500f2f0a17a7b5c7bcb1";
    private final String passphrase = "ArknRQxD1YgaSFRHrjYazX7JMrlRxTERdkQx0dhENVlz";
    
    public AesUtil() {
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        }
        catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            throw fail(e);
        }
    }
    
    public String encrypt(String plaintext) {
        try {
            SecretKey key = generateKey(salt, passphrase);
            byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, iv, plaintext.getBytes("UTF-8"));
            return Base64.encodeToString(encrypted, Base64.DEFAULT);
        }
        catch (UnsupportedEncodingException e) {
            throw fail(e);
        }
    }
    
    
    
    
    public String decrypt(String salt, String iv, String passphrase, String ciphertext) {
        try {
            SecretKey key = generateKey(salt, passphrase);
            byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv, base64(ciphertext));
            return new String(decrypted, "UTF-8");
        } catch (Exception e) {
            throw fail(e);
        }
    }
    
    private byte[] doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes) {
        try {
            cipher.init(encryptMode, key, new IvParameterSpec(hex(iv)));
            return cipher.doFinal(bytes);
        } catch (InvalidKeyException
                | InvalidAlgorithmParameterException
                | IllegalBlockSizeException
                | BadPaddingException e) {
            throw fail(e);
        }
    }
    
    public SecretKey generateKey(String salt, String passphrase) {
        try {
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
            KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), iterationCount, keySize);
            SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
            return key;
        }
        catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            return null;
        }
    }
    
    public static byte[] base64(String str) {
        return Base64.decode(str, Base64.DEFAULT);
    }
    
    public static byte[] hex(String str) {
        try {
            return Hex.decodeHex(str.toCharArray());
        }
        catch (DecoderException e) {
            throw fail(e);
        }
    }
    
    private IllegalStateException fail(Exception e) {
        e.printStackTrace();
        return null;
    }
    
    
    private static IllegalStateException fail(DecoderException e) {
        e.printStackTrace();
        return null;
    } }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 2020-06-21
      • 2017-10-16
      • 2016-01-12
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      相关资源
      最近更新 更多