Rivest–Shamir–Adleman
An implementation of RSA, a safe public-key cryptosystem for data transmission using a public key to encrypt and a private one to decrypt. Its security relies on the difficulty of factoring large primes, known as the RSA problem which has not be solved yet.
Includes
- Key generator, encryption, decryption (with Chinese Remainder Theorem optimization) and signature verification (does not include hash function).
- Simple
bigInt
andbytes
type implementations for calculus, files and filters.
Implementation
In order to implement this asset, download the model or use require
. Check if the asset is the original one and insert it to your place. A simple example is provided here:
local RSA = require(11287199148) -- Or reference a ModuleScript
n, e, d = RSA.newKeys()
encrypted = RSA.crypt(n, 242351, e)
decrypted = RSA.crypt(n, encrypted, d)
print(decrypted[1]) -- Expected output: 242351
A correct use of this algorithm is to keep the primes and private key secret, as well as all the processes must be done in the server. Do not use this for bulk encryption/decryption, as the algorithm is relatively slow; you might be looking for a symmetric-key one.
Information
This module includes 4 utilities:
-
Key generator generates the key pairs with a given bit length (default
256
), or givenp
,q
ande
(optional). Returns 8bigInt
s:
Value |
Default |
Description |
---|---|---|
n |
Product of p and q . Can be released |
|
e |
3 or 65537
|
Public key. Used to encrypt data |
d |
Secret private key. Used to decrypt data |
The next ones are used only with CRT optimization, which allows computing modular exponentiation more efficiently and sent as part of the private key.
Value |
Default |
Description |
---|---|---|
p |
p input |
Source prime number |
q |
q input |
Source prime number |
d_p |
||
d_q |
||
q_inv |
-
Encryption and Decryption uses modular exponentiation properties to get the message. Text must be in the range [0;
n
>. Ife
is short or pair is suitable then encryption is fast. For optimized decryption, CRT mode should be used. - Signature verification verifies the signature with the hash of the decrypted message. Notice that to sign a message you should use encryption function and this does not include a hash one.
-
Data type conversion between
bigInt
andbytes
. Since RSA usually works with large primes, Luau nativenumber
s cannot handle correctly them. This function brings an easier form to convert files tobigInt
s and vice versa so files (such asnumber
s andstring
s) can be encrypted. Notice that padding is not included.
bigInt
s can be automatically converted from string
s and number
s. For strings, they are notated as decimal (default), binary (base-2, beginning with 0b
) or hexadecimal (base-16, beginning with 0x
), in big-endian and underscores can be used. Usually using binary or hexadecimal notations are faster to convert.
Performance
The algorithm has been benchmarked with an Intel® Core™ i7-1065G7 @ 1.30GHz processor with a 16 GB RAM. e
= 65537
for encryption was used.
Key size |
Generation av. time | Encryption av. time | CRT decryption av. time |
---|---|---|---|
32 bits | 0.0085 |
0.0002 |
0.0005 |
64 bits | 0.0446 |
0.0007 |
0.0014 |
128 bits (16 bytes) | 0.2325 |
0.0018 |
0.0161 |
256 bits (32 bytes) | 2.6018 |
0.0145 |
0.0864 |
384 bits (48 bytes) | 4.6809 |
0.0148 |
0.2283 |
512 bits (1/4 def. gen.) | ~40 |
0.0619 |
1.5326 |
As seen above, bigInt
calculus takes more time to compute, especially pow
function. However, compared to other libraries, this is relatively faster since this was built for Luau, both key generation and decryption using optimization. Rest times can be used to prevent crashes, at the cost of time.
- Great and useful
- Great
- Good
- Needs improvements
0 voters
- Yes (which one?)
- No
0 voters
Thanks for the feedback and your support!