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

1

8

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?

Pak answered 4/10, 2020 at 1:25 Comment(5)
It appears to expect the hex of bytes on STDIN, along with some parameters: -n=... (a string), -d=... (an integer) and -k=... (?). I don't know what values are expected for those parameters. The gruntwork is done by the dc command-line utility.Retroversion
From comments on another version, I'm guessing that $n is the modulus (probably in hex), $k is the exponent (probably in hex) and $d indicates whether which a decryption or encryption operation should be performed (0 or 1???)Retroversion
Re "Does the program support key generation as well", No.Retroversion
Note that there have been multiple variants of this, and although the original (and therefore most historically significant) one doesn't have a direct explanation, in story2.html there is a description of other ones using dc... Note that this seems to be plaintext RSA, so hey, secure it is not.Excise
there is a section How this program works here: cypherspace.org/adam/rsa did you read it already?Tessler
L
0

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

Luxe answered 3/8 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.