gpt4 book ai didi

java - Java中的基本加密算法无法解密

转载 作者:行者123 更新时间:2023-12-02 10:10:14 25 4
gpt4 key购买 nike

我正在尝试在 Java 中实现 TEA(微型加密算法),以对包含大小为 512 的音频的 byte[] 进行编码。

这是我的加密函数:

//Encrypt 64bit/8byte buffer with 128bit/16byte key
public byte[] encrypt(int[] data, int[] key) {
int x = data[0];
int y = data[1];
ByteBuffer encrypted = ByteBuffer.allocate(8);
int sum = 0;
int constant = 0x9e3779b9; //magic constant

for (int k = 0; k < 32; ++k) {
sum += constant;
x += (y << 4 & 0xfffffff0) + key[0] ^ y + sum ^ (y >> 5 & 0x7ffffff) + key[1];
y += (x << 4 & 0xfffffff0) + key[2] ^ x + sum ^ (x >> 5 & 0x7ffffff) + key[3];
}
encrypted.putInt(x);
encrypted.putInt(y);
return encrypted.array();
}

并解密:

public byte[] decrypt(int[] data, int[] key) { 
int x = data[0];
int y = data[1];
ByteBuffer decrypted = ByteBuffer.allocate(8);
int sum = 0xC6EF3720; //32*delta
int constant = 0x9e3779b9; //magic constant

for (int k = 0; k < 32; ++k) {
x -= (x << 4 & 0xfffffff0) + key[2] ^ x + sum ^ (x >> 5 & 0x7ffffff) + key[3];
y -= (y << 4 & 0xfffffff0) + key[0] ^ y + sum ^ (y >> 5 & 0x7ffffff) + key[1];
sum -= constant;
}
decrypted.putInt(x);
decrypted.putInt(y);
return decrypted.array();
}

和我的加密调用:

ByteBuffer unwrapEncrypt = ByteBuffer.allocate(512);
int[] encryptionKey = {55555, 8888, 123857, 912029};

//block is a byte[] with length 512
ByteBuffer plainText = ByteBuffer.wrap(block);
for (int j = 0; j < block.length / 8; j++) {
//Initiate array for int pairs
int[] plainTextInts = new int[2];
plainTextInts[0] = plainText.getInt();
plainTextInts[1] = plainText.getInt();
//Encrypt and store
unwrapEncrypt.put(encrypt(plainTextInts, encryptionKey));
}

并解密调用:

ByteBuffer audioToPlay = ByteBuffer.allocate(512);
int[] decryptionKey = {55555, 8888, 123857, 912029};

//audio is a byte[] with length 512
ByteBuffer cipherText = ByteBuffer.wrap(audio);
for (int j = 0; j < audio.length / 8; j++) {
int[] plainTextInts = new int[2];
//Initiate array for int pairs
plainTextInts[0] = cipherText.getInt();
plainTextInts[1] = cipherText.getInt();
//Decrypt and store
audioToPlay.put(decrypt(plainTextInts, decryptionKey));
}

对大量代码感到抱歉 - 我已经尝试分析发送的音频和接收到的解密数据 - 它们的长度都是正确的,只是完全不同。如果我删除这 4 个代码块,音频就完美了。有人能发现发生了什么吗?谢谢

最佳答案

Wikipedia' s description of TEA 进行比较时,您的 decrpyt() 方法似乎存在错误。 。您必须交换 -= 运算符左侧的 x 和 y。以下似乎对我有用:

public byte[] decrypt(int[] data, int[] key) { 
int x = data[0];
int y = data[1];
ByteBuffer decrypted = ByteBuffer.allocate(8);
int sum = 0xC6EF3720; //32*delta
int constant = 0x9e3779b9; //magic constant

for (int k = 0; k < 32; ++k) {
y -= (x << 4 & 0xfffffff0) + key[2] ^ x + sum ^ (x >> 5 & 0x7ffffff) + key[3];
x -= (y << 4 & 0xfffffff0) + key[0] ^ y + sum ^ (y >> 5 & 0x7ffffff) + key[1];
sum -= constant;
}
decrypted.putInt(x);
decrypted.putInt(y);
return decrypted.array();
}

关于java - Java中的基本加密算法无法解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55052753/

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