在Android 11下正常,8.1下不正常
修改import
import org.apache.commons.codec.binary.Base64;
为
import android.util.Base64;
public static String encrypt(String data) {
try {
SecretKey secretKey = new SecretKeySpec(SECRET_KEY.getBytes(), ENCRYPTION_ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
IvParameterSpec ivParameterSpec = new IvParameterSpec(INIT_VECTOR.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] encryptedData = cipher.doFinal(data.getBytes("UTF-8"));
return Base64.encodeToString(encryptedData, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String decrypt(String encryptedData) {
try {
SecretKey secretKey = new SecretKeySpec(SECRET_KEY.getBytes(), ENCRYPTION_ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
IvParameterSpec ivParameterSpec = new IvParameterSpec(INIT_VECTOR.getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] decryptedData = cipher.doFinal(Base64.decode(encryptedData, Base64.DEFAULT));
return new String(decryptedData, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
标签:String,一例,Base64,Cipher,new,Android,getBytes,cipher
From: https://www.cnblogs.com/zhaogaojian/p/17775356.html