Click to See Complete Forum and Search --> : Encrypt Regex


Cheater
03-23-2006, 09:43 PM
I have a somewhat sloppy script that uses a bunch of regexes to replace each letter/number/space/linebreak in a file with a randomly generated 3 digit code. Look at the following example:

File: Hi

H=479
I=382
A=793

Encrypted: 479382

The encryption part works fine, but then I want to have it decrypt the file. I have it search and replace the 3 digit number with the letter. This works some of the time (about 20%). But in the example, as you can see, it would pick up the letter A in that encrypted string when it shouldn't be there (not tested). I'm looking for an easy way to check for things like this. The only way I can think of would be to see if the encrpted form of A+B contains the encrypted form of C. I would then do that for every combination. Any ideas?

Nedals
03-23-2006, 10:16 PM
Not terribly elegant, but you could try something like this.

my $code = "123678345";
my @ary = split('',$code);
for (my $i=0; $i<length($code); $i+=3) {
## process the code instead of printing
print join('',@ary[$i,$i+1,$i+2])."\n";
}


Why do you use a bunch of regexes to create the encrypted string...
How about this?

my $chars = " ABCDEFGHIJ"; ## etc
my @code = ('000','111','222','333','444','555','666','777','888','999','123'); #etc

my $text = "A BABE JADE";
my $encrypt = "";
for (my $i=0; $i<length($text); $i++) {
my $k = index($chars,substr($text,$i,1));
$encrypt .= $code[$k];
}

print "$encrypt\n";
exit;

ghostdog74
03-24-2006, 07:41 PM
I have a somewhat sloppy script that uses a bunch of regexes to replace each letter/number/space/linebreak in a file with a randomly generated 3 digit code. Look at the following example:

File: Hi

H=479
I=382
A=793

Encrypted: 479382

The encryption part works fine, but then I want to have it decrypt the file. I have it search and replace the 3 digit number with the letter. This works some of the time (about 20%). But in the example, as you can see, it would pick up the letter A in that encrypted string when it shouldn't be there (not tested). I'm looking for an easy way to check for things like this. The only way I can think of would be to see if the encrpted form of A+B contains the encrypted form of C. I would then do that for every combination. Any ideas?


if you are doing this just for fun, then it should be alright to create your
own encryption method. But if you are doing a full fledge web application and the publish to the public, you should consider using already established encryption algos for "better" security....

Cheater
03-24-2006, 07:48 PM
This is just for fun. That is why I'm trying to keep it simple.

bellefaith
03-25-2006, 10:40 PM
Use mod 3 in a loop so that you know that the 0th, 3rd, 6th etc characters are the beginning of 3-digit sequences.