Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
38
Task/RSA-code/Ada/rsa-code.adb
Normal file
38
Task/RSA-code/Ada/rsa-code.adb
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;
|
||||
201
Task/RSA-code/EasyLang/rsa-code.easy
Normal file
201
Task/RSA-code/EasyLang/rsa-code.easy
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
func[] bn s$ .
|
||||
i = len s$ - 7 + 1
|
||||
while i >= -5
|
||||
r[] &= number substr s$ i 7
|
||||
i -= 7
|
||||
.
|
||||
return r[]
|
||||
.
|
||||
func$ str bn[] .
|
||||
s$ = bn[$]
|
||||
for i = len bn[] - 1 downto 1
|
||||
h$ = bn[i]
|
||||
s$ &= substr "0000000" 1 (7 - len h$) & h$
|
||||
.
|
||||
return s$
|
||||
.
|
||||
func[] bnmul a[] b[] .
|
||||
len r[] len a[] + len b[]
|
||||
if len a[] > len b[] : swap a[] b[]
|
||||
for ia = 1 to len a[]
|
||||
h = 0
|
||||
for ib = 1 to len b[]
|
||||
h += r[ia + ib - 1] + b[ib] * a[ia]
|
||||
r[ia + ib - 1] = h mod 10000000
|
||||
h = h div 10000000
|
||||
.
|
||||
r[ia + ib - 1] += h
|
||||
.
|
||||
while r[$] = 0 and len r[] > 1 : len r[] -1
|
||||
return r[]
|
||||
.
|
||||
func[] bnadd a[] b[] .
|
||||
if len b[] > len a[] : swap a[] b[]
|
||||
len r[] len a[]
|
||||
for i = 1 to len r[]
|
||||
v = 0
|
||||
if i <= len b[] : v = b[i]
|
||||
h += a[i] + v
|
||||
r[i] = h mod 10000000
|
||||
h = h div 10000000
|
||||
.
|
||||
if h > 0 : r[] &= h
|
||||
while len r[] > 1 and r[$] = 0 : len r[] -1
|
||||
return r[]
|
||||
.
|
||||
func bncmp a[] b[] .
|
||||
if len a[] > len b[] : return 1
|
||||
if len a[] < len b[] : return -1
|
||||
for i = len a[] downto 1
|
||||
if a[i] > b[i] : return 1
|
||||
if a[i] < b[i] : return -1
|
||||
.
|
||||
return 0
|
||||
.
|
||||
func[] bnsub a[] b[] .
|
||||
len r[] len a[]
|
||||
for i = 1 to len r[]
|
||||
v = 0
|
||||
if i <= len b[] : v = b[i]
|
||||
h = a[i] - v - h
|
||||
if h < 0
|
||||
r[i] = h + 10000000
|
||||
h = 1
|
||||
else
|
||||
r[i] = h
|
||||
h = 0
|
||||
.
|
||||
.
|
||||
while r[$] = 0 and len r[] > 1 : len r[] -1
|
||||
return r[]
|
||||
.
|
||||
func[][] bndivmod a[] b[] .
|
||||
if bncmp a[] b[] < 0 : return [ [ 0 ] a[] ]
|
||||
lb = len b[]
|
||||
d = 10000000 div (b[lb] + 1)
|
||||
if d > 1
|
||||
for i = 1 to lb
|
||||
carry += b[i] * d
|
||||
b[i] = carry mod 10000000
|
||||
carry = carry div 10000000
|
||||
.
|
||||
for i = 1 to len a[]
|
||||
carry += a[i] * d
|
||||
a[i] = carry mod 10000000
|
||||
carry = carry div 10000000
|
||||
.
|
||||
.
|
||||
a[] &= carry
|
||||
#
|
||||
len q[] len a[] - lb
|
||||
v1 = b[lb]
|
||||
if lb >= 2 : v2 = b[lb - 1]
|
||||
for j = len q[] downto 1
|
||||
u0 = a[j + lb]
|
||||
u1 = a[j + lb - 1]
|
||||
u2 = 0
|
||||
if j + lb >= 2 : u2 = a[j + lb - 2]
|
||||
if u0 = v1
|
||||
qhat = 9999999
|
||||
rhat = u1 + v1
|
||||
else
|
||||
h = u0 * 10000000 + u1
|
||||
qhat = h div v1
|
||||
rhat = h mod v1
|
||||
.
|
||||
while rhat < 10000000 and qhat * v2 > 10000000 * rhat + u2
|
||||
qhat -= 1
|
||||
rhat += v1
|
||||
.
|
||||
k = 0
|
||||
for i = 1 to lb
|
||||
p = qhat * b[i] + k
|
||||
k = p div 10000000
|
||||
t = a[j + i - 1] - (p mod 10000000)
|
||||
if t < 0
|
||||
t += 10000000
|
||||
k += 1
|
||||
.
|
||||
a[j + i - 1] = t
|
||||
.
|
||||
t = a[j + lb] - k
|
||||
if t < 0
|
||||
qhat -= 1
|
||||
k = 0
|
||||
for i = 1 to lb
|
||||
t = a[j + i - 1] + b[i] + k
|
||||
a[j + i - 1] = t mod 10000000
|
||||
k = t div 10000000
|
||||
.
|
||||
a[j + lb] = t + k
|
||||
else
|
||||
a[j + lb] = t
|
||||
.
|
||||
q[j] = qhat
|
||||
.
|
||||
if d > 1
|
||||
k = 0
|
||||
for i = lb downto 1
|
||||
t = k * 10000000 + a[i]
|
||||
a[i] = t div d
|
||||
k = t mod d
|
||||
.
|
||||
.
|
||||
len a[] lb
|
||||
while len q[] > 1 and q[len q[]] = 0 : len q[] len q[] - 1
|
||||
while len a[] > 1 and a[len a[]] = 0 : len a[] len a[] - 1
|
||||
return [ q[] a[] ]
|
||||
.
|
||||
func[] bnmod a[] n[] .
|
||||
result[][] = bndivmod a[] n[]
|
||||
return result[2][]
|
||||
.
|
||||
func[] bnpowmod base[] exp[] m[] .
|
||||
r[] = [ 1 ]
|
||||
b[] = bnmod base[] m[]
|
||||
e[] = exp[]
|
||||
while bncmp e[] [ 0 ] > 0
|
||||
if e[1] mod 2 = 1
|
||||
r[] = bnmul r[] b[]
|
||||
r[] = bnmod r[] m[]
|
||||
.
|
||||
h = 0
|
||||
for i = len e[] downto 1
|
||||
h = h * 10000000 + e[i]
|
||||
e[i] = h div 2
|
||||
h = h mod 2
|
||||
.
|
||||
while e[$] = 0 and len e[] > 1 : len e[] -1
|
||||
b[] = bnmul b[] b[]
|
||||
b[] = bnmod b[] m[]
|
||||
.
|
||||
return r[]
|
||||
.
|
||||
#
|
||||
n[] = bn "9516311845790656153499716760847001433441357"
|
||||
e[] = bn "65537"
|
||||
d[] = bn "5617843187844953170308463622230283376298685"
|
||||
#
|
||||
m$ = "Rosetta Code"
|
||||
print "Plain: " & m$
|
||||
for c$ in strchars m$
|
||||
h = strcode c$ - 28
|
||||
if h <= 9 : mn$ &= 0
|
||||
mn$ &= h
|
||||
.
|
||||
print "Encoded: " & mn$
|
||||
m[] = bn mn$
|
||||
# Encrypt: c = m^e mod n
|
||||
c[] = bnpowmod m[] e[] n[]
|
||||
print "Encrypted: " & str c[]
|
||||
print ""
|
||||
#
|
||||
# Decrypt: m = c^d mod n
|
||||
m2[] = bnpowmod c[] d[] n[]
|
||||
print "Decrypted: " & str m2[]
|
||||
mn$ = str m2[]
|
||||
m$ = ""
|
||||
for i = 1 step 2 to len mn$
|
||||
m$ &= strchar (number substr mn$ i 2 + 28)
|
||||
.
|
||||
print "Decoded: " & m$
|
||||
|
|
@ -1,32 +1,30 @@
|
|||
(notonline)-->
|
||||
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
without javascript_semantics
|
||||
include builtins/mpfr.e
|
||||
|
||||
<span style="color: #004080;">mpz</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"9516311845790656153499716760847001433441357"</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"65537"</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"5617843187844953170308463622230283376298685"</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">pt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(),</span>
|
||||
<span style="color: #000000;">ct</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
|
||||
mpz n = mpz_init("9516311845790656153499716760847001433441357"),
|
||||
e = mpz_init("65537"),
|
||||
d = mpz_init("5617843187844953170308463622230283376298685"),
|
||||
pt = mpz_init(),
|
||||
ct = mpz_init()
|
||||
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">plaintext</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Rossetta Code"</span> <span style="color: #000080;font-style:italic;">-- matches C/zkl
|
||||
-- "Rosetta Code" -- matches D/FreeBasic/Go/Icon/J/Kotlin/Seed7.</span>
|
||||
string plaintext = "Rossetta Code" -- matches C/zkl
|
||||
-- "Rosetta Code" -- matches D/FreeBasic/Go/Icon/J/Kotlin/Seed7.
|
||||
|
||||
<span style="color: #000000;">mpz_import</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plaintext</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">plaintext</span><span style="color: #0000FF;">)</span>
|
||||
mpz_import(pt, length(plaintext), 1, 1, 0, 0, plaintext)
|
||||
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
if mpz_cmp(pt, n)>0 then ?9/0 end if
|
||||
|
||||
<span style="color: #7060A8;">mpz_powm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ct</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">);</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Encoded: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ct</span><span style="color: #0000FF;">)})</span>
|
||||
mpz_powm(ct, pt, e, n);
|
||||
printf(1,"Encoded: %s\n", {mpz_get_str(ct)})
|
||||
|
||||
<span style="color: #7060A8;">mpz_powm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ct</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">);</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Decoded: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">)})</span>
|
||||
mpz_powm(pt, ct, d, n);
|
||||
printf(1,"Decoded: %s\n", {mpz_get_str(pt)})
|
||||
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">size</span> <span style="color: #0000FF;">=</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #7060A8;">mpz_sizeinbase</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">pMem</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">size</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mpz_export</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pMem</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pt</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">></span><span style="color: #000000;">size</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
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
|
||||
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"As String: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">peek</span><span style="color: #0000FF;">({</span><span style="color: #000000;">pMem</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">})})</span>
|
||||
printf(1,"As String: %s\n", {peek({pMem,count})})
|
||||
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ct</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_free</span><span style="color: #0000FF;">({</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ct</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">})</span>
|
||||
<!--
|
||||
{pt, ct, n, e, d} = mpz_free({pt, ct, n, e, d})
|
||||
|
|
|
|||
39
Task/RSA-code/Pluto/rsa-code.pluto
Normal file
39
Task/RSA-code/Pluto/rsa-code.pluto
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
require "bignum"
|
||||
require "table2"
|
||||
|
||||
local pt = "Rosetta Code"
|
||||
print($"Plain text: : {pt}")
|
||||
local n = bigint.new("9516311845790656153499716760847001433441357")
|
||||
local e = bigint.new("65537")
|
||||
local d = bigint.new("5617843187844953170308463622230283376298685")
|
||||
local ptn = bigint.zero
|
||||
-- Convert plain text to a number.
|
||||
for {pt:byte(1, #pt)} as b do
|
||||
ptn = bigint.bor(bigint.shl(ptn, 8), bigint.new(b))
|
||||
end
|
||||
if ptn >= n then
|
||||
print("Plain text message too long")
|
||||
os.exit(1)
|
||||
end
|
||||
print($"Plain text as a number : {ptn}")
|
||||
|
||||
-- Encode a single number.
|
||||
local etn = bigint.modpow(ptn, e, n)
|
||||
print($"Encoded : {etn}")
|
||||
|
||||
-- Decode a single number.
|
||||
local dtn = bigint.modpow(etn, d, n)
|
||||
print($"Decoded : {dtn}")
|
||||
|
||||
-- Convert number to text.
|
||||
local db = table.rep(16, 0)
|
||||
local dx = 16
|
||||
local bff = bigint.new(255)
|
||||
while dtn:bitlength() > 0 do
|
||||
db[dx] = bigint.toplain(bigint.band(dtn, bff))
|
||||
dx -= 1
|
||||
dtn = bigint.shr(dtn, 8)
|
||||
end
|
||||
local s = ""
|
||||
for i = dx + 1, 16 do s ..= string.char(db[i]) end
|
||||
print($"Decoded number as text : {s}")
|
||||
13
Task/RSA-code/PowerShell/rsa-code.ps1
Normal file
13
Task/RSA-code/PowerShell/rsa-code.ps1
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
$n = [BigInt]::Parse("9516311845790656153499716760847001433441357")
|
||||
$e = [BigInt]::new(65537)
|
||||
$d = [BigInt]::Parse("5617843187844953170308463622230283376298685")
|
||||
$plaintextstring = "Hello, Rosetta!"
|
||||
$plaintext = [Text.ASCIIEncoding]::ASCII.GetBytes($plaintextstring)
|
||||
[BigInt]$pt = [BigInt]::new($plaintext)
|
||||
if ($n -lt $pt) {throw "`$n = $n < $pt = `$pt"}
|
||||
$ct = [BigInt]::ModPow($pt, $e, $n)
|
||||
"Encoded: $ct"
|
||||
$dc = [BigInt]::ModPow($ct, $d, $n)
|
||||
"Decoded: $dc"
|
||||
$decoded = [Text.ASCIIEncoding]::ASCII.GetString($dc.ToByteArray())
|
||||
"As ASCII: $decoded"
|
||||
|
|
@ -2,34 +2,34 @@
|
|||
fn main() {
|
||||
//var bb, ptn, etn, dtn big.Int
|
||||
pt := "Rosetta Code"
|
||||
println("Plain text: $pt")
|
||||
println("Plain text: $pt")
|
||||
|
||||
// a key set big enough to hold 16 bytes of plain text in
|
||||
// a single block (to simplify the example) and also big enough
|
||||
// to demonstrate efficiency of modular exponentiation.
|
||||
n := big.integer_from_string("9516311845790656153499716760847001433441357")?
|
||||
e := big.integer_from_string("65537")?
|
||||
d := big.integer_from_string("5617843187844953170308463622230283376298685")?
|
||||
n := big.integer_from_string("9516311845790656153499716760847001433441357")!
|
||||
e := big.integer_from_string("65537")!
|
||||
d := big.integer_from_string("5617843187844953170308463622230283376298685")!
|
||||
|
||||
mut ptn := big.zero_int
|
||||
// convert plain text to a number
|
||||
for b in pt.bytes() {
|
||||
bb := big.integer_from_i64(i64(b))
|
||||
ptn = ptn.lshift(8).bitwise_or(bb)
|
||||
ptn = ptn.left_shift(8).bitwise_or(bb)
|
||||
}
|
||||
if ptn >= n {
|
||||
println("Plain text message too long")
|
||||
return
|
||||
}
|
||||
println("Plain text as a number:$ptn")
|
||||
println("Plain text as a number: $ptn")
|
||||
|
||||
// encode a single number
|
||||
etn := ptn.big_mod_pow(e,n)
|
||||
println("Encoded: $etn")
|
||||
etn := ptn.big_mod_pow(e,n)!
|
||||
println("Encoded: $etn")
|
||||
|
||||
// decode a single number
|
||||
mut dtn := etn.big_mod_pow(d,n)
|
||||
println("Decoded: $dtn")
|
||||
mut dtn := etn.big_mod_pow(d,n)!
|
||||
println("Decoded: $dtn")
|
||||
|
||||
// convert number to text
|
||||
mut db := [16]u8{}
|
||||
|
|
@ -39,44 +39,44 @@ fn main() {
|
|||
dx--
|
||||
bb := dtn.bitwise_and(bff)
|
||||
db[dx] = u8(i64(bb.int()))
|
||||
dtn = dtn.rshift(8)
|
||||
dtn = dtn.right_shift(8)
|
||||
println('${db[0..].bytestr()} ${dtn.bit_len()}')
|
||||
}
|
||||
println("Decoded number as text: ${db[dx..].bytestr()}")
|
||||
}*/
|
||||
|
||||
}
|
||||
*/
|
||||
import math.big
|
||||
fn main() {
|
||||
//var bb, ptn, etn, dtn big.Int
|
||||
pt := "Hello World"
|
||||
println("Plain text: $pt")
|
||||
println("Plain text: $pt")
|
||||
|
||||
// a key set big enough to hold 16 bytes of plain text in
|
||||
// a single block (to simplify the example) and also big enough
|
||||
// to demonstrate efficiency of modular exponentiation.
|
||||
n := big.integer_from_string("9516311845790656153499716760847001433441357")?
|
||||
e := big.integer_from_string("65537")?
|
||||
d := big.integer_from_string("5617843187844953170308463622230283376298685")?
|
||||
n := big.integer_from_string("9516311845790656153499716760847001433441357")!
|
||||
e := big.integer_from_string("65537")!
|
||||
d := big.integer_from_string("5617843187844953170308463622230283376298685")!
|
||||
|
||||
mut ptn := big.zero_int
|
||||
// convert plain text to a number
|
||||
for b in pt.bytes() {
|
||||
bb := big.integer_from_i64(i64(b))
|
||||
ptn = ptn.lshift(8).bitwise_or(bb)
|
||||
ptn = ptn.left_shift(8).bitwise_or(bb)
|
||||
}
|
||||
if ptn >= n {
|
||||
println("Plain text message too long")
|
||||
return
|
||||
}
|
||||
println("Plain text as a number:$ptn")
|
||||
println("Plain text as a number: $ptn")
|
||||
|
||||
// encode a single number
|
||||
etn := ptn.big_mod_pow(e,n)
|
||||
println("Encoded: $etn")
|
||||
etn := ptn.big_mod_pow(e,n)!
|
||||
println("Encoded: $etn")
|
||||
|
||||
// decode a single number
|
||||
mut dtn := etn.big_mod_pow(d,n)
|
||||
println("Decoded: $dtn")
|
||||
mut dtn := etn.big_mod_pow(d,n)!
|
||||
println("Decoded: $dtn")
|
||||
|
||||
// convert number to text
|
||||
mut db := [16]u8{}
|
||||
|
|
@ -86,7 +86,7 @@ fn main() {
|
|||
dx--
|
||||
bb := dtn.bitwise_and(bff)
|
||||
db[dx] = u8(i64(bb.int()))
|
||||
dtn = dtn.rshift(8)
|
||||
dtn = dtn.right_shift(8)
|
||||
}
|
||||
println("Decoded number as text: ${db[dx..].bytestr()}")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue