Solved

String XOR Method

Posted on 2000-05-12
1
4,159 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now