C++ Program to Encrypt and Decrypt the String (Source Code Explained) (2024)

C++ program to encrypt and decrypt the string

In this example, you will learn simple C++ program to encrypt and decrypt the string using two different encryption algorithms i.e. Caesar Cypher and RSA.

Encryption/Decryption using Caesar Cypher Algorithm
Encryption/Decryption using RSA Algorithm

C++ Program to Encrypt and Decrypt the String (Source Code Explained) (1)

Encryption basically means encoding a particular message or information so that it can’t be read by other person and decryption is the process of decoding that message to make it readable.

Method 1: C++ program to encrypt and decrypt the string using Caesar Cypher Algorithm.

We have used a simple method of adding and subtracting a key value for encryption and decryption.

For encrypting a string, key-value ‘2’ is added to the ASCII value of the characters in the string.

Similarly, for decrypting a string, key-value ‘2’ is subtracted from the ASCII value of the characters.

Let’s take a deeper look at the source code:

//Simple C++ program to encrypt and decrypt a string#include <iostream>using namespace std;int main(){ int i, x; char str[100]; cout << "Please enter a string:\t"; cin >> str; cout << "\nPlease choose following options:\n"; cout << "1 = Encrypt the string.\n"; cout << "2 = Decrypt the string.\n"; cin >> x; //using switch case statements switch(x) { //first case for encrypting a string case 1: for(i = 0; (i < 100 && str[i] != '\0'); i++) str[i] = str[i] + 2; //the key for encryption is 3 that is added to ASCII value cout << "\nEncrypted string: " << str << endl; break; //second case for decrypting a string case 2: for(i = 0; (i < 100 && str[i] != '\0'); i++) str[i] = str[i] - 2; //the key for encryption is 3 that is subtracted to ASCII value cout << "\nDecrypted string: " << str << endl; break; default: cout << "\nInvalid Input !!!\n"; } return 0;}

Output

#Encrypting
C++ Program to Encrypt and Decrypt the String (Source Code Explained) (2)

#Decrypting
C++ Program to Encrypt and Decrypt the String (Source Code Explained) (3)

Explanation

In the above program, if you change the value of key then the encrypted value will be different.

If the user enters other value than 1 or 2 it will show Invalid Input.

Method 2: C++ program to encrypt and decrypt the string using RSA algorithm.

RSA algorithm is bit complex than Ceaser Cypher. It involves the use of public and private key, where the public key is known to all and used for encryption.

On the other hand, Private key is only used to decrypt the encrypted message.

Here is the deeper look at the steps of encryption algorithm:

1: Creating Keys

  • Select two large prime numbers x and y
  • Compute n = x * y
    where n is the modulus of private and the public key
  • Calculate totient function, ø (n) = (x − 1)(y − 1)
  • Choose an integer e such that e is coprime toø(n) and 1 < e < ø(n).
    e is the public key exponent used for encryption
  • Now choose d, so that d · e mod ø (n) = 1, i.e., >code>d is the multiplicative inverse of e in mod ø (n)

2: Encrypting Message

Messages are encrypted using the Public key generated and is known to all.

The public key is the function of bothe and n i.e.{e,n}.

If M is the message(plain text), then ciphertext

C = M ^ n( mod n )

3: Decrypting Message

The private key is the function of both d and n i.e {d,n}.

If C is the encrypted ciphertext, then the plain decrypted text M is

M = C ^ d ( mod n )

You might be wondering how to write a source code for this program.

So let’s look at the program.

//C++ program for encryption and decryption#include<iostream>#include<stdlib.h>#include<math.h>#include<string.h>using namespace std;int x, y, n, t, i, flag;long int e[50], d[50], temp[50], j;char en[50], m[50];char msg[100];int prime(long int); //function to check for prime numbervoid encryption_key();long int cd(long int);void encrypt();void decrypt();int main(){ cout << "\nENTER FIRST PRIME NUMBER\n"; cin >> x; //checking whether input is prime or not flag = prime(x); if(flag == 0) { cout << "\nINVALID INPUT\n"; exit(0); } cout << "\nENTER SECOND PRIME NUMBER\n"; cin >> y; flag = prime(y); if(flag == 0 || x == y) { cout << "\nINVALID INPUT\n"; exit(0); } cout << "\nENTER MESSAGE OR STRING TO ENCRYPT\n"; cin >> msg; for(i = 0; msg[i] != NULL; i++) m[i] = msg[i]; n = x * y; t = (x - 1) * (y - 1); encryption_key(); cout << "\nPOSSIBLE VALUES OF e AND d ARE\n"; for(i = 0; i < j - 1; i++) cout << "\n" << e[i] << "\t" << d[i]; encrypt(); decrypt(); return 0;} //end of the main programint prime(long int pr){ int i; j = sqrt(pr); for(i = 2; i <= j; i++) { if(pr % i == 0) return 0; } return 1; }//function to generate encryption keyvoid encryption_key(){ int k; k = 0; for(i = 2; i < t; i++) { if(t % i == 0) continue; flag = prime(i); if(flag == 1 && i != x && i != y) { e[k] = i; flag = cd(e[k]); if(flag > 0) { d[k] = flag; k++; } if(k == 99) break; } }}long int cd(long int a){ long int k = 1; while(1) { k = k + t; if(k % a == 0) return(k/a); }}//function to encrypt the messagevoid encrypt(){ long int pt, ct, key = e[0], k, len; i = 0; len = strlen(msg); while(i != len) { pt = m[i]; pt = pt - 96; k = 1; for(j = 0; j < key; j++) { k = k * pt; k = k % n; } temp[i] = k; ct= k + 96; en[i] = ct; i++; } en[i] = -1; cout << "\n\nTHE ENCRYPTED MESSAGE IS\n"; for(i=0; en[i] != -1; i++) cout << en[i];}//function to decrypt the messagevoid decrypt(){ long int pt, ct, key = d[0], k; i = 0; while(en[i] != -1) { ct = temp[i]; k = 1; for(j = 0; j < key; j++) { k = k * ct; k = k % n; } pt = k + 96; m[i] = pt; i++; } m[i] = -1; cout << "\n\nTHE DECRYPTED MESSAGE IS\n"; for(i = 0; m[i] != -1; i++) cout << m[i]; cout << endl;}

Output

C++ Program to Encrypt and Decrypt the String (Source Code Explained) (4)

This is all about encryption and decryption program in C++.

C++ Program to Encrypt and Decrypt the String (Source Code Explained) (2024)
Top Articles
Wart Liquid Topical: Uses, Side Effects, Interactions, Pictures, Warnings & Dosing - WebMD
What Is a Proof Coin? Definition and How to Identify Proofs
Katie Pavlich Bikini Photos
Trevor Goodwin Obituary St Cloud
Was ist ein Crawler? | Finde es jetzt raus! | OMT-Lexikon
Ets Lake Fork Fishing Report
DENVER Überwachungskamera IOC-221, IP, WLAN, außen | 580950
Call Follower Osrs
Optimal Perks Rs3
Achivr Visb Verizon
Ohiohealth Esource Employee Login
Buying risk?
Healing Guide Dragonflight 10.2.7 Wow Warring Dueling Guide
The fabulous trio of the Miller sisters
Bitlife Tyrone's
Sky X App » downloaden & Vorteile entdecken | Sky X
Michael Shaara Books In Order - Books In Order
Leader Times Obituaries Liberal Ks
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
Missouri Highway Patrol Crash
Ubg98.Github.io Unblocked
Where Is George The Pet Collector
FDA Approves Arcutis’ ZORYVE® (roflumilast) Topical Foam, 0.3% for the Treatment of Seborrheic Dermatitis in Individuals Aged 9 Years and Older - Arcutis Biotherapeutics
Azpeople View Paycheck/W2
Toyota Camry Hybrid Long Term Review: A Big Luxury Sedan With Hatchback Efficiency
Reptile Expo Fayetteville Nc
Isaidup
Stihl Dealer Albuquerque
Reicks View Farms Grain Bids
Dexter Gomovies
Biografie - Geertjan Lassche
Joann Fabrics Lexington Sc
Lesson 1.1 Practice B Geometry Answers
Rund um die SIM-Karte | ALDI TALK
Wake County Court Records | NorthCarolinaCourtRecords.us
Gabrielle Enright Weight Loss
A Man Called Otto Showtimes Near Carolina Mall Cinema
Metro By T Mobile Sign In
Hingham Police Scanner Wicked Local
Raising Canes Franchise Cost
Walgreens Agrees to Pay $106.8M to Resolve Allegations It Billed the Government for Prescriptions Never Dispensed
Second Chance Apartments, 2nd Chance Apartments Locators for Bad Credit
“To be able to” and “to be allowed to” – Ersatzformen von “can” | sofatutor.com
Wunderground Orlando
Man Stuff Idaho
Thor Majestic 23A Floor Plan
Sarahbustani Boobs
Deepwoken: How To Unlock All Fighting Styles Guide - Item Level Gaming
Best Haircut Shop Near Me
Germany’s intensely private and immensely wealthy Reimann family
Diccionario De Los Sueños Misabueso
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 6120

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.