?
Solved

String XOR Method

Posted on 2000-05-12
1
Medium Priority
?
4,171 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
[X]
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
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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