in reply to Ideas for encryption algorithm developement?

PARI is the math library commonly used by crytographers, which Perl has direct interface to.

RSA is an easy to implement algorithm to begin with.

Here two examples of the same thing:
#! /usr/local/bin/perl -w use strict; # -------------------------------------------------------------- # the essence of RSA algorithm -- assymetrical\public-key cryptogr +aphy # -------------------------------------------------------------- use Math::Pari qw(gcd PARI) ; # -------------------------------------------------------------- # m -- message my $m = 'IBM' ; 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) -- d = private key; (n, r) public my $k = (1/$r)%$b ; # the math operators are Pari's, since vars + = Pari # encrypt -- c = (m ^ r) mod n my @c ; map { $c[$_] = ($m[$_]**$r)%$n } 0..$#m ; # Perl uses ** fo +r power print "ciphered: @c\n" ; # decrypt -- m = (c ^ k) mod n my @d ; map { $d[$_] = PARI("($c[$_]^$k)%$n") } 0..$#c ; # Pari uses ^ for + power print "deciphered: @d\n" ; print "decoded: " . pack($tmpl, @d) . "\n" ; # just checking.... print "\nprimes: $p, $q\n" ; print "n: $n; (p-1)(q-1): $b\n" ; print "rand: $r; key: $k\n"
#! /usr/local/bin/perl -w use strict; # -------------------------------------------------------------------- +------ # the essence of RSA algorithm -- assymetrical\public-key cryptogr +aphy # -------------------------------------------------------------------- +------ use Math::Pari qw(gcd PARI) ; my $msg = 'ibm%^&*"<>`' ; my $cipher = cipher->new() ; my @c = $cipher->cipher($msg) ; print "@c\n" ; print $cipher->decipher(\@c) . "\n" ; { package cipher ; use strict ; use Math::Pari qw(gcd PARI) ; my ($int, $p, $q, $n, $b, $tmpl) ; # declared here, or won' +t share BEGIN { $int = 40 ; $p = PARI("prime(".int(rand $int).")") ; # Pari: prime(n) - +- the n-th prime $q = PARI("prime(".int(rand $int).")") ; $n = $p*$q ; $b = ($p-1)*($q-1) ; # s.t. 1 < e < (p-1)(q-1), gcd(r, b) = + 1 $tmpl = 'C*' ; # template for pack, unpack } sub new { my $class = shift ; my $self = {} ; # - - - - - - - - - - - - - - - - - - - - - - - - - - do {$self->{e} = int rand $b ; } until (gcd($self->{e},$b)==1) + ; $self->{e} = PARI $self->{e} ; # public key, along with + $n $self->{d} = (1/$self->{e})%$b ; # private key # - - - - - - - - - - - - - - - - - - - - - - - - - - bless($self, ref($class) || $class) ; return $self ; } sub cipher { my $self = shift ; my @m = unpack($tmpl, shift) ; my @c ; map { $c[$_] = ($m[$_]**$self->{e})%$n } 0..$#m ; # + encrypt -- c = (m ^ e) mod n return @c ; } sub decipher { my $self = shift ; my @c = @{shift(@_)} ; my @d ; map { $d[$_] = ($c[$_]**$self->{d})%$n } 0..$#c ; # + decrypt -- m = (c ^ d) mod n return pack($tmpl, @d) ; } }

There is a book, I think, called "In Codes" about a teenager girl's (from Ireland?) journey of coming up with some groundbreaking encryption algorithm. Fun to read.

And, yah, elliptic curve is a hot topic, which there is an easy to follow tutorial at certicom.