以下是一个使用Java代码示例来解密Android中的AES-CBC加密的方法:
import java.security.NoSuchAlgorithmException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;
public class AESUtil {
private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String AES_KEY = "YourAESKey"; // 替换为你的AES密钥
private static final String AES_IV = "YourAESIV"; // 替换为你的AES向量
public static String decrypt(String encryptedData) {
try {
byte[] key = AES_KEY.getBytes("UTF-8");
byte[] iv = AES_IV.getBytes("UTF-8");
byte[] encryptedBytes = Base64.decode(encryptedData, Base64.DEFAULT);
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
要解密使用AES-CBC加密的数据,只需将加密的数据作为参数传递给decrypt
方法即可。请确保替换AES_KEY
和AES_IV
为你自己的密钥和向量。