How to resolve the algorithm RSA code step by step in the Mathematica/Wolfram Language programming language
How to resolve the algorithm RSA code step by step in the Mathematica/Wolfram Language programming language
Table of Contents
Problem Statement
Given an RSA key (n,e,d), construct a program to encrypt and decrypt plaintext messages strings. Background RSA code is used to encode secret messages. It is named after Ron Rivest, Adi Shamir, and Leonard Adleman who published it at MIT in 1977. The advantage of this type of encryption is that you can distribute the number “
n
{\displaystyle n}
” and “
e
{\displaystyle e}
” (which makes up the Public Key used for encryption) to everyone. The Private Key used for decryption “
d
{\displaystyle d}
” is kept secret, so that only the recipient can read the encrypted plaintext. The process by which this is done is that a message, for example “Hello World” is encoded as numbers (This could be encoding as ASCII or as a subset of characters
a
01 , b
02 , . . . , z
26
{\displaystyle a=01,b=02,...,z=26}
). This yields a string of numbers, generally referred to as "numerical plaintext", “
P
{\displaystyle P}
”. For example, “Hello World” encoded with a=1,...,z=26 by hundreds would yield
08051212152315181204
{\displaystyle 08051212152315181204}
. The plaintext must also be split into blocks so that the numerical plaintext is smaller than
n
{\displaystyle n}
otherwise the decryption will fail. The ciphertext,
C
{\displaystyle C}
, is then computed by taking each block of
P
{\displaystyle P}
, and computing Similarly, to decode, one computes To generate a key, one finds 2 (ideally large) primes
p
{\displaystyle p}
and
q
{\displaystyle q}
. the value “
n
{\displaystyle n}
” is simply:
n
p × q
{\displaystyle n=p\times q}
. One must then choose an “
e
{\displaystyle e}
” such that
gcd ( e , ( p − 1 ) × ( q − 1 ) )
1
{\displaystyle \gcd(e,(p-1)\times (q-1))=1}
. That is to say,
e
{\displaystyle e}
and
( p − 1 ) × ( q − 1 )
{\displaystyle (p-1)\times (q-1)}
are relatively prime to each other. The decryption value
d
{\displaystyle d}
is then found by solving The security of the code is based on the secrecy of the Private Key (decryption exponent) “
d
{\displaystyle d}
” and the difficulty in factoring “
n
{\displaystyle n}
”. Research into RSA facilitated advances in factoring and a number of factoring challenges. Keys of 829 bits have been successfully factored, and NIST now recommends 2048 bit keys going forward (see Asymmetric algorithm key lengths). Summary of the task requirements:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm RSA code step by step in the Mathematica/Wolfram Language programming language
This Wolfram code is a simple implementation of the RSA encryption algorithm, which is used for secure communication. It takes a message as input, encrypts it using a public key, and then decrypts it using a private key.
The first function, toNumPlTxt
, converts a string into a number by converting each character to its ASCII code and then representing the result as a single number. The second function, fromNumPlTxt
, does the reverse, converting a number back into a string.
The third function, enc
, encrypts a message using the RSA algorithm. It takes three arguments: the public key n, the public exponent e, and the message mess. It first checks if the message is too long for the given key, and if it is, it returns an error message. Otherwise, it converts the message to a number using toNumPlTxt
, and then raises the result to the power of e modulo n. The result of this calculation is the encrypted message.
The fourth function, dec
, decrypts an encrypted message using the RSA algorithm. It takes three arguments: the private key n, the private exponent d, and the encrypted message en. It first checks if the encrypted message is too long for the given key, and if it is, it returns an error message. Otherwise, it raises the encrypted message to the power of d modulo n. The result of this calculation is the decrypted message.
The last part of the code is an example of how to use the enc
and dec
functions to encrypt and decrypt a message. It defines a message, the public key, the private key, and the encrypted message. It then prints out the original message, the public key, the private key, the numeric plaintext, the encrypted message, and the decrypted message.
Source code in the wolfram programming language
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 <> "'"];
You may also check:How to resolve the algorithm Increment a numerical string step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Composite numbers k with no single digit factors whose factors are all substrings of k step by step in the Arturo programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the VBA programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Scala programming language