Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8942289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:28:52+00:00 2026-06-15T11:28:52+00:00

Let’s say I have a plain text a nice cup of milk tea ,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:28:53+00:00Added an answer on June 15, 2026 at 11:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a method in java, which looks up a user in
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.