When it comes to asymmetric cryptography, RSA (Rivest-Shamir-Adleman) is the default example to explain concepts regarding the topic. The algorithm is one of the first invented (1978) to do so work, rely on a pair of keys, and yhe reversible property of the key to encrypt with one key and roll back to the original message using the other key.
The basic principle behind RSA is the multiplication of two factors
LilRSA is a simple RSA library for studying purpose. The goal is not to create a reliable encryptation function using this almost pure python implementation. You can learn how RSA works under the hood by reading the jupyter RSA which will explain using mathematical operations concatenated with python codes. The goal is to have something readable and interactive.
- Import the library.
- Make an instance of the class
LilRSA
. - Generate the key pair. Do it by specifying the number of digits of each factor
p
andq
. If the factors have 32 digits, the final module contains 64 digits. The factors are not random prime numbers.
>>> import lilRSA as rsa
>>> x = rsa.LilRSA()
>>> x.GenPair(32)
>>> x.e
65537
>>> x.n
2444571593862958259036071898156097394153021932606491330126202367
>>> c = x.Encry("HI!")
>>> c
582782624632987655950005284831611630067944829631703443063072197
>>> x.Decry(c)
'HI!'
>>> x.d
1251883849325245358954005295419552993186690025834535752726744033
>>>
- Import the library.
- Make an instance of the class
LilRSA
. - Generate the key pair. Do it by specifying the number of digits of each factor
p
andq
. If the factors have 32 digits, the final module contains 64 digits. The factors are not random prime numbers.
Basic functions:
class LilRSA(builtins.object)
| LilRSA class contains all params and methods
| for creating a simple RSA cryptography and
| data signature.
|
| Methods defined here:
|
| Decry(self, c: int) -> str
|
| Encry(self, m: str) -> int
|
| GenPair(self, d: int = 64)
|
| Sign(self, m: str) -> int
| # Sing will just encrypt with the private key
|
| Validate(self, m: int, hash: str) -> bool
| # Validate will just decrypt using the public