Skip to content

Instantly share code, notes, and snippets.

@arodland
Created July 30, 2010 03:47
Show Gist options
  • Save arodland/499854 to your computer and use it in GitHub Desktop.
Save arodland/499854 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use Crypt::RSA;
use Crypt::RSA::Key::Private::SIvy;
use Data::Dumper;
my $keystr = "RSA.mVgY8RN6URBTstndvmUUPb4UZTdwvwmddSKE5z_jvKUEK6yk1u3rrC9yN8k6FilGj9K0eeUPe2hf4Pj-5CmHww==.AQAB.Lgy_yL3hsLBngkFdDw1Jy9TmSRMiH6yihYetQ8jy-jZXdsZXd8V5ub3kuBHHk4M39i3TduIkcrjcsiWQb77D8Q==";
my $key = Crypt::RSA::Key::Private::SIvy->from_string($keystr);
my $rsa = Crypt::RSA->new;
my $ct = $rsa->encrypt(
Message => "hello world",
Key => $key,
Armour => 1
) or die $rsa->errstr;
print Dumper($ct);
my $pt = $rsa->decrypt(
Cyphertext => $ct,
Key => $key,
Armour => 1
) or die $rsa->errstr;
print Dumper($pt);
package Crypt::RSA::Key::Private::SIvy;
use strict;
use warnings;
use MIME::Base64::URLSafe ();
use Math::BigInt;
use base 'Crypt::RSA::Key::Private';
sub from_string {
my ($class, $string) = @_;
my $self = $class->new;
my ($type, $mod, $exp, $private_exp) = split /\./, $string;
if ($type ne 'RSA' || !defined($mod) || !defined($exp) || !defined($private_exp)) {
die "Unknown key format";
}
$_ = $self->_decode_b64($_) for $mod, $exp, $private_exp;
$self->n($mod);
$self->e($exp);
$self->d($private_exp);
$self->{Checked} = 1;
return $self;
}
sub _decode_b64 {
my ($self, $val) = @_;
my $decoded = MIME::Base64::URLSafe::decode($val);
my $hex = "0x" . join('', map { sprintf "%02x", $_ } unpack "C*", $decoded);
return Math::BigInt->from_hex($hex)->bstr;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment