[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String XOR Method

Posted on 2000-05-12
1
Medium Priority
?
4,178 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 1200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

873 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