Tuesday, March 25, 2008

Simplified one time pad encoder/decoder

This simplified one-time-pad script will only work with letters for ease of physical transmission. (that is to say; no numbers or symbols, only letters of the alphabet)

It uses a simple XOR to generate the encrypted text. As usual, the unbeatable strength of this type of encryption depends ENTIRELY on the randomness of the key used, and that a key NEVER be reused. For more information on this potentially unbreakable form of encryption, and tips on how to use it, check my earlier post.

For ease of translation it is recommended that keys be entered one line at a time, and messages be decrypted one line at a time. This makes it easier to keep track of the lines used.

EXAMPLE:
If your key is formatted thusly;
ARVVHALWIREUGAHLIUVHPORUBAHWOVIUHSL
DKVJBWPEVOUJPEQWJFNSJKDVNPWEUIVAAOC
EUGILUSARWGRWIEUHPVOASKDUVAWRIEVAUH


Format your message to match;
PINEAPPLESAREFARTOODELICIOUSFORYOUX
DISREGARDPREVIOUSMESSAGEDUETOEXCESS
IVEUSEOFALCOHOLQXQGBUWREWIOWJKLAGNA


Then encrypt it while keeping the formatting.

Here's the script;

#!/usr/bin/perl

my ($mode,$count,$result);
$mode=shift;

if($mode eq "-d"){print "decrypting mode\n";}
elsif($mode eq "-e"){print "encrypting mode\n";}
else{print "Use -e to encrypt, and -d to decrypt\n";exit;}

my %alpha = (
A => '1', B => '2', C => '3', D => '4', E => '5',F => '6',
G => '7', H => '8', I => '9', J => '10', K => '11', L => '12',
M => '13', N => '14', O => '15', P => '16', Q => '17', R => '18',
S => '19', T => '20', U => '21', V => '22', W => '23', X => '24',
Y => '25', Z => '26'
);
my @alpha=qw(A B C D E F G H I J L M N O P Q R S T U V W X Y Z);

print "input your message: ";
chomp($msg=<stdin>);
print "input your key: ";
chomp($key=<stdin>);
my @msg=split('',$msg);
my @key=split('',$key);

print "translated message:\n";

for(@msg){
$count+='1';
if($mode eq "-e"){
$result=($alpha{$_} + $alpha{@key[($count-1)]});
if($result>26){$result-=26;}
}
if($mode eq "-d"){
$result=($alpha{$_} - $alpha{@key[($count-1)]});
if($result<1){$result+=26;}
}
print @alpha[($result-1)];
}
print "\n";

Just drag-select everything in the box, copy, and paste into a wider text editor like notepad or gedit. Don't worry, it preserves newlines.



Instructions
To run in teh loonix, just save it and run it in a terminal as "perl file_you_saved"
To run in windows, download and install activeperl (free) and save it as a .pl file. Then run it in a command prompt (start->run->cmd->[enter]).

Use switches to run a decrypt (-d) or an encrypt (-e)
file_you_saved.pl -e
file_you_saved.pl -d



Security
Keys should be long and random for many uses. They should also be given to the recipient in person, or via mail on paper media, and kept in a very secure location. Once the key has been given, encrypted messages can be sent via insecure methods like e-mail. I originally recommended NOT using your computer, everyone has their own level of security, and if you feel your computer is 100% secure, you're wrong. Instead, download and burn a copy of a live linux distribution (such as Knoppix). This CD will boot on your computer, and make NO CHANGES to your existing operating system. (Your Windows XP will be untouched. Just remove the CD and reboot, and you're back.) Boot it using the "toram" (to ram) function, which will ensure that all data is written ONLY to ram, which wipes all data completely, and irreversibly upon power down. Load the script via a usb key (you unplugged the network cable right?), and run at your leisure, encode/decode, and turn the computer off (that's OFF, not reboot), and have a coke. You've earned it. This method of computer work ensures complete computer security. Physical security, however, is up to you, so make sure no one is shoulder surfing. Maybe hide under a thick blanket. Or a tinfoil fort. :)

2 comments:

Anonymous said...

Thanks again, I'll never use it, but it sure is cool.

Fletch said...

Good to have, but may you never have to use it.

(along with your self-defense firearm, bullet-proof vest, fifth amendment, etc)