Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   XOR Encryption (https://forums.graalonline.com/forums/showthread.php?t=81275)

coreys 08-17-2008 12:24 AM

XOR Encryption
 
I'm not sure if anyone has done this before, but I was rather bored so I made a XOR Encryption function with my own additions to get rid of the parts of the ASCII character set that aren't really shown in Graal.

PHP Code:

/*Paramters:
s - string to be encrypted
usechars - boolean determining whether or not it changes ascii values into characters.
key - Encryption key. (Optional)  If left NULL it replaces with the default. s must be shorter or the same length as the encryption key.
*/
public function XOREncrypt(susecharskey) {
  if (
key == NULL)
    
key "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  
temp.replace "-";
  if (
s.length() <= key.length()) {
    
temp.out "";
    for (
i=0i<s.length(); i++) {
      
temp.getascii(s.charat(i)) xor getascii(key.charat(i));
      if (
usechars) {
        if (
temp.32 && temp.!= 127) {
          
temp.char(temp.a);
          if (
temp." ")
            
temp.temp.replace;
        }
        else 
temp.temp.replace;
      }
      
temp.out @= temp.a;
    }
    return 
temp.out;
  }
  else
    return 
"String too long. (Limit " key.length() @ ")";    


Example:
PHP Code:

XOREncrypt("A nice cat"trueNULL); 

Which will return "-b--&#g+(>"

From what I hear, this method is hard, if not impossible to decrypt, and it's rather simple.

xXziroXx 08-17-2008 12:35 AM

Nice one. :)

DrakilorP2P 08-17-2008 12:50 AM

Quote:

Originally Posted by coreys (Post 1415001)
From what I hear, this method is hard, if not impossible to decrypt, and it's rather simple.

Try all possible keys until you find one that works. Message decrypted.

coreys 08-17-2008 12:56 AM

Quote:

Originally Posted by DrakilorP2P (Post 1415006)
Try all possible keys until you find one that works. Message decrypted.

I'm fairly certain it's impossible to reverse the xor operator.

DrakilorP2P 08-17-2008 01:29 AM

Quote:

Originally Posted by coreys (Post 1415007)
I'm fairly certain it's impossible to reverse the xor operator.

You don't need to reverse it. Either do the above where you try random keys until you stumble upon the correct one or use frequency analysis.

LoneAngelIbesu 08-17-2008 02:15 AM

I just did a simple test, and there's something wrong with this code (I'm not sure what it is). XOR ciphers are solvable via (plaintext) XOR (ciphertext), meaning if I encrypt the encrypted text using the same key, I should get the original text.

PHP Code:

function test() {
  
temp.XOREncrypt("test string"truenull);
  
temp.XOREncrypt(temp.atruenull);
  echo(
temp.a);
  echo(
temp.b);


Returns:
PHP Code:

5'00e53:-$,
test-strdng 

It seems that it's always 1 letter off.

Correct me if I'm wrong.

coreys 08-17-2008 02:34 AM

Yeah, like I said I had to go around some ASCII characters that can't be displayed. So I replaced all those with "-". So, in that sense, you shouldn't be able to decrypt it. If you stick with ASCII characters by setting the second parameter false you really shouldn't be able to.

LoneAngelIbesu 08-17-2008 02:54 AM

:( Good job, anyways, though. I'm wondering, though, why is it that spaces can't be used? I'm a newbie, so I don't quite understand. I just removed the replacing part of the code, and it encrypts an decrypts just fine; encrypting an encrypted text returns the original text, even with the spaces.

coreys 08-17-2008 03:12 AM

I know, I just didn't like that a lot of the characters wouldn't display correctly.

Loriel 08-17-2008 12:10 PM

Quote:

Originally Posted by coreys (Post 1415038)
I know, I just didn't like that a lot of the characters wouldn't display correctly.

You could use an additional Base64 encoding to make the encrypted string printable.

coreys 08-17-2008 10:21 PM

Quote:

Originally Posted by Loriel (Post 1415088)
You could use an additional Base64 encoding to make the encrypted string printable.

Ah, true, I hadn't thought of that.

PHP Code:

public function XOREncrypt2(skey) {
  if (
key == NULL)
    
key "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  
temp.out "";
  if (
s.length() <= key.length()) {
    for (
i=0i<s.length(); i++) {
      
temp.getascii(s.charat(i)) xor getascii(key.charat(i));
      
temp.out @= char(temp.a);
    }
    return 
base64encode(temp.out);
  }
  else
    return 
"String too long. (Limit " key.length() @ ")";


This seems to work rather well, actually. Short and sweet.


All times are GMT +2. The time now is 07:52 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.