没有静态方法encodeBase64String([B)Ljava/lang/String; 在类 Lorg/apache/commons/codec/binary/Base64 中;或其超类

Cod*_*fee 1 java encryption android aes kotlin

实际上我正在尝试在我的 Android 应用程序中添加 Aes 加密和解密功能。在加密过程中,它会让我的 motoG5 S plus 设备崩溃,但它在我的 OnePlus 设备中工作正常。

这是我的代码: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;
} }
Run Code Online (Sandbox Code Playgroud)

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

String encryptedText=AesUtil().encrypt("codeinsidecoffee")
Run Code Online (Sandbox Code Playgroud)

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。加密(AesUtil.java:40)

Cod*_*fee 6

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

代替:

String encodedString = Base64.encodeBase64String(bytes);
Run Code Online (Sandbox Code Playgroud)

使用:

String encodedString = new String(Base64.encodeBase64(bytes));
Run Code Online (Sandbox Code Playgroud)

对于解码,而不是:

byte[] bytes = Base64.decodeBase64(encodedString);
Run Code Online (Sandbox Code Playgroud)

使用:

byte[] bytes = Base64.decodeBase64(encodedString.getBytes()); 
Run Code Online (Sandbox Code Playgroud)

方法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;
} }
Run Code Online (Sandbox Code Playgroud)