I have been writing a VoIP program and have used the code below to encrypt some data for sending in Java:
private byte[] encryptData(byte[] data){
ByteBuffer unwrapEncrypt = ByteBuffer.allocate(data.length + 2);
short authKey = 10;
unwrapEncrypt.putShort(authKey);
ByteBuffer plainText = ByteBuffer.wrap(data);
for (int i = 0; i < keys.size(); i++) {
for (int j = 0; j < data.length / 4; j++) {
int fourByte = plainText.getInt();
fourByte = fourByte ^ keys.get(i); // XOR operation with key
if(i == keys.size() - 1) {
unwrapEncrypt.putInt(fourByte);
}
}
plainText.rewind();
}
return unwrapEncrypt.array();
}
and have tried doing the opposite to decrypt it on the other end:
ByteBuffer cipherText = ByteBuffer.wrap(block);
short authKey = cipherText.getShort();
if (authKey == 10) {
for (int i = 0; i < keys.size(); i++) {
for (int j = 0; j < block.length / 4; j++) {
int fourByte = cipherText.getInt();
fourByte = fourByte ^ keys.get(i); // XOR decrypt
if (i == keys.size() - 1) {
unwrapDecrypt.putInt(fourByte);
}
}
cipherText.rewind();
}
byte[] decryptedBlock = unwrapDecrypt.array();
However, the data I receive is still somewhat encrypted and as such the audio is garbled. The keys used are in reverse order in the receiver, so that should not be an issue. I assume the issue is with the indexing of ByteBuffers but am unsure how to proceed.
lang-java
datais 3 bytes length?unwrapEncrypt?d XOR k1 XOR k2 XOR k3 XOR k4is mathematically equivalent tod XOR kpwhere kp is simplyx1 XOR k2 XOR k3 XOR k4on its own; hence, this adds nothing, and your code is 'silly' (calculatekponce and just XOR with that, eliminates a loop!), [B] "decrypting" this is child's play. Call it perhaps 'obfuscation' - make sure your words don't mislead.