RSA code: Difference between revisions

3,517 bytes added ,  3 years ago
implement in nim lang
(Add Rust implementation)
(implement in nim lang)
Line 995:
Encoded: 199505409518408949879682159958576932863989
Decoded: 'The cake is a lie!'</pre>
 
=={{header|Nim}}==
<lang nim>import strutils, streams, strformat
# nimble install stint
import stint
 
const messages = ["PPAP", "I have a pen, I have a apple\nUh! Apple-pen!",
"I have a pen, I have pineapple\nUh! Pineapple-pen!",
"Apple-pen, pineapple-pen\nUh! Pen-pineapple-apple-pen\nPen-pineapple-apple-pen\nDance time!"]
const
n = u256("9516311845790656153499716760847001433441357")
e = u256("65537")
d = u256("5617843187844953170308463622230283376298685")
proc translate(hexString: string, key, divisor: UInt256,
encrypt = true): string =
var
strm = newStringStream(hexString)
chunk, residue: string
chunkSize = len(toHex(divisor))
while true:
chunk = strm.peekStr(chunkSize-int(encrypt))
if len(chunk) == 0:
result&=residue
break
if len(residue) > 0:
result&=align(residue, chunkSize-1+int(encrypt), '0')
discard strm.readStr(chunkSize-int(encrypt))
residue = toHex(powmod(UInt256.fromHex(chunk), key, divisor))
strm.close()
for message in messages:
echo(&"plaintext:\n{message}")
var numPlaintext = message.toHex()
echo(&"numerical plaintext in hex:\n{numPlaintext}")
var ciphertext = translate(numPlaintext, e, n)
echo(&"ciphertext is: \n{ciphertext}")
var deciphertext = translate(ciphertext, d, n, false)
echo(&"deciphered numerical plaintext in hex is:\n{deciphertext}")
echo(&"deciphered plaintext is:\n{parseHexStr(deciphertext)}\n\n")</lang>
{{out}}
<pre>plaintext:
PPAP
numerical plaintext in hex:
50504150
ciphertext is:
6c55b71c718b8921555eaa9754b7bfd7018b
deciphered numerical plaintext in hex is:
50504150
deciphered plaintext is:
PPAP
 
 
plaintext:
I have a pen, I have a apple
Uh! Apple-pen!
numerical plaintext in hex:
49206861766520612070656E2C204920686176652061206170706C650A556821204170706C652D70656E21
ciphertext is:
0095018316e01ab0c681c61ed03c12e49fd34363bf2498b4d6d5a396cf3ad7a86018609448e92cb927a9444b1225f085f68cbaf103d5
deciphered numerical plaintext in hex is:
49206861766520612070656e2c204920686176652061206170706c650a556821204170706c652d70656e21
deciphered plaintext is:
I have a pen, I have a apple
Uh! Apple-pen!
 
 
plaintext:
I have a pen, I have pineapple
Uh! Pineapple-pen!
numerical plaintext in hex:
49206861766520612070656E2C204920686176652070696E656170706C650A5568212050696E656170706C652D70656E21
ciphertext is:
0095018316e01ab0c681c61ed03c12e49fd35da93d762b6d4d80a227fa6394136bb95ed725f8c4b378297efb4c417da533f488b3ce8e
deciphered numerical plaintext in hex is:
49206861766520612070656e2c204920686176652070696e656170706c650a5568212050696e656170706c652d70656e21
deciphered plaintext is:
I have a pen, I have pineapple
Uh! Pineapple-pen!
 
 
plaintext:
Apple-pen, pineapple-pen
Uh! Pen-pineapple-apple-pen
Pen-pineapple-apple-pen
Dance time!
numerical plaintext in hex:
4170706C652D70656E2C2070696E656170706C652D70656E0A5568212050656E2D70696E656170706C652D6170706C652D70656E0A50656E2D70696E656170706C652D6170706C652D70656E0A44616E63652074696D6521
ciphertext is:
49c1a7c0844fbc0d3d6915539cdde8794b6f58ae75cf4e4f452a5004147788b71a96fc5e6cb5c08bd9b78b3630615a877740735ec9bb3d7a45a45b43b8ae2bffca38dd4e4bd8361f60961dc9acd9a362d5c0cf2f158d4948f90a1
deciphered numerical plaintext in hex is:
4170706c652d70656e2c2070696e656170706c652d70656e0a5568212050656e2d70696e656170706c652d6170706c652d70656e0a50656e2d70696e656170706c652d6170706c652d70656e0a44616e63652074696d6521
deciphered plaintext is:
Apple-pen, pineapple-pen
Uh! Pen-pineapple-apple-pen
Pen-pineapple-apple-pen
Dance time!</pre>
 
=={{header|PARI/GP}}==