#! /usr/local/bin/perl -w use strict; # ----------------------------------------------------------- # RSA algorithm -- assymetrical\public-key cryptography # ----------------------------------------------------------- use Math::Pari qw(gcd PARI) ; # ----------------------------------------------------------- # m -- message my \$m = 'Perl' ; print "original: \$m\n" ; my \$tmpl = 'C*' ; my @m = unpack(\$tmpl, \$m) ; # string -> unsigned char values print "coded: @m\n" ; # n = pq -- in RSA, p & q = prime, each 1024 bits/308 digits long my \$p = PARI("prime(".int(rand 50).")") ; my \$q = PARI("prime(".int(rand 50).")") ; my \$n = \$p*\$q ; # \$n = Pari's obj # choose a random number r, s.t. # 1 < r < (p-1)(q-1) = b # gcd(r, b) = 1 -- relative prime my \$b = (\$p-1)*(\$q-1) ; my \$r ; do {\$r = int rand \$b ; } until (gcd(\$r,\$b) == 1) ; \$r = PARI \$r ; # rk = 1 mod (p-1)(q-1) -- k = private key; (n, r) public my \$k = (1/\$r)%\$b ; # Pari's math operators, since vars = Pari # encrypt -- c = (m ^ r) mod n my @c ; map { \$c[\$_] = (\$m[\$_]**\$r)%\$n } 0..\$#m ; # Perl: ** for power print "ciphered: @c\n" ; # decrypt -- m = (c ^ k) mod n my @d ; map { \$d[\$_] = PARI("(\$c[\$_]^\$k)%\$n") } 0..\$#c ; # Pari: ^ for power print "deciphered: @d\n" ; print "decoded: " . pack(\$tmpl, @d) . "\n" ; __END__ original: Perl coded: 80 101 114 108 ciphered: 18431 6512 5843 7236 deciphered: 80 101 114 108 decoded: Perl