sub rsa { #RSA (de)crypt function (message,modulus,exponent)
my ($msg,$m,$e) = @_;
- ($msg**$e)%($m);
+ ($msg**$e)%($m); #TODO: reimplement using chinese remainder theorem (this is too slow!!!)
}
sub isprime { #Tell if number is prime or not (suboptimal)
print "=== KEYS ===\nPUB: ($n,$e)\nPRV: ($n,$d)\n";
my $msg=1337;
+$msg=$n-1;
print "=== TEST ===\nMSG: $msg\n";
my $enc=rsa($msg,$n,$e); #encrypt
print "ENC: $enc\n";
my $dec=rsa($enc,$n,$d); #decrypt
- print "DEC: $dec\nTST: ".$msg.($msg==$dec?"=":"!=").$dec."\n";
+ $test=($msg==$dec);
+ print "DEC: $dec\nTST: ".$msg.($test?"=":"!=").$dec."\n";
+ exit !$test;