September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
38
Task/RSA-code/Ada/rsa-code.ada
Normal file
38
Task/RSA-code/Ada/rsa-code.ada
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
WITH GMP, GMP.Integers, Ada.Text_IO, GMP.Integers.Aliased_Internal_Value, Interfaces.C;
|
||||
USE GMP, Gmp.Integers, Ada.Text_IO, Interfaces.C;
|
||||
|
||||
PROCEDURE Main IS
|
||||
FUNCTION "+" (U : Unbounded_Integer) RETURN Mpz_T IS (Aliased_Internal_Value (U));
|
||||
FUNCTION "+" (S : String) RETURN Unbounded_Integer IS (To_Unbounded_Integer (S));
|
||||
FUNCTION Image_Cleared (M : Mpz_T) RETURN String IS (Image (To_Unbounded_Integer (M)));
|
||||
N : Unbounded_Integer := +"9516311845790656153499716760847001433441357";
|
||||
E : Unbounded_Integer := +"65537";
|
||||
D : Unbounded_Integer := +"5617843187844953170308463622230283376298685";
|
||||
Plain_Text : CONSTANT String := "Rosetta Code";
|
||||
M, M_C, M_D : Mpz_T;
|
||||
-- We import two C functions from the GMP library which are not in the specs of the gmp package
|
||||
PROCEDURE Mpz_Import
|
||||
(Rop : Mpz_T; Count : Size_T; Order : Int; Size : Size_T; Endian : Int;
|
||||
Nails : Size_T; Op : Char_Array);
|
||||
PRAGMA Import (C, Mpz_Import, "__gmpz_import");
|
||||
|
||||
PROCEDURE Mpz_Export
|
||||
(Rop : OUT Char_Array; Count : ACCESS Size_T; Order : Int; Size : Size_T;
|
||||
Endian : Int; Nails : Size_T; Op : Mpz_T);
|
||||
PRAGMA Import (C, Mpz_Export, "__gmpz_export");
|
||||
BEGIN
|
||||
Mpz_Init (M);
|
||||
Mpz_Init (M_C);
|
||||
Mpz_Init (M_D);
|
||||
Mpz_Import (M, Plain_Text'Length + 1, 1, 1, 0, 0, To_C (Plain_Text));
|
||||
Mpz_Powm (M_C, M, +E, +N);
|
||||
Mpz_Powm (M_D, M_C, +D, +N);
|
||||
Put_Line ("Encoded plain text: " & Image_Cleared (M));
|
||||
DECLARE Decrypted : Char_Array (1 .. Mpz_Sizeinbase (M_C, 256));
|
||||
BEGIN
|
||||
Put_Line ("Encryption of this encoding: " & Image_Cleared (M_C));
|
||||
Mpz_Export (Decrypted, NULL, 1, 1, 0, 0, M_D);
|
||||
Put_Line ("Decryption of the encoding: " & Image_Cleared (M_D));
|
||||
Put_Line ("Final decryption: " & To_Ada (Decrypted));
|
||||
END;
|
||||
END Main;
|
||||
63
Task/RSA-code/Perl/rsa-code.pl
Normal file
63
Task/RSA-code/Perl/rsa-code.pl
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use bigint;
|
||||
|
||||
$n = 9516311845790656153499716760847001433441357;
|
||||
$e = 65537;
|
||||
$d = 5617843187844953170308463622230283376298685;
|
||||
|
||||
package Message {
|
||||
my @alphabet;
|
||||
push @alphabet, $_ for 'A' .. 'Z', ' ';
|
||||
my $rad = +@alphabet;
|
||||
$code{$alphabet[$_]} = $_ for 0..$rad-1;
|
||||
|
||||
sub encode {
|
||||
my($t) = @_;
|
||||
my $cnt = my $sum = 0;
|
||||
for (split '', reverse $t) {
|
||||
$sum += $code{$_} * $rad**$cnt;
|
||||
$cnt++;
|
||||
}
|
||||
$sum;
|
||||
}
|
||||
|
||||
sub decode {
|
||||
my($n) = @_;
|
||||
my(@i);
|
||||
while () {
|
||||
push @i, $n % $rad;
|
||||
last if $n < $rad;
|
||||
$n = int $n / $rad;
|
||||
}
|
||||
reverse join '', @alphabet[@i];
|
||||
}
|
||||
|
||||
sub expmod {
|
||||
my($a, $b, $n) = @_;
|
||||
my $c = 1;
|
||||
do {
|
||||
($c *= $a) %= $n if $b % 2;
|
||||
($a *= $a) %= $n;
|
||||
} while ($b = int $b/2);
|
||||
$c;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
my $secret_message = "ROSETTA CODE";
|
||||
|
||||
$numeric_message = Message::encode $secret_message;
|
||||
$numeric_cipher = Message::expmod $numeric_message, $e, $n;
|
||||
$text_cipher = Message::decode $numeric_cipher;
|
||||
$numeric_cipher2 = Message::encode $text_cipher;
|
||||
$numeric_message2 = Message::expmod $numeric_cipher2, $d, $n;
|
||||
$secret_message2 = Message::decode $numeric_message2;
|
||||
|
||||
print <<"EOT";
|
||||
Secret message is $secret_message
|
||||
Secret message in integer form is $numeric_message
|
||||
After exponentiation with public exponent we get: $numeric_cipher
|
||||
This turns into the string $text_cipher
|
||||
If we re-encode it in integer form we get $numeric_cipher2
|
||||
After exponentiation with SECRET exponent we get: $numeric_message2
|
||||
This turns into the string $secret_message2
|
||||
EOT
|
||||
29
Task/RSA-code/Phix/rsa-code.phix
Normal file
29
Task/RSA-code/Phix/rsa-code.phix
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
include builtins/mpfr.e
|
||||
|
||||
mpz n = mpz_init("9516311845790656153499716760847001433441357"),
|
||||
e = mpz_init("65537"),
|
||||
d = mpz_init("5617843187844953170308463622230283376298685"),
|
||||
pt = mpz_init(),
|
||||
ct = mpz_init()
|
||||
|
||||
string plaintext = "Rossetta Code" -- matches C/zkl
|
||||
-- "Rosetta Code" -- matches D/FreeBasic/Go/Icon/J/Kotlin/Seed7.
|
||||
|
||||
mpz_import(pt, length(plaintext), 1, 1, 0, 0, plaintext)
|
||||
|
||||
if mpz_cmp(pt, n)>0 then ?9/0 end if
|
||||
|
||||
mpz_powm(ct, pt, e, n);
|
||||
printf(1,"Encoded: %s\n", {mpz_get_str(ct)})
|
||||
|
||||
mpz_powm(pt, ct, d, n);
|
||||
printf(1,"Decoded: %s\n", {mpz_get_str(pt)})
|
||||
|
||||
integer size =floor((mpz_sizeinbase(pt,2)+7)/8)
|
||||
atom pMem = allocate(size,true)
|
||||
integer count = mpz_export(pMem, 1, 1, 0, 0, pt)
|
||||
if count>size then ?9/0 end if
|
||||
|
||||
printf(1,"As String: %s\n", {peek({pMem,count})})
|
||||
|
||||
{pt, ct, n, e, d} = mpz_free({pt, ct, n, e, d})
|
||||
25
Task/RSA-code/Python/rsa-code.py
Normal file
25
Task/RSA-code/Python/rsa-code.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import binascii
|
||||
|
||||
n = 9516311845790656153499716760847001433441357 # p*q = modulus
|
||||
e = 65537
|
||||
d = 5617843187844953170308463622230283376298685
|
||||
|
||||
message='Rosetta Code!'
|
||||
print('message ', message)
|
||||
|
||||
hex_data = binascii.hexlify(message.encode())
|
||||
print('hex data ', hex_data)
|
||||
|
||||
plain_text = int(hex_data, 16)
|
||||
print('plain text integer ', plain_text)
|
||||
|
||||
if plain_text > n:
|
||||
raise Exception('plain text too large for key')
|
||||
|
||||
encrypted_text = pow(plain_text, e, n)
|
||||
print('encrypted text integer ', encrypted_text)
|
||||
|
||||
decrypted_text = pow(encrypted_text, d, n)
|
||||
print('decrypted text integer ', decrypted_text)
|
||||
|
||||
print('message ', binascii.unhexlify(hex(decrypted_text)[2:]).decode()) # [2:] slicing, to strip the 0x part
|
||||
21
Task/RSA-code/Visual-Basic-.NET/rsa-code.visual
Normal file
21
Task/RSA-code/Visual-Basic-.NET/rsa-code.visual
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
Imports System
|
||||
Imports System.Numerics
|
||||
Imports System.Text
|
||||
|
||||
Module Module1
|
||||
Sub Main()
|
||||
Dim n As BigInteger = BigInteger.Parse("9516311845790656153499716760847001433441357")
|
||||
Dim e As BigInteger = 65537
|
||||
Dim d As BigInteger = BigInteger.Parse("5617843187844953170308463622230283376298685")
|
||||
Dim plainTextStr As String = "Hello, Rosetta!"
|
||||
Dim plainTextBA As Byte() = ASCIIEncoding.ASCII.GetBytes(plainTextStr)
|
||||
Dim pt As BigInteger = New BigInteger(plainTextBA)
|
||||
If pt > n Then Throw New Exception() ' Blocking not implemented
|
||||
Dim ct As BigInteger = BigInteger.ModPow(pt, e, n)
|
||||
Console.WriteLine(" Encoded: " & ct.ToString("X"))
|
||||
Dim dc As BigInteger = BigInteger.ModPow(ct, d, n)
|
||||
Console.WriteLine(" Decoded: " & dc.ToString("X"))
|
||||
Dim decoded As String = ASCIIEncoding.ASCII.GetString(dc.ToByteArray())
|
||||
Console.WriteLine("As ASCII: " & decoded)
|
||||
End Sub
|
||||
End Module
|
||||
Loading…
Add table
Add a link
Reference in a new issue