Árni Dagur
Árni Dagur

Reputation: 89

How do I use the "three line version of the RSA algorithm" written in Perl?

The book "An Introduction to Mathematical Cryptography" by J. Hoffstein et al. talks about a three-line implementation of the RSA algorithm in Perl, which people used to protest US government censorship of cryptography:

To protest the government's policy, people wrote a three line version of the RSA algorithm in a programming language called perl and printed it on tee shirts and soda cans, thereby making these products into munitions. In principle, wearing an "RSA enabled" tee shirt on a flight from New York to Europe subjected the wearer to a large fine and a 10 year jail term.

I looked online for the actual Perl program, and found it here: http://www.cypherspace.org/rsa/story.html.

#!/bin/perl -s-- -export-a-crypto-system-sig -RSA-3-lines-PERL
$m=unpack(H.$w,$m."\0"x$w),$_=`echo "16do$w 2+4Oi0$d*-^1[d2%Sa
2/d0<X+d*La1=z\U$n%0]SX$k"[$m*]\EszlXx++p|dc`,s/^.|\W//g,print
pack('H*',$_)while read(STDIN,$m,($w=2*$d-1+length$n&~1)/2)

The above is equivalent to the following:

#!/bin/perl -s --

$w = ( 2 * $d - 1 + length($n) ) & ~1;

while (read(STDIN, $m, $w/2)) {
   $m = unpack(H.$w, $m.("\0"x$w));
   $_ = `echo "16do$w 2+4Oi0$d*-^1[d2%Sa2/d0<X+d*La1=z\U$n%0]SX$k"[$m*]\EszlXx++p | dc`;
   s/^.|\W//g;
   print pack('H*', $_);
}

My question is: How would I use this program to encrypt and later decrypt a piece of data? Does the program support key generation as well, or do I need to have a key already?

Upvotes: 8

Views: 1282

Answers (1)

yvan allioux
yvan allioux

Reputation: 1

I have implemented the Perl program using Docker to make it easy to test. My setup includes everything needed to generate keys and encrypt messages. You can find the details and the Docker setup at the following link: https://github.com/yvan-allioux/RSA-in-3-lines-of-perl

1 create the keys and convert them into an acceptable format for the perl script with my bash script 2 run the script in a container so you don't have to install perl

Upvotes: 0

Related Questions