[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Doing an XOR on a string?

Posted on 2009-02-12
1
Medium Priority
?
751 Views
Last Modified: 2012-05-06
Hi,

I am doing an XOR on a string in an objective-c application, the strings are UTF-8, unicode.

Now I'm trying to 'decrypt' the strings again in PHP, but it is not working. I have the following which works on ASCII characters I guess. It would be difficult to get the objective-c part to work in ascii only, so I'm looking to modify the PHP part to work with UTF-8. I tried just getting rid of ord() and replacing chr() with a unicode variant, but no luck.

Thanks

function XOREncryption($input)
{
    $crypt_key = "ABCDEFG";
 
    $vlen = strlen($input);
    $klen = strlen($crypt_key);
    $k = 0;
 
    for ($v = 0; $v < $vlen; $v++) {
            
        $c = ord($input[$v]) ^ ord($crypt_key[$k]);
        $input[$v] = chr($c);
 
        $k++;
        if ($k == $klen) {
            $k = 0;
        }
    }
 
    return $input;            
}

Open in new window

0
Comment
Question by:DJ_AM_Juicebox
1 Comment
 
LVL 19

Accepted Solution

by:
Michael701 earned 2000 total points
ID: 23630548
you should be able to have this a litle cleaner ^ works on characters just as well as numbers

        $input[$v] = $input[$v] ^ $crypt_key[$k];

I tested this and it works


function XOREncryption($input)
{
    $crypt_key = "ABCDEFG";
 
    $vlen = strlen($input);
    $klen = strlen($crypt_key);
    $k = 0;
 
    for ($v = 0; $v < $vlen; $v++) {
            
         $input[$v] = $input[$v] ^ $crypt_key[$k];
 
        $k++;
        if ($k == $klen) {
            $k = 0;
        }
    }
 
    return $input;            
}
 
$message=XOREncryption("hello world");
echo "ecrypt: ". $message ."<br />\n";
$original=XOREncryption($message);
echo "original: ".$original ."<br />\n";

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 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