Inspired by tye's comments and by the general lack of a module to do RSA encryption/decryption using public and private keys, I came up with this. It's based loosely on the obfuscated versions, but it's clearer and faster.

It's faster in particular if you have Math::GMP installed, because that's *way* faster than Math::BigInt. But it falls back to the latter if the former isn't installed.

I'll probably upload to CPAN after writing some docs. Thoughts?

package Crypt::RSA; use strict; use Carp qw( croak ); use vars qw( $INT_CLASS ); BEGIN { my @err; for my $mod (qw( Math::GMP Math::BigInt )) { eval "use $mod;"; $INT_CLASS = $mod, last unless $@; push @err, $@; } die "Can't load a big integer class:\n@err" unless $INT_CLASS; } sub new { my($class, $key, $n) = @_; croak "usage: ", __PACKAGE__, "->new(\$key, \$n)" unless $key && $n; $key = "0$key" if length($key) % 2; ($key = unpack('B*', pack('H*', $key))) =~ s/^0*//; my @k = split //, $key; my $x = $INT_CLASS->new(0); $x = ($x*16) + hex $1 while $n =~ /(.)/g; bless { k => \@k, n => $x, n_len => length($n) }, $class; } sub encrypt { $_[0]->crypt($_[1], 0) } sub decrypt { $_[0]->crypt($_[1], 1) } sub crypt { my($rsa, $data, $d) = @_; ## Determine sizes of blocks that we read and write, respectively. ## $br is the size of the blocks we read; $bw is the size of the ## blocks we write. This depends on whether we're encrypting or ## decrypting ($d). Both values must be smaller than N, ## the RSA modulus. my $br = ((2*$d-1 + $rsa->{n_len})&~1)/2; my $bw = $br + 1 - 2*$d; my($offset, $len, $whole) = (0, length $data); while ($offset < $len) { my $chunk = substr $data, $offset, $br; $whole .= _crypt_chunk($chunk, $rsa->{k}, $rsa->{n}, $br, $bw) +; $offset += $br; } $whole; } sub _crypt_chunk { my($chunk, $k, $n, $br, $bw) = @_; $chunk = substr $chunk . "\0"x$br, 0, $br; # Pad with nulls. my $cl = $INT_CLASS->new(0); $cl = ($cl * 256) + ord $1 while $chunk =~ /(.)/sg; my $res = $INT_CLASS->new(1); for my $b (@$k) { $res = ($res * $res) % $n; $res = ($res * $cl) % $n if $b; } my($text, $t) = (""); for (1..$bw) { ($res, $t) = ($res/256, $res%256); $text = pack("C", $t) . $text; } $text; }

In reply to Crypt::RSA: RSA encryption/decryption by btrott

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.