As mentioned in my [old] “About Me” page, I do a lot of custom encryptions. In this article, I’m going to dive shallowly into the basics of writing a simple encryption, and then provide information regarding how to improve it (that is, without losing any information while still making it reversible.)
First of all, here is the completed code – in case you want to refer back to it while reading. The comments have been removed because they killed the readability, but you can find the script with comments here:
Hackerthreads: Post Your Source! thread
#!/usr/bin/perl
$u=shift;
for ($i = 0; $i < length($u); $i++) {
$c .= ord(substr($u,$i,1));
}
$c="$c$c";
for ($i = 0; $i < length($c); $i++) {
substr($c,$i,1)++;
$i++;
}
for ($i = 0; $i < length($c); $i++) {
substr($c,$i,1) = substr($c,$i-1,1) + substr($c,$i/2,1);
}
for ($i = 0; $i < length($c); $i++) {
for ($j = 0; $j < length($c); $j++) {
if ((substr($c,$i,$j) > 32) && (substr($c,$i,$j) < 128)) {
substr($c,$i,1) = chr(substr($c,$i,$j));
}
}
}
}
print "$c\n";
Before I go through the script, I want to explain ASCII. For each character, (abcd1234!@#, etc.), there is a numeric code associated with it. The ascii equivalent to “A” is 65, while the letter “a” returns 97. To find a list of what each character’s ascii code is and for more information, you can check out the Wikipedia Article.
Now, let’s look at the code. I’ll take this step by step:
Step 1: Input the string to encrypt
$u=shift;
This allows the script to be run from the command line with the string as parameters.
Step 2: Convert each character to ascii
for ($i = 0; $i < length($u); $i++) {
$c .= ord(substr($u,$i,1));
}
With this for loop, we’re just cycling through the length of the string and appending the numeric version of the letter to the end of another string ($c). We’re using another string to avoid an infinite loop, because each single-digit character has at least 2 characters in ascii, which would just keep adding on to the string and we would never reach the end.
Step 3: Double the length of the string
$c="$c$c";
Simply enough, this just takes the string and copies it. “lame” becomes “lamelame” and “moseph sucks the chode” becomes “moseph sucks the chodemoseph sucks the chode”.
Step 4: Increment every other number
for ($i = 0; $i < length($c); $i++) {
substr($c,$i,1)++;
$i++;
}
When writing your own encryptions, you don’t always want to do the same thing to every part of the script; sometimes you want to mix it up a bit. What I’m doing here is just incrementing every single-number in the string. If the string before was “2366927″, it would become “34771038″.
Step 5: Further manipulate the numbers
for ($i = 0; $i < length($c); $i++) {
substr($c,$i,1) = substr($c,$i-1,1) + substr($c,$i/2,1);
}
In this step we’re just continuing to mix around the numbers. Right now we’re adding each number and the previous number and putting the new number in when the current number was.
Step 6: Convert back to a good-looking alphanumeric string
for ($i = 0; $i < length($c); $i++) {
for ($j = 0; $j < length($c); $j++) {
if (substr($c,$i,$j) 32) {
substr($c,$i,1) = chr(substr($c,$i,$j));
}
}
}
}
Of all the steps, this is probably the most difficult to comprehend. (And it shouldn’t be too difficult as long as I explain correctly..) In the first two lines, we’re just entering two for loops: these will allow the substr() function to check for ranges of numbers. The numbers we’re looking for are in the ranges of 32 (a space) and 128. If substr() finds one of the numbers, it will replace that number in the string with the character equivalent chr() and move on. This will successfully create a “random” string of characters, symbols and numbers from the input string.
Improvements
Step 4: In step 4, I want to point out that although the method of adding a number and it’s successor may be good, sometimes you’ll want to be more creative. For example, if someone were writing an encryption for, say, a wireless API for a laptop used by the President of your company, you might want to have a little extra security. There are plenty of ways to be creative, so I won’t cramp into your brain with more than one.
Think of this: Someone has gained access to your president’s laptop and is either keylogging or sniffing out his password (which in this example, is “lolwutomgwtfbbq”). The intruder also has a copy of the program, which – now that he knows the president’s password – he can use to log in and steal all the president’s child pornography. If you were to implement security measures specific to that computer, like the computer name or mac address, you could use that in the cipher however you wish.
There are also different alternatives on how you can do step 6 (or any other step, it’s all up to the programmer).
This encryption’s output:
|
drusepth@drusepth0:~/Desktop/myCrypt$ perl 001enc.pl d 2QQ1[{23 drusepth@drusepth0:~/Desktop/myCrypt$ perl 001enc.pl dr drusepth@drusepth0:~/Desktop/myCrypt$ perl 001enc.pl dru drusepth@drusepth0:~/Desktop/myCrypt$ perl 001enc.pl drus drusepth@drusepth0:~/Desktop/myCrypt$ perl 001enc.pl druse drusepth@drusepth0:~/Desktop/myCrypt$ perl 001enc.pl drusep drusepth@drusepth0:~/Desktop/myCrypt$ perl 001enc.pl drusept drusepth@drusepth0:~/Desktop/myCrypt$ perl 001enc.pl drusepth |