Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
73
Task/RSA-code/ALGOL-68/rsa-code.alg
Normal file
73
Task/RSA-code/ALGOL-68/rsa-code.alg
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
COMMENT
|
||||
First cut. Doesn't yet do blocking and deblocking. Also, as
|
||||
encryption and decryption are identical operations but for the
|
||||
reciprocal exponents used, only one has been implemented below.
|
||||
|
||||
A later release will address these issues.
|
||||
COMMENT
|
||||
|
||||
BEGIN
|
||||
PR precision=1000 PR
|
||||
MODE LLI = LONG LONG INT; CO For brevity CO
|
||||
PROC mod power = (LLI base, exponent, modulus) LLI :
|
||||
BEGIN
|
||||
LLI result := 1, b := base, e := exponent;
|
||||
IF exponent < 0
|
||||
THEN
|
||||
put (stand error, (("Negative exponent", exponent, newline)))
|
||||
ELSE
|
||||
WHILE e > 0
|
||||
DO
|
||||
(ODD e | result := (result * b) MOD modulus);
|
||||
e OVERAB 2; b := (b * b) MOD modulus
|
||||
OD
|
||||
FI;
|
||||
result
|
||||
END;
|
||||
PROC modular inverse = (LLI a, m) LLI :
|
||||
BEGIN
|
||||
PROC extended gcd = (LLI x, y) []LLI :
|
||||
BEGIN
|
||||
LLI v := 1, a := 1, u := 0, b := 0, g := x, w := y;
|
||||
WHILE w>0
|
||||
DO
|
||||
LLI q := g % w, t := a - q * u;
|
||||
a := u; u := t;
|
||||
t := b - q * v;
|
||||
b := v; v := t;
|
||||
t := g - q * w;
|
||||
g := w; w := t
|
||||
OD;
|
||||
a PLUSAB (a < 0 | u | 0);
|
||||
(a, b, g)
|
||||
END;
|
||||
[] LLI egcd = extended gcd (a, m);
|
||||
(egcd[3] > 1 | 0 | egcd[1] MOD m)
|
||||
END;
|
||||
PROC number to string = (LLI number) STRING :
|
||||
BEGIN
|
||||
[] CHAR map = (blank + "ABCDEFGHIJKLMNOPQRSTUVWXYZ")[@0];
|
||||
LLI local number := number;
|
||||
INT length := SHORTEN SHORTEN ENTIER long long log(number) + 1;
|
||||
(ODD length | length PLUSAB 1);
|
||||
[length % 2] CHAR text;
|
||||
FOR i FROM length % 2 BY -1 TO 1
|
||||
DO
|
||||
INT index = SHORTEN SHORTEN (local number MOD 100);
|
||||
text[i] := (index > 26 | "?" | map[index]);
|
||||
local number := local number % 100
|
||||
OD;
|
||||
text
|
||||
END;
|
||||
CO The parameters of a particular RSA cryptosystem CO
|
||||
LLI p = 3490529510847650949147849619903898133417764638493387843990820577;
|
||||
LLI q = 32769132993266709549961988190834461413177642967992942539798288533;
|
||||
LLI n = p * q;
|
||||
LLI phi n = (p-1) * (q-1);
|
||||
LLI e = 9007;
|
||||
LLI d = modular inverse (e, phi n);
|
||||
CO A ciphertext CO
|
||||
LLI cipher text = 96869613754622061477140922254355882905759991124574319874695120930816298225145708356931476622883989628013391990551829945157815154;
|
||||
CO Print out the corresponding plain text CO
|
||||
print (number to string (mod power (ciphertext, d, n)))
|
||||
END
|
||||
34
Task/RSA-code/C/rsa-code.c
Normal file
34
Task/RSA-code/C/rsa-code.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmp.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
mpz_t n, d, e, pt, ct;
|
||||
|
||||
mpz_init(pt);
|
||||
mpz_init(ct);
|
||||
mpz_init_set_str(n, "9516311845790656153499716760847001433441357", 10);
|
||||
mpz_init_set_str(e, "65537", 10);
|
||||
mpz_init_set_str(d, "5617843187844953170308463622230283376298685", 10);
|
||||
|
||||
const char *plaintext = "Rossetta Code";
|
||||
mpz_import(pt, strlen(plaintext), 1, 1, 0, 0, plaintext);
|
||||
|
||||
if (mpz_cmp(pt, n) > 0)
|
||||
abort();
|
||||
|
||||
mpz_powm(ct, pt, e, n);
|
||||
gmp_printf("Encoded: %Zd\n", ct);
|
||||
|
||||
mpz_powm(pt, ct, d, n);
|
||||
gmp_printf("Decoded: %Zd\n", pt);
|
||||
|
||||
char buffer[64];
|
||||
mpz_export(buffer, NULL, 1, 1, 0, 0, pt);
|
||||
printf("As String: %s\n", buffer);
|
||||
|
||||
mpz_clears(pt, ct, n, e, d, NULL);
|
||||
return 0;
|
||||
}
|
||||
46
Task/RSA-code/Haskell/rsa-code.hs
Normal file
46
Task/RSA-code/Haskell/rsa-code.hs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
module RSAMaker
|
||||
where
|
||||
import Data.Char ( chr )
|
||||
|
||||
encode :: String -> [Integer]
|
||||
encode s = map (toInteger . fromEnum ) s
|
||||
|
||||
rsa_encode :: Integer -> Integer -> [Integer] -> [Integer]
|
||||
rsa_encode n e numbers = map (\num -> mod ( num ^ e ) n ) numbers
|
||||
|
||||
rsa_decode :: Integer -> Integer -> [Integer] -> [Integer]
|
||||
rsa_decode d n ciphers = map (\c -> mod ( c ^ d ) n ) ciphers
|
||||
|
||||
decode :: [Integer] -> String
|
||||
decode encoded = map ( chr . fromInteger ) encoded
|
||||
|
||||
divisors :: Integer -> [Integer]
|
||||
divisors n = [m | m <- [1..n] , mod n m == 0 ]
|
||||
|
||||
isPrime :: Integer -> Bool
|
||||
isPrime n = divisors n == [1,n]
|
||||
|
||||
totient :: Integer -> Integer -> Integer
|
||||
totient prime1 prime2 = (prime1 - 1 ) * ( prime2 - 1 )
|
||||
|
||||
myE :: Integer -> Integer
|
||||
myE tot = head [n | n <- [2..tot - 1] , gcd n tot == 1]
|
||||
|
||||
myD :: Integer -> Integer -> Integer -> Integer
|
||||
myD e n phi = head [d | d <- [1..n] , mod ( d * e ) phi == 1]
|
||||
|
||||
main = do
|
||||
putStrLn "Enter a test text!"
|
||||
text <- getLine
|
||||
let primes = take 90 $ filter isPrime [1..]
|
||||
p1 = last primes
|
||||
p2 = last $ init primes
|
||||
tot = totient p1 p2
|
||||
e = myE tot
|
||||
n = p1 * p2
|
||||
rsa_encoded = rsa_encode n e $ encode text
|
||||
d = myD e n tot
|
||||
encrypted = concatMap show rsa_encoded
|
||||
decrypted = decode $ rsa_decode d n rsa_encoded
|
||||
putStrLn ("Encrypted: " ++ encrypted )
|
||||
putStrLn ("And now decrypted: " ++ decrypted )
|
||||
20
Task/RSA-code/Mathematica/rsa-code.math
Normal file
20
Task/RSA-code/Mathematica/rsa-code.math
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
toNumPlTxt[s_] := FromDigits[ToCharacterCode[s], 256];
|
||||
fromNumPlTxt[plTxt_] := FromCharacterCode[IntegerDigits[plTxt, 256]];
|
||||
enc::longmess = "Message '``' is too long for n = ``.";
|
||||
enc[n_, _, mess_] /;
|
||||
toNumPlTxt[mess] >= n := (Message[enc::longmess, mess, n]; $Failed);
|
||||
enc[n_, e_, mess_] := PowerMod[toNumPlTxt[mess], e, n];
|
||||
dec[n_, d_, en_] := fromNumPlTxt[PowerMod[en, d, n]];
|
||||
text = "The cake is a lie!";
|
||||
n = 9516311845790656153499716760847001433441357;
|
||||
e = 65537;
|
||||
d = 5617843187844953170308463622230283376298685;
|
||||
en = enc[n, e, text];
|
||||
de = dec[n, d, en];
|
||||
Print["Text: '" <> text <> "'"];
|
||||
Print["n = " <> IntegerString[n]];
|
||||
Print["e = " <> IntegerString[e]];
|
||||
Print["d = " <> IntegerString[d]];
|
||||
Print["Numeric plaintext: " <> IntegerString[toNumPlTxt[text]]];
|
||||
Print["Encoded: " <> IntegerString[en]];
|
||||
Print["Decoded: '" <> de <> "'"];
|
||||
|
|
@ -5,7 +5,7 @@ constant $d = 5617843187844953170308463622230283376298685;
|
|||
my $secret-message = "ROSETTA CODE";
|
||||
|
||||
package Message {
|
||||
my @alphabet = 'A' .. 'Z', ' ';
|
||||
my @alphabet = slip('A' .. 'Z'), ' ';
|
||||
my $rad = +@alphabet;
|
||||
my %code = @alphabet Z=> 0 .. *;
|
||||
subset Text of Str where /^^ @alphabet+ $$/;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue