Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

String XOR Method

Posted on 2000-05-12
1
4,164 Views
Last Modified: 2008-02-01
Hi,

I'm looking for a method that makes a XOR operation between 2 String with the following signature : String xor(String str, String key); and used like this :

String myStr = "treteterterterterter";
String myKey = "terwb897856304984erzvgtze";

String myCodedStr = xor(myStr,myKey);
String myDecodedStr = xor(myCodedStr, myKey);

Note that myDecodedStr is equals to myStr.

It's quitely URGENT for me, so that's why I give 300 points for the source code.

Thanks
0
Comment
Question by:baresse
1 Comment
 
LVL 9

Accepted Solution

by:
doronb earned 300 total points
ID: 2803998
Hi baresse,

Try the following method:

  public String xor(String str, String key) {
    String result = null;
    byte[] strBuf = str.getBytes();
    byte[] keyBuf = key.getBytes();
    int c = 0;
    int z = keyBuf.length;
    ByteArrayOutputStream baos = new ByteArrayOutputStream(strBuf.length);
    for (int i = 0; i < strBuf.length; i++) {
      byte bS = strBuf[i];
      byte bK = keyBuf[c];
      byte bO = (byte)(bS ^ bK);
      if (c < z - 1) {
        c++;
      } else {
        c = 0;
      }
      baos.write(bO);
    }
    try {
      baos.flush();
      result = baos.toString();
      baos.close();
      baos = null;
    } catch (IOException ioex) {
    }
    return result;
  }

The idea is to convert the string and the key to byte arrays, then you XOR each byte of the string with the corresponding byte from the key, when you "run-out" of key-bytes you just start over from the start of the key ;)


Regards and good luck,

Doron
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
nextBoolean(double p) for Random class 3 41
use lov values 2 61
jsp login check 12 42
Html Table Looping (part 2) 5 27
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question