Java中的基本加密算法不解密
我试图用Java实现TEA微型加密算法,对包含音频的字节[]进行编码,大小为512 下面是我的加密函数:Java中的基本加密算法不解密,java,encryption,Java,Encryption,我试图用Java实现TEA微型加密算法,对包含音频的字节[]进行编码,大小为512 下面是我的加密函数: //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);
//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));
}
和解密调用:
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块代码,音频是完美的。有人能认出发生了什么事吗?谢谢我觉得您的decrpyt方法在与比较时有一个bug。必须在-=运算符的左侧交换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();
}
>>->>>>>?你是说在哪里?在右班。哦,我认为&优先。所以x>>5&0x7ffffff和y>>5&0x7ffffff->x>>>5&0x7ffffff和y>>5&0x7ffffff内部加密和解密?我认为x>>5&0x7ffffffff。请注意,我甚至还没有看过对茶的描述,但这对我来说似乎是不正确的。事实上,即使上面的说法似乎也不正确,因为对于这段代码,您不需要-。一步一步地测试。它应该是旋转的而不是移位的吗?嗯,我来看看。如果我能睁大眼睛,那就是。啊,我想这是另一个错误。请注意,解密工作并不意味着它是TEA的正确实现。您需要针对测试向量或已知良好的实现进行测试。而且,请注意,即使两个人在实现过程中也可能犯相同的错误。如果您有测试向量,那么很容易看到,如果它是设计师第一次实现,那么就不容易看到。