Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 08-17-2008, 12:24 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
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.
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
  #2  
Old 08-17-2008, 12:35 AM
xXziroXx xXziroXx is offline
Master of Puppets
xXziroXx's Avatar
Join Date: May 2004
Location: Sweden
Posts: 5,288
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Send a message via AIM to xXziroXx Send a message via MSN to xXziroXx
Nice one.
__________________

"A delayed game is eventually good, but a rushed game is forever bad." - Shigeru Miyamoto
Reply With Quote
  #3  
Old 08-17-2008, 12:50 AM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Quote:
Originally Posted by coreys View Post
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.
Reply With Quote
  #4  
Old 08-17-2008, 12:56 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Quote:
Originally Posted by DrakilorP2P View Post
Try all possible keys until you find one that works. Message decrypted.
I'm fairly certain it's impossible to reverse the xor operator.
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
  #5  
Old 08-17-2008, 01:29 AM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Quote:
Originally Posted by coreys View Post
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.
Reply With Quote
  #6  
Old 08-17-2008, 02:15 AM
LoneAngelIbesu LoneAngelIbesu is offline
master of infinite loops
LoneAngelIbesu's Avatar
Join Date: May 2007
Location: Toldeo, Ohio
Posts: 1,049
LoneAngelIbesu has a spectacular aura aboutLoneAngelIbesu has a spectacular aura about
Send a message via AIM to LoneAngelIbesu
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.
__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan
Reply With Quote
  #7  
Old 08-17-2008, 02:34 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
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.
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
  #8  
Old 08-17-2008, 02:54 AM
LoneAngelIbesu LoneAngelIbesu is offline
master of infinite loops
LoneAngelIbesu's Avatar
Join Date: May 2007
Location: Toldeo, Ohio
Posts: 1,049
LoneAngelIbesu has a spectacular aura aboutLoneAngelIbesu has a spectacular aura about
Send a message via AIM to LoneAngelIbesu
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.
__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan
Reply With Quote
  #9  
Old 08-17-2008, 03:12 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
I know, I just didn't like that a lot of the characters wouldn't display correctly.
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
  #10  
Old 08-17-2008, 12:10 PM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
Quote:
Originally Posted by coreys View Post
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.
Reply With Quote
  #11  
Old 08-17-2008, 10:21 PM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Quote:
Originally Posted by Loriel View Post
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.
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:20 AM.


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