实际上,我正在尝试在我的android应用程序中添加aes加密和解密功能。在加密过程中,它会让我崩溃在我的motog5 s plus设备,但它在我的一个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)
1条答案
按热度按时间lf5gs5x21#
方法1:此方法自ApacheCommons编解码器的1.4版起可用。如果手机的操作系统只有旧版本的编解码器软件包,该方法将不可用。或者,我们可以使用旧版本中存在的方法。
而不是:
用途:
对于解码,而不是:
用途:
方法2:您还可以使用下面完整的代码进行加密和解密。