Solved

String XOR Method

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 60
Custom Java Application connects to database: Which one of the 20? 4 41
Configure a Bean in an XML file 4 49
how to debug htl and js pages 8 58
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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

726 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