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?

ikegami
  • 367,544
  • 15
  • 269
  • 518
Árni Dagur
  • 89
  • 1
  • 2
  • 1
    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. – ikegami Oct 04 '20 at 02:09
  • 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???) – ikegami Oct 04 '20 at 02:26
  • Re "*Does the program support key generation as well*", No. – ikegami Oct 04 '20 at 02:29
  • 1
    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. – Maarten Bodewes Oct 04 '20 at 11:54
  • 2
    there is a section How this program works here: http://www.cypherspace.org/adam/rsa/ did you read it already? – mabe02 Oct 17 '20 at 21:12

0 Answers0