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.

In reply to Re: Ideas for encryption algorithm developement? by chunlou
in thread Ideas for encryption algorithm developement? by stonecolddevin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.