gpt4 book ai didi

Java AES 加密无法正确解密

转载 作者:行者123 更新时间:2023-11-30 09:28:07 25 4
gpt4 key购买 nike

我在下面进行了这个测试,我试图验证我的带有压缩/解压的 AES 加密/解密是否正常工作。我有一个加密/解密测试和一个压缩/解压测试,我知道它们单独工作正常,但由于某种原因,当我将它们组合在一起时,IV 在解压后搞砸了。我被困住了,不知道还要寻找什么,任何人都可以提供任何建议或帮助吗?如果我需要发布更多代码,请告诉我。我想我包括了重要的东西。

对于上下文:我正在尝试做的是获取一些敏感数据,使用 AES 对其进行加密,使用 RSA 公钥对 AES key 进行加密,然后压缩这两个数据片段,以便它们可以通过网络发送。然后在另一端我想解压缩数据,使用 RSA 私钥解密 AES key ,然后用它来解密数据。如果有另一种方法可以做到这一点,我就不会被困在写我自己的一切。如果您建议图书馆,请只建议我可以在商业产品中使用的图书馆。

@Test
public void testEncryptionDecryptionProcesses() throws SSHException {

SSHKey key1 = generateKeyPair();

UnencryptedData data = new UnencryptedData();
data.setUserName("hardy");
data.setHashedPassword("somepassword");
data.setRequest("eat");
data.setResponse("");
data.setTimestamp(new Timestamp(new Date().getTime()).toString());
data.setPublicKeyMod(key1.getPublicMod().toString());
data.setPublicKey(key1.getPublicKey().toString());

byte[] bytes = encryptAndCompress(data, key1);

assertTrue(bytes != null);
assertTrue(bytes.length > 0);

UnencryptedData decryptedData = decompressAndDecrypt(bytes, key1);
assertEquals(data.getDataForEncryption(), decryptedData.getDataForEncryption());

}

public static byte[] encryptAndCompress(UnencryptedData data, SSHKey sshKey) {

byte[] results = null;

try {
byte[] aesKey = createKeyForAES(AES_BIT_LENGTH);
//this should use the servers public key so that only the server can decrypt it
//gather data, get a digest, encrypt the data
UnencryptedData digestedData = createDigest(data);
//encrypt it
EncryptedData toCompress = encryptDataAES(digestedData, aesKey);
String encryptedAESKey = encryptKey(sshKey, aesKey);
toCompress.setEncryptedAESKey(encryptedAESKey);
//compress it
byte[] compressed = compressString(toCompress.getDataForCompression());
//return the compressed and encrypted data.
results = compressed;
} catch(SSHException e) {

Log.e("SSHFunctions.encryption", "Unable to run the encryption/compression process on the data");
} catch (UnsupportedEncodingException e) {

Log.e("SSHFunctions.encryption", "Charset not supported");
}

return results;
}

public static UnencryptedData decompressAndDecrypt(byte[] data, SSHKey sshKey) {

UnencryptedData results = null;

try {
//take the data and decompress it, should result in encryptedData|encryptedAESKey
byte[] decompressed = decompressString(data);
String decompressedStr = new String(decompressed, CHAR_SET);
String[] decompressedArr = decompressedStr.split(SPLIT_STRING);
//using the users private key decrypt the data
byte[] decryptedKey = decryptKey(sshKey, decompressedArr[1]);
EncryptedData encryptedData = new EncryptedData();
encryptedData.setAesEncryptedData(decompressedArr[0].getBytes(CHAR_SET));
encryptedData.setIV(decompressedArr[2].getBytes(CHAR_SET)); //TODO: this doesn't seem to decompress correctly
//create a digest from the decrypted data and compare it with the digest that was included.
UnencryptedData decryptedDate = decryptDataAES(encryptedData, decryptedKey);
if(validDigest(decryptedDate)) {

results = decryptedDate;
}
//if equal return the data, if not equal return null
} catch(Exception e) {

Log.e("SSHFunctions.decryption", "Unable to run the uncompress/decrypt process on the data");
}

return results;
}

public static byte[] decompressString(byte[] toDecompress) {

ByteArrayInputStream bis = new ByteArrayInputStream(toDecompress);
byte[] uncompressed;

try {

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
GZIPInputStream is = new GZIPInputStream(bis);

byte[] tmp = new byte[256];

while (true) {

int r = is.read(tmp);
if (r < 0) {

break;
}
buffer.write(tmp, 0, r);
}
is.close();

uncompressed = buffer.toByteArray();

try {
bis.close();
} catch (IOException e) {
;
}

try {
buffer.close();
} catch (IOException e) {
;
}
} catch(IOException e) {

uncompressed = null;
Log.e("Zipfunctions.decompress", "Unable to decompress");
}

return uncompressed;
}

public static byte[] compressString(byte[] toCompress) {

byte[] toCompressBytes = toCompress;
byte[] compressed;

ByteArrayOutputStream bos = new ByteArrayOutputStream(toCompressBytes.length);
try {
GZIPOutputStream compressor = new GZIPOutputStream(bos);

compressor.write(toCompressBytes, 0, toCompress.length);
compressor.close();

compressed = bos.toByteArray();

try {
bos.close();
} catch(IOException e) {
;
}
} catch(IOException e) {

compressed = null;
Log.e("ZipFunctions.compress", "Unable to compress data");
}

return compressed;
}

最佳答案

您正在尝试实现类似于 OpenPGP 的密码系统.也就是说,您想要使用对称 key 加密任意数量的数据,并与接收者安全地共享此 key (和加密数据)。

因此,我建议您考虑使用 Java OpenPGP 库 provided by the BouncyCastle team .

他们的许可证是 very permissive .但是,他们的文档非常糟糕,因此您需要通过 Google 搜索许多示例以了解如何实现您的目标。

关于Java AES 加密无法正确解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14207813/

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