Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR cipher in Java & PHP: different results

Let's say I have a plain text a nice cup of milk tea, which is going to be XOR cipher-ed with key 12345.

This Java code:

import sun.misc.BASE64Encoder;

import sun.misc.BASE64Decoder;

public class XORTest {

  public static void main(String args[]){

    String plaintext = "a nice cup of milk tea";
    String key = "12345";
    String encrypted = xor_encrypt(plaintext, key);
    String decrypted = xor_decrypt(encrypted, key);
    System.out.println("Encrypted: "+encrypted);
    System.out.println("Decrypted: "+decrypted);
  }

  public static String xor_encrypt(String message, String key){
    try {
      if (message==null || key==null ) return null;

      char[] keys=key.toCharArray();
      char[] mesg=message.toCharArray();
      BASE64Encoder encoder = new BASE64Encoder();

      int ml=mesg.length;
      int kl=keys.length;
      char[] newmsg=new char[ml];

      for (int i=0; i<ml; i++){
        newmsg[i]=(char)(mesg[i]^keys[i%kl]);
      }
      mesg=null; 
      keys=null;
      String temp = new String(newmsg);
      return new String(new BASE64Encoder().encodeBuffer(temp.getBytes()));
    }
    catch ( Exception e ) {
      return null;
    }  
  }
  

  public static String xor_decrypt(String message, String key){
    try {
      if (message==null || key==null ) return null;
      BASE64Decoder decoder = new BASE64Decoder();
      char[] keys=key.toCharArray();
      message = new String(decoder.decodeBuffer(message));
      char[] mesg=message.toCharArray();

      int ml=mesg.length;
      int kl=keys.length;
      char[] newmsg=new char[ml];

      for (int i=0; i<ml; i++){
        newmsg[i]=(char)(mesg[i]^keys[i%kl]);
      }
      mesg=null; keys=null;
      return new String(newmsg);
    }
    catch ( Exception e ) {
      return null;
    }  
  }}

gives me:

Encrypted: UBJdXVZUElBBRRFdVRRYWF5YFEFUUw==

Decrypted: a nice cup of milk tea

And this PHP code:

<?php

$input = "a nice cup of milk tea";
$key = "12345";
$encrypted = XOR_encrypt($input, $key);
$decrypted = XOR_decrypt($encrypted, $key);

echo "Encrypted: " . $encrypted . "<br>";
echo "Decrypted: " . $decrypted . "<br>";

function XOR_encrypt($message, $key){
  $ml = strlen($message);
  $kl = strlen($key);
  $newmsg = "";
  
  for ($i = 0; $i < $ml; $i++){
    $newmsg = $newmsg . ($msg[$i] ^ $key[$i % $kl]);
  }
  
  return base64_encode($newmsg);
}

function XOR_decrypt($encrypted_message, $key){
  $msg = base64_decode($encrypted_message);
  $ml = strlen($msg);
  $kl = strlen($key);
  $newmsg = "";
  
  for ($i = 0; $i < $ml; $i++){
    $newmsg = $newmsg . ($msg[$i] ^ $key[$i % $kl]);
  }
  
  return $newmsg;
}

?>

gives me:

Encrypted: MTIzNDUxMjM0NTEyMzQ1MTIzNDUxMg==

Decrypted:

Wonder why both results are different. I must admit before that PHP is not my cup of tea.

BTW, I use this for a toy project, so high security is not needed.

like image 300
anta40 Avatar asked Nov 30 '12 08:11

anta40


People also ask

Which cipher uses XOR?

It is also used in the DES cipher. XOR, or “exclusive or” operates on binary data.

What is XOR based encryption?

XOR Encryption is an encryption method used to encrypt data and is hard to crack by brute-force method, i.e generating random encryption keys to match with the correct one.

Why is XOR used in AES?

XOR allows you to easily encrypt and decrypt a string, the other logic operations don't.

What is a single byte XOR cipher?

In a single byte XOR , each byte from the plaintext is XORed with the encryption key. For example, if an attacker wants to encrypt plaintext cat with a key of 0x40 , then each character (byte) from the text is XORed with 0x40 , which results in the cipher-text #! 4 .


1 Answers

In your PHP encryption method, you have the following code:

for ($i = 0; $i < $ml; $i++){
  $newmsg = $newmsg . ($msg[$i] ^ $key[$i % $kl]);
}

However, $msg is not defined anywhere. That should be $message.

like image 136
Duncan Jones Avatar answered Oct 18 '22 10:10

Duncan Jones