Math behind RSA

181 Views Asked by At

Now I very recently wrote an investigation paper on RSA Encryption and how it works. After completing it, I thought of looking up real life key generators. To my surprise, they all have characters within them, and not just numbers... Everything I've read till now has been based on the fact that RSA works solely on prime numbers + Euler's Totient function + Modular Arithmetic. I know how to encrypt when the public and private keys are solely numbers, but how is it done with characters?

Just to make it clear, I do know how to use RSA encryption and know the math behind it.

PS: Here are a few links :

https://travistidwell.com/jsencrypt/demo/

https://www.devglan.com/online-tools/rsa-encryption-decryption

2

There are 2 best solutions below

0
On

This is simply a way of encoding numbers.

Consider writing down a number of size $1024$ bits. If we use decimal system, then the number is around $308$ digits long, and hence needs $308$ characters to print out.

Now if we use a higher base such as base 64, then the number of "digits" will be reduced to around $171$, which is much more compact.

In theory, one can use any sort of "number-to-string" encoding, but there are some standard ones that are commonly used.

0
On

Here is another RSA article for your list. One of the problems with some of the programming languages is that their primitive data types have limited sizes. Say the longest integer possible

  • In Java it's long and it's 64 bits
  • In C++ it's long long and is also 64 bits.

Obviously, that's not enough to hold a key of $1024$ bits or $2048$ bits. Java has a "any size" BigInteger, but it's different from the C++ equivalent (or Python). Some sort of a standard is required, for keeping the keys, so that all the languages could follow it. And there are quite a few such standards.

Essentially, big numbers are converted into chunks or arrays of bytes (there are systems with different order of bits, thus I use the word "converted" and thus another reason for standards). However, persisting those bits in binary form, again, isn't always portable (Java arrays and C++ arrays, persisted in binary files, look different). One way to convert binary to "readable" ASCII is base64 encoding. It is used to encode email attachments, because email protocols like POP3, SMTP, IMAP are text based, binary "gibberish" would make the life of the protocol (and the engineers developing and maintaining it) very difficult.

An example, because we are on a maths forum, say a $2048$ bits key would require $2048/8=256$ bytes. But base64 "converts" chunks of 6 bits to "readable" ASCII characters ($2^6=64$, this is the reason for base64 name), thus $2048/6 \approx 342$ characters.