Dual Key Encryption in C++
Dual key encryption, also known as public/private key cryptography, is a core concept in modern data security. It is widely used in secure communications, digital certificates, and encrypted web applications. In this tutorial, you will learn how public and private key encryption works in C++, why it is classified as asymmetric encryption, and how dual keys are used to securely encrypt and decrypt data.
This explanation is designed for tertiary-level students and beginners who want a clear, practical understanding of cryptography concepts without unnecessary complexity.
What Is Dual Key (Public/Private Key) Encryption? | Explanation for C++ Kids
Dual key encryption is an encryption method that uses two mathematically related keys:
- A public key, which is shared openly and used to encrypt data
- A private key, which is kept secret and used to decrypt data
This approach is formally known as public key cryptography or asymmetric encryption. Unlike symmetric encryption, where the same key is used for both encryption and decryption, dual key encryption ensures that encrypted data can only be decrypted by the intended recipient.
Why Asymmetric Encryption Is Important | Explanation for C++ Kids
Asymmetric encryption solves a major problem in secure communication: key sharing. Since the public key can be distributed freely, there is no need to transmit a secret key over an insecure channel.
Key advantages include:
- Improved security for data transmission
- Safe communication over public networks
- Strong authentication mechanisms
- Foundation for HTTPS, SSL/TLS, and digital signatures
Because of these benefits, public/private key encryption is commonly used in web applications, secure messaging systems, and online banking platforms.
How Dual Key Encryption Works (Step by Step) | Explanation for C++ Kids
The dual key encryption process follows a logical sequence:
- A key pair is generated (public key and private key)
- The sender encrypts data using the public key
- The encrypted message is transmitted
- The recipient decrypts the message using the private key
Only the matching private key can decrypt the data, even though the public key is visible to everyone. This is what makes asymmetric cryptography secure.
C++ Example: Public and Private Key Encryption
In C++, asymmetric encryption can be implemented using cryptographic libraries or the Web Crypto API. The general workflow remains the same regardless of the specific implementation.
A typical C++ public/private key encryption process includes:
- Generating an RSA key pair
- Encrypting data with the public key
- Decrypting encrypted data with the private key
This approach allows C++ applications to securely handle sensitive information such as passwords, tokens, and confidential messages.
The Mathematics of Public Key Encryption | Math Explanation for C++ Kids
At its core, dual key encryption relies on the mathematical difficulty of factoring large numbers. Our C++ RSA encryption tutorial focuses on the relationship between prime numbers and modular arithmetic. To generate a secure pair of keys, we follow a rigorous mathematical process:
- Prime Selection: Choosing two large prime numbers ( and ) to create a semi-prime modulus.
- LCM Calculation: Determining the Lowest Common Multiple of $(p-1)$ and $(q-1)$.
- Key Generation: Selecting a Public Key that is coprime to the LCM.
- The Inverse: Calculating the Private Key as the modular multiplicative inverse of the Public Key.
- Encrypt data: \([Unicode(data)]^{public\_key} % semi_prime = encoded_data;\)
- Decrypt data: \([encoded\_data]^{private\_key} % semi_prime = original data;\)
Implementing RSA Logic in C++
While many modern applications use the built-in WebCrypto API, writing a manual RSA implementation is the best way to grasp how ciphertext and plaintext interact.
Using C++'s BigInteger capability, we can handle the large-scale integer calculations required for secure encryption. Below, we explore the C++ code required to transform a standard Unicode string into an encrypted hexadecimal array, ensuring that only the holder of the private key can reverse the process.
Create a new C++ class file;
Call it DualKeyEncryption
.
Type out the adjoining C++ code for encrypting and decrypting a chunk of data
using a Public Key - Private Key pair.
By The Way: The encryption algorithm described in the above steps is called R.S.A. algorithm; and coming up with an algorithm that can factor very large semi primes into their prime factors in linear time is called the R.S.A. problem.
Also noteworthy is the fact that there are other ways of implementing an open-lock-only encryption algorithm; like the Logarithm Encryption, e.t.c.
Important: We obligatorily wrote a BigInteger class
for use with this section.
It is freely available on Github
or you can download it as a zip file here.
The C++ algorithm class for finding LCM
has been explained in the Primary Category.
Create a new C++ class file called LCM in your current
project and copy the L.C.M. code into it.
Why Use RSA in C++?
- Educational Value: Great for learning cryptography concepts.
- Practical Applications: Secure login systems, encrypted messaging, and digital signatures.
- Flexibility: C++ runs everywhere — windows, linux, and android.
Dual Key Encryption vs Symmetric Encryption | Explanation for C++ Kids
It is important to distinguish between symmetric encryption and dual key encryption:
| Feature | Symmetric Encryption | Dual Key Encryption |
|---|---|---|
| Number of keys | One shared key | Two separate keys |
| Security | Lower for key exchange | Higher for communication |
| Speed | Faster | Slower |
| Common use | Data storage | Secure communication |
In practice, many systems use both methods together, combining the speed of symmetric encryption with the security of asymmetric encryption.
Common Uses of Public/Private Key Cryptography | Explanation for C++ Kids
Public and private key encryption is used in many real-world applications, including:
- Secure web communication (HTTPS)
- Digital certificates and authentication
- Secure email systems
- Encrypted file sharing
- Secure API communication
Understanding dual key encryption in C++ provides a strong foundation for learning advanced security and cryptography topics.
Key Takeaways from the C++ Dual Key Encryption Algorithm
- Dual key encryption uses a public key for encryption and a private key for decryption
- It is also known as public/private key cryptography or asymmetric encryption
- C++ supports public key encryption through cryptographic APIs and libraries
- This method is essential for secure communication on the web
Summary: C++ Dual Key Encryption Algorithm
By understanding RSA encryption in C++, you gain insight into how modern cryptography protects sensitive information. Whether you're a student exploring modular arithmetic or a developer implementing public/private key cryptography, this tutorial provides the foundation you need.
C++ Code for Dual Key Encryption - Header File
#include "BigInteger.h"
#include <string>
#include <vector>
using namespace std;
class DualKeyEncryption
{
public:
DualKeyEncryption(int);
virtual ~DualKeyEncryption();
vector<string> encodeWord(vector<char>, int);
string decodeWord(vector<string>, int);
private:
string semi_prime;
};
C++ Code for Dual Key Encryption - Class File
#include "DualKeyEncryption.h"
DualKeyEncryption::DualKeyEncryption(int semi_p)
{
semi_prime = to_string(semi_p);
}
/*
* STEP VI:
*/
vector<string> DualKeyEncryption::encodeWord(vector<char> msg, int key) {
vector<string> encryption = {};
string x;
for (size_t i = 0; i < msg.size(); i++) {
// get unicode of this character as x
x = to_string((int)msg[i]);
// use RSA to encrypt & save in base 16
encryption.push_back(BigInteger::decToHex(BigInteger::powerModulus(x, key, semi_prime)));
}
return encryption;
}
/*
* STEP VII:
*/
string DualKeyEncryption::decodeWord(vector<string> code, int key) {
string decryption = "";
string char_string;
int c;
for (size_t i = 0; i < code.size(); i++) {
char_string = "";
// use RSA to decrypt
c = stoi(BigInteger::powerModulus(BigInteger::hexToDec(code[i]), key, semi_prime));
char_string += (char)c;
decryption += char_string;
}
return decryption;
}
DualKeyEncryption::~DualKeyEncryption()
{
}
C++ Code for Dual Key Encryption - Main Class
#include "LCM.h"
#include "DualKeyEncryption.h"
#include <iostream>
using namespace std;
int main() {
/*
* STEP I:
*/
unsigned p1 = 101; // 1st prime
unsigned p2 = 401; // 2nd prime
/*
* STEP II:
*/
int semi_prime = p1 * p2;
/*
* STEP III:
*/
// get L.C.M. of p1-1 and p2-1
LCM l_c_m({ p1 - 1, p2 - 1 });
int lcm = l_c_m.getLCM();
/*
* STEP IV:
*/
// pick a random prime (public_key) that lies
// between 1 and LCM but not a factor of LCM
int public_key = 313;
// find 'public_key' complement - private_key - such that
// (public_key * private_key) % LCM = 1
//this involves some measure of trial and error
int i = 1;
while ((lcm * i + 1) % public_key != 0) {
i++;
}
/*
* STEP V:
*/
int private_key = (i * lcm + 1) / public_key;
vector<char> message = { 'm', 'e', 'r', 'r', 'y', ' ', 'x', 'm', 'a', 's' };
DualKeyEncryption go_secure(semi_prime);
vector<string> encrypted = go_secure.encodeWord(message, public_key);
string disp = "", msg = "";
for (size_t i = 0; i < encrypted.size(); i++) {
disp += encrypted[i] + "; ";
msg += message[i];
}
cout << "Message is '" << msg << "';\nEncrypted version is " << disp << endl;
string decrypted = go_secure.decodeWord(encrypted, private_key);
cout << "\n\nDecrypted version is '" << decrypted << "'.";
return 0;
}