0

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.

asked Mar 10 at 22:28
6
  • what happens if original data is 3 bytes length? Commented Mar 10 at 22:37
  • 1
    why do you need to iterate over all keys, if only last key is actually used to populate unwrapEncrypt? Commented Mar 10 at 22:37
  • 1
    Problem: "The keys used are in reverse order" but only the last key is relevant, assuming first and last key are different (see Iłya's second comment - the first comment also points a possible problem) Commented Mar 10 at 22:53
  • 2
    you probably wanted to exchange the inner and the outer loops, that is, the key loop is the inner one. (by the way, the order of XOR values is not relevant, only which values are used is relevant - as coded, only the last key is used; previous calculated values are ignored) Commented Mar 10 at 23:06
  • 5
    As a separate note, you should not call this 'encryption'. [A] XORing it multiple times adds no security in any way - the operation d XOR k1 XOR k2 XOR k3 XOR k4 is mathematically equivalent to d XOR kp where kp is simply x1 XOR k2 XOR k3 XOR k4 on its own; hence, this adds nothing, and your code is 'silly' (calculate kp once 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. Commented Mar 10 at 23:38

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.