x10Hosting Forums

Corporate Free Hosting for the Masses.



Register

Reply
 
LinkBack Thread Tools Display Modes
x10Hosting Member

Join Date: Nov 2007
Posts: 24
Credits: 658
cerbere is on a distinguished road
Quote  
09-21-2008, 08:06 PM
I need a simple encryption algorithm

Hi folks,

I want to encrypt a short (10-100 chars) string into a new
string of the same length, using a key of, say, 8 chars,
then decrypt it using the same key.

The original string and key contain only hexadecimal
characters ("0".."9", "A".."F"). So should the encrypted
string too.

I don't need high security, just a plain hacker- and robot-
proof barrier.

I don't want to use a package or module, I prefer writing
it myself (with your help if possible) :>) The encryption
and decryption should probably fit inside 40 lines of code.

Target language : Perl and/or PHP.
Pseudo-code welcome if readable !

Any ideas ? Thanks in advance.
Reply With Quote
cerbere is offlineReport Post
x10 Lieutenant

shaunak's Avatar

Join Date: Sep 2007
Posts: 307
Credits: 4,361
shaunak is on a distinguished road
Location: 3rd rock from the sun

Quote  
09-21-2008, 11:11 PM
Re: I need a simple encryption algorithm

If you want something really simple to throw off bots, try the ROT13 algorithm.
http://en.wikipedia.org/wiki/ROT13

Logic:
if ( (int)(char>65 &&(int)char<=78 ) || ( (int)(char>97 &&(int)char<=110) )
{ char=char+13; }

else if ( (int)(char>78 &&(int)char<=90 ) || ( (int)(char>110 &&(int)char<=123) )
{ char = char-13; }

else{
char=char; // If you want to delete special chars, mod here.
}
__________________

Last edited by shaunak; 09-21-2008 at 11:12 PM.
Reply With Quote
shaunak is offlineReport Post
Account Manager

sunils's Avatar

Join Date: Jan 2008
Posts: 2,023
Credits: 30,523
sunils has a reputation beyond repute
Location: Chennai ,India

Send a message via AIM to sunils Send a message via Yahoo to sunils
Quote  
09-22-2008, 08:38 AM
Re: I need a simple encryption algorithm

If you want a simple one, then this can be used.

consider if your word is
hello
i will take 4 as the key,
no shift it..
It becomes
lipps
ie h->l, e->i,l->p,o->s 4th alphabet after the current one. This is the simplest of all.
__________________
Sunil Sankar
-------------------------------------------------------------------------
Account Manager

Reply With Quote
sunils is offlineReport Post
x10Hosting Member

Join Date: Nov 2007
Posts: 24
Credits: 658
cerbere is on a distinguished road
Quote  
09-22-2008, 10:55 AM
Re: I need a simple encryption algorithm

Thanks shaunak and sunils for your suggestions.

To see why I need that encryption, you can look at

http://ixedix.x10hosting.com/cgi-bin...CCCardCheck.pl

and at the 11th line of the page source. The hex string
contains (in the last 4 digits) the locations of the correct
numbers, and the digit before is the desired suit. This is
the string I want to encrypt before sending it via GET or
POST (last line).
Reply With Quote
cerbere is offlineReport Post
x10 Lieutenant

xPlozion's Avatar

Join Date: Mar 2008
Posts: 393
Credits: 6,156
xPlozion will become famous soon enough
Location: Newark, DE

Send a message via AIM to xPlozion
Quote  
09-22-2008, 10:42 PM
Re: I need a simple encryption algorithm

well, if you're just trying to hash a simple captcha image, then str_replace with a simple hash database should do just fine .

also, by the looks of it, you've just added a string to the end of the code. although that may work, the rest of the captcha code is unencrytped and could be programmed to remove the last 5 characters of the string or to just retrieve the set number that you need to pass, then submit the return value.

PHP Code:
<?php
function simpleHash($str,$way=1)
{
  
// $way == 1 // hash (redundant, and it's just to prevent errors)
  // $way == 2 // unhash (needs to be set as 2 after your string.  if it's empty, it'll get the value of 1)

  
$unenc = array('0','1','2','3','4','5','6','7','8','9');
  
$hash  = array('y','e','w','i','t','b','q','d','j','p');
  if (
$way == 1)
    return 
str_replace($unenc,$hash,$str);
  elseif (
$way == 2)
    return 
str_replace($hash,$unenc,$str);
}

echo 
simpleHash('194981842156');
// would return 'eptpjejtwebq'

echo simpleHash('eptpjejtwebq',2); // if there's no 2 at the end, it would try to rehash this string instead of unhash it.
// would return '194981842156'
?>
-xP
__________________


STi · Subaru Tecnica International · STi

CheaterCheater - My Game Site


Last edited by xPlozion; 09-22-2008 at 10:46 PM.
Reply With Quote
xPlozion is offlineReport Post
x10 Sophmore

xmakina's Avatar

Join Date: May 2008
Posts: 239
Credits: 2,077
xmakina will become famous soon enough
Location: England

Send a message via MSN to xmakina
Quote  
09-23-2008, 11:16 AM
Re: I need a simple encryption algorithm

It's not an encryption, but if you're just using it for a Captcha might I recommend you investigate PHP Sessions? They're exactly what you need to make a captcha work without revealing anything on the webpage.
Reply With Quote
xmakina is offlineReport Post
x10Hosting Member

Join Date: Nov 2007
Posts: 24
Credits: 658
cerbere is on a distinguished road
Quote  
09-24-2008, 04:13 AM
Re: I need a simple encryption algorithm

Here is a method I devised that fits my needs (it must have been
around since ENIAC). I XOR the key with packets of hex digits
from the the input string, using the binary value of the digits
(not the ASCII code). The hexcrypt function does both encryp-
tion and decryption.

Perl code :

Code:
$string = "0123456789ABCDEFBEBEBA0BAB0123CACA"; # any length
$keystr = "948D5E5"; # length <= 8

print "original  : \"$string\"   key = \"$keystr\"\n";
$enc = hexcrypt($string, $keystr);
print "encrypted : \"$enc\"\n";
$dec = hexcrypt($enc, $keystr);
print "decrypted : \"$dec\"\n";

sub hexcrypt
{
  my (@pac, $pacstr, $l, $pac, $pacenc, $format, $pacencstr);
  my @string = split(//, @_[0]); # the string to encrypt
  my $keystr = @_[1]; # the key
  my $key = hex($keystr); # the key in binary
  my $keylength = length($keystr);
  my $encstr = ""; # accumulator for returned string
  do
  {   
    @pac = splice(@string, 0, $keylength); # take out a packet of digits (or less at end)
    $pacstr = join("", @pac); # into a string
    $l = length($pacstr); 
    $pac = hex($pacstr); # convert to a 32-bit integer
    $pacenc = $pac ^ ($key >> ($keylength * 4 - 4 * $l)); # shift key right if $l < $keylength, and XOR
    $format = "%0" . sprintf("%.d", $l) . "X"; # we want the leading zeros
    $pacencstr = sprintf($format, $pacenc); # back into a string
    $encstr .= $pacencstr; # add to encrypted string
  }
  while scalar @string > 0;
return $encstr;  
}
Program output :

Code:
original  : "0123456789ABCDEFBEBEBA0BAB0123CACA"   key = "948D5E5"
encrypted : "95AE1B3EC17E287B33E0E3437EE4B74794"
decrypted : "0123456789ABCDEFBEBEBA0BAB0123CACA"

Last edited by cerbere; 09-24-2008 at 04:32 AM.
Reply With Quote
cerbere is offlineReport Post
Account Manager

sunils's Avatar

Join Date: Jan 2008
Posts: 2,023
Credits: 30,523
sunils has a reputation beyond repute
Location: Chennai ,India

Send a message via AIM to sunils Send a message via Yahoo to sunils
Quote  
09-24-2008, 04:35 AM
Re: I need a simple encryption algorithm

Congrats... There are lot of methods for encryption and decryptions. We have to choose the one which is afforable and the needed for our web application.
__________________
Sunil Sankar
-------------------------------------------------------------------------
Account Manager

Reply With Quote
sunils is offlineReport Post
Reply

Tags
encryption, perl, php

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
earn money,very simple beandab Earning Money 0 07-13-2008 07:26 PM
Simple Ways to Build Trust in Your Website tittat Tutorials 13 04-10-2008 12:52 PM
Two very simple php file upload scripts-Free Symbian.Ankit Scripts & 3rd Party Apps 2 02-20-2008 10:55 AM
simple script swirly Scripts & 3rd Party Apps 10 06-17-2006 07:32 PM
New Simple Sig phenetic Graphics & Webdesign 2 02-23-2005 09:29 AM


All times are GMT -5. The time now is 07:14 PM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

Per Insurance | MySpace Images | Charmed News | Advertising | Home Loan