Implementing RSA Cryptography (C++)

Search for a command to run...

No comments yet. Be the first to comment.
Transferring files between your local machine and a virtual machine (VM) is one of the most common tasks in tech. We often use scp because it comes natively installed and has similar utilization to ssh, but it is the clunkiest tool for the job. In th...

Alias Alchemy is a Deno-powered server that fetches aliases from a GitHub repository based on your query. Think of it as your instant alias spellbook, perfect for setting up new environments in seconds. ⚡ Usage: $ alias-alchemy.ra101.dev/?q=py,shell,...

Reader discretion is advised!A good Sus-Saturday to all. In this article, I drag and drown you with my paranoia about AI. Let the mass psychosis begin! What are AI hallucinations?AI hallucinations are incorrect or misleading results that AI models ge...

I recently joined a company, that follows squash-rebase flow here, and as my Git KT was going on, I figured I should build these commands before actually starting development, now that I have started development, these turned out to be quite handy! ...

Aim: To create an iterable blockchain, which could work with an extendable transaction class. Here the link GitHub repo! Blockchain in Brief: Blockchain is fundamentally a ledger maintaining records of transaction, This ledger is publicly distribute...

On this page
To create a working RSA code, Using namespace ra::random_prime_engine, Here the link GitHub repo!
The Rivest-Shamir-Adleman (RSA) is an asymmetric encryption algorithm. Asymmetric Encryption is when a box(data) can be locked by one key and unlocked by other. locking key cannot unlock the box and vice-versa. The locking key is the public key and the unlocking key is the private key, Public key is called so because we publicly distribute it, so anyone/everyone would send the user an encrypted msg, and the user can unlock it in private with the private key. By keeping private to the user-self, no hacker can get a method of unlocking it.
https://ra101.hashnode.dev/rsa-encryption-algorithm
private_keypublic_keycreate_key(long seed)Constructor & Constructor(seed)std::size_t decrypt(std::size_t);std::size_t decrypt_with_padding(std::string);Key Gettersstd::size_t sign(Message message)namespace but Outside class (All the functions utilizing public key):std::size_t encrypt(std::size_t digest, public_key);std::string encrypt_with_padding(std::size_t digest, public_key);bool verify(Message message, size_t digest, public_key)string padding(string)The seed in the constructor is used in random prime engine and the logic of the seed argument is as follow,
# pseudo code implemented in python
def constructor(seed=None):
if not seed:
seed = timestamp()
else:
seed = hash(seed)
return create_key(seed)
// Usage
ra::rsa_key_pair k1, k2("127.0.0.1");
create_key(seed) function,I will not get into the mathematics of it all, but in the end, using 2 primary numbers we get
(n, e, d) all are integers.
To concatenate these two numbers (n, e/d) let us define a big number BIG_NO, such that
#define BIG_NO 100000000000
public_key = n * BIG_NO + e;
private_key = n * BIG_NO + d;
encrypt(digest, key) function,return (digest ** e) % n
decrypt(encrypted_data) function,return (encrypted_data ** d) % n
padding(string str) function,#define PAD_SIZE 10
// 12345 -> 0000012345
while (str.length() < PAD_SIZE)
{
str = '0' + str; // add 0 in front of no.
}
return str;
encrypt_with_padding(digest, key) function,# encrypt each digit
for each_digit in digest:
output = padding(encrypt(each_digit)) + output
return output
decrypt_with_padding(encrypted_data) function,# cut the input string in chunks of length PAD_SIZE
# decrypt each chunk to digit and then form a output
for i_chunk in len(encrypted_data)/PAD_SIZE:
i = i_chunk * PAD_SIZE
digit = decrypt(int(encrypted_data[i: i + PAD_SIZE]))
output = digit + output * 10
return output
sign(Message message) function,return decrypt(hash(message))
verify(Message message, digest, others_public_key) function,return ( hash(message) == encrypt(digest, others_public_key) )
All the code can be found at the below link: https://github.com/ra101/RSA-cpp