RSA code: Difference between revisions
→{{header|J}}: Perl 6 entry
m (Should be a task, shouldn't it?) |
(→{{header|J}}: Perl 6 entry) |
||
Line 221:
Note: as indicated at http://www.jsoftware.com/help/dictionary/special.htm, <code>N&|@^</code> does not bother with creating the exponential intermediate result.
=={{header|Perl 6}}==
No blocking here.
<lang perl6>constant $n = 9516311845790656153499716760847001433441357;
constant $e = 65537;
constant $d = 5617843187844953170308463622230283376298685;
my $secret-message = "ROSETTA CODE";
package Message {
my @alphabet = 'A' .. 'Z', ' ';
my %code = @alphabet »=>» 0 .. *;
subset Text of Str where /^^<@alphabet>+$$/;
our sub encode(Text $t) {
reduce * * ^@alphabet + *, %code{$t.comb};
}
our sub decode(Int $n is copy) {
@alphabet[
gather loop {
take $n % @alphabet;
last if $n < @alphabet;
$n div= +@alphabet;
}
].join.flip;
}
}
use Test;
plan 1;
say "Secret message is $secret-message";
say "Secret message in integer form is $_" given
my $numeric-message = Message::encode $secret-message;
say "After exponentiation with public exponent we get: $_" given
my $numeric-cipher = expmod $numeric-message, $e, $n;
say "This turns into the string $_" given
my $text-cipher = Message::decode $numeric-cipher;
say "If we re-encode it in integer form we get $_" given
my $numeric-cipher2 = Message::encode $text-cipher;
say "After exponentiation with SECRET exponent we get: $_" given
my $numeric-message2 = expmod $numeric-cipher2, $d, $n;
say "This turns into the string $_" given
my $secret-message2 = Message::decode $numeric-message2;
is $secret-message, $secret-message2, "the message has been correctly decrypted";</lang>
{{out}}
<pre>1..1
Secret message is ROSETTA CODE
Secret message in integer form is 97525102075211938
After exponentiation with public exponent we get: 8326171774113983822045243488956318758396426
This turns into the string ZULYDCEZOWTFXFRRNLIMGNUPHVCJSX
If we re-encode it in integer form we get 8326171774113983822045243488956318758396426
After exponentiation with SECRET exponent we get: 97525102075211938
This turns into the string ROSETTA CODE
ok 1 - the message has been correctly decrypted</pre>
=={{header|PicoLisp}}==
|