gpt4 book ai didi

java - 安卓 AES 问题

转载 作者:行者123 更新时间:2023-11-29 21:46:28 26 4
gpt4 key购买 nike

我需要在我的 Android 应用程序中实现 AES 算法,我在下面创建了这段代码,它可以像 Java 应用程序一样完美地工作,但 Android 似乎无法识别 JAXB。因为如您所见,我使用了 import javax.xml.bind.DatatypeConverter,因为我使用数据类型转换器将 byte[] 转换为字符串...

我尝试导入 jaxb jar,但它再次失败并出现此错误:Conversion to Dalvik format failed with error 1。

我该如何解决这个问题?

代码如下:

public class AESCrypt {

private final Cipher cipher;
private final SecretKeySpec key;
private AlgorithmParameterSpec spec;
private String encryptedText, decryptedText;
ByteArrayOutputStream baos;


public AESCrypt(String password) throws Exception {
// hash password with SHA-256 and crop the output to 128-bit for key
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(password.getBytes("UTF-8"));
byte[] keyBytes = new byte[16];
System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);

cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
key = new SecretKeySpec(keyBytes, "AES");
spec = getIV();
}

public AlgorithmParameterSpec getIV() {
AlgorithmParameterSpec ivspec;
byte[] iv = new byte[cipher.getBlockSize()];
new SecureRandom().nextBytes(iv);
ivspec = new IvParameterSpec(iv);
return ivspec;
}

public String encrypt(String plainText) throws Exception {
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
encryptedText = DatatypeConverter.printBase64Binary(encrypted);
return encryptedText;
}

public String decrypt(String cryptedText) throws Exception {
cipher.init(Cipher.DECRYPT_MODE, key, spec);
byte[] bytes = DatatypeConverter.parseBase64Binary(cryptedText);
byte[] decrypted = cipher.doFinal(bytes);
decryptedText = new String(decrypted, "UTF-8");
return decryptedText;
}

最佳答案

Android 库有类 Base64 (android.util.Base64) 非常方便的将base64字符串转换为数据。

关于java - 安卓 AES 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15686527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com