This is fast and simple realization for encrypt/decrypt RSA.
Require Math::Pari. This is only encrypt/decrypt without keys generation. I'm use OpenSSL for key generation:
openssl genrsa 1024 | openssl rsa -text >RSA.keys
And this code encrypt/decrypt only numeric messages.
{ use Math::Pari qw(PARI Mod lift); my ($n, $e, $d);
sub RSA_encrypt { lift Mod($_[0], $n) ** $e }
sub RSA_decrypt { lift Mod($_[0], $n) ** $d }
sub RSA_read_keys {
my ($filename) = @_;
local (*F, $/);
open F, $filename or die $!; my $keys=<F>; close F;
my @n = map{int hex}split /[\s:]+/,
join"",$keys=~/modulus:\n((?:\s.*\n)+)/;
$e = join"",$keys=~/publicExponent: (\d+).*\n/;
my @d = map{int hex}split /[\s:]+/,
join"",$keys=~/privateExponent:\n((?:\s.*\n)+)/;
my $base = PARI 256; $n = PARI 0; $d = PARI 0;
$n += ($base ** $#n) * shift @n while @n;
$d += ($base ** $#d) * shift @d while @d;
}}
print "Reading keys...\n";
RSA_read_keys("RSA.keys");
$msg = "1020304050";
print "Encrypting: $msg\n";
$enc = RSA_encrypt($msg);
print "Encrypted: $enc\n";
$dec = RSA_decrypt($enc);
print "Decrypted: $dec\n";
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.