gpt4 book ai didi

java - 在 Android 中加密并在 CryptoJS 中解密

转载 作者:行者123 更新时间:2023-11-29 04:23:24 25 4
gpt4 key购买 nike

您好,我有 Java 代码可以解密使用 CryptoJS 库 (AES) 加密的密文。现在我想编写将再次加密明文的 javacode。

请找到下面的代码。

 try {
String secret = "René Über";
String cipherText="U2FsdGVkX1+tsmZvCEFa/iGeSA0K7gvgs9KXeZKwbCDNCs2zPo+BXjvKYLrJutMK+hxTwl/hyaQLOaD7LLIRo2I5fyeRMPnroo6k8N9uwKk=";

byte[] cipherData = Base64.decode(cipherText, Base64.DEFAULT);
byte[] saltData = Arrays.copyOfRange(cipherData, 8, 16);

MessageDigest md5 = MessageDigest.getInstance("MD5");
final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes("utf-8"), md5);
SecretKeySpec key = new SecretKeySpec(keyAndIV[0], "AES");
IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);

byte[] encrypted = Arrays.copyOfRange(cipherData, 16, cipherData.length);
Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding");
aesCBC.init(Cipher.DECRYPT_MODE, key, iv);
byte[] decryptedData = aesCBC.doFinal(encrypted);
String decryptedText = new String(decryptedData,"utf-8");
System.out.println("Decrypted "+decryptedText);
//Here I get right plain text as
//System.out: Decrypted The quick brown fox jumps over the lazy dog.


Cipher abc=Cipher.getInstance("AES/CBC/PKCS5Padding");
abc.init(Cipher.ENCRYPT_MODE,key,iv);
byte[] encryptedData=abc.doFinal(decryptedData);
String str=Base64.encodeToString(encryptedData,Base64.DEFAULT);


System.out.println("encrypted "+str);

//Here i want the encrypted text as
// encrypted U2FsdGVkX1+tsmZvCEFa/iGeSA0K7gvgs9KXeZKwbCDNCs2zPo+BXjvKYLrJutMK+hxTwl/hy//aQLOaD7LLIRo2I5fyeRMPnroo6k8N9uwKk=
//but i receive
//System.out: encrypted IZ5IDQruC+Cz0pd5krBsIM0KzbM+j4FeO8pgusm60wr6HFPCX+HJpAs5oPssshGjYjl/J5Ew+//eui



}catch (Exception e)
{}

当我解密代码时,我得到了正确的纯文本,但是当我再次加密纯文本时,我没有像以前那样得到加密文本。请帮忙。

GenerateKeyAndIV函数代码:-

 public static byte[][] GenerateKeyAndIV(int keyLength, int ivLength, int iterations, byte[] salt, byte[] password, MessageDigest md) {

int digestLength = md.getDigestLength();
int requiredLength = (keyLength + ivLength + digestLength - 1) / digestLength * digestLength;
byte[] generatedData = new byte[requiredLength];
int generatedLength = 0;

try {
md.reset();

// Repeat process until sufficient data has been generated
while (generatedLength < keyLength + ivLength) {

// Digest data (last digest if available, password data, salt if available)
if (generatedLength > 0)
md.update(generatedData, generatedLength - digestLength, digestLength);
md.update(password);
if (salt != null)
md.update(salt, 0, 8);
md.digest(generatedData, generatedLength, digestLength);

// additional rounds
for (int i = 1; i < iterations; i++) {
md.update(generatedData, generatedLength, digestLength);
md.digest(generatedData, generatedLength, digestLength);
}

generatedLength += digestLength;
}

// Copy key and IV into separate byte arrays
byte[][] result = new byte[2][];
result[0] = Arrays.copyOfRange(generatedData, 0, keyLength);
if (ivLength > 0)
result[1] = Arrays.copyOfRange(generatedData, keyLength, keyLength + ivLength);

return result;

} catch (DigestException e) {
throw new RuntimeException(e);

} finally {
// Clean out temporary data
Arrays.fill(generatedData, (byte)0);
}
}

最佳答案

您的密文开头有“Salted__<8 byte salt>”,您在解密时会跳过它。如果你想创建 OpenSSL 兼容的密文,你需要在你的加密模式中加上相同的前缀。

当您在 base64 到十六进制解码器中查看您的加密代码密文时,它似乎是正确的,例如提供的那个here .但是,因为每个字符仅包含 64 位,并且字节移动了 16 个位置(不能被 3 整除),所以它只是表明您的整个密文不正确,而它只是在前面缺少 16 个字节。

关于java - 在 Android 中加密并在 CryptoJS 中解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47741649/

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