Credit goes to Jeff's answer for the details and Steve's answer which was also helpful. Credit also goes to tylerl's answer which included links to Wikipedia for all the functions, particularly modInverse
, also it clarified the ambiguous starting point for e
. Thank you, I upvoted your answers, and using combined information from all three answers, I created what I hope is a better answer.
The key to making reverse-engineering so expensive is using power-of. Square root is not so hard, power of 3 means you need a cubed root, but power of 34,051,489 is pretty hard. There are other mathematical operations that are difficult to reverse-engineer. There are also multiple ways to create an Asymmetric Algorithm, but this answer focuses on RSA. The oldest and most common.
RSA Algorithm (based on the Java Code)
The the below calculations should be done with arbitrary precision integers. (Such as BigInt or BigInteger)
Generating the keys:
- Constant key length is
l
.
- Usually constant
e_start
can =3
for simplicity. However, 0x10001
is more common, at any rate, a prime number is best (for key-generation performance reasons and probably other reasons).
p
and q
are the randomly generated positive prime numbers, that require up to l
bits for storage. (To keep these positive, the first bit will always be 0
)
n
= p*q
This is used for both the encryption and decryption.
e
starts off as e_start
. This will eventually be the part of the encryption key.
m
= (p-1)*(q-1)
is used to convert e
to d
, which will be used for decryption.
while(
gcd
(e,m)>1){e+=2}
This is necessary for the next step to work.
d=
modInverse
(e,m)
This performs a standard arithmatic operation. Probably not worth examining much, especially if your programming language has this built in
To encrypt or decrypt, first convert your bytes to a single arbitrary precision integer.
Encryption: encrypted=(plain^e)%n
Note: If plain>=n
, you must split plain
into two or more smaller values and encrypt them separately.
Decryption: plain=(encrypted^d)%n
Asymmetric encryption is typically less efficient than Symmetric encryption. Sometimes Asymmetric encryption is used only for key exchange.