NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

**********Caeser Cipher**********
#include <stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to encrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for (i = 0; message[i] != ''; ++i)
{
ch = message[i];
if (ch >= 'a' && ch <= 'z')
{
ch = ch + key;
if (ch > 'z')
{
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if (ch >= 'A' && ch <= 'Z')
{
ch = ch + key;
if (ch > 'Z')
{
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
printf("Encrypted message: %s", message);
return 0;
}
********MONOALPHABETIC**********
#include<bits/stdc++.h>
using namespace std;
int main() {
char pt[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z'};
char ct[26] = {'Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P',
'O', 'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B',
'A'};
string plain, cipher = "", decipher = "";
int i, j;
cout << "Enter Plain text : ";
getline(cin, plain);
int flg = 0;
if (islower(plain[0])) {
flg = 1;
}
transform(plain.begin(), plain.end(), plain.begin(), ::toupper);
int k = 0;
for (i = 0; i < plain.size(); i++) {
if (plain[i] == ' ') {
cipher += ' ';
continue;
}
for (j = 0; j < 26; j++) {
if (pt[j] == plain[i]) {
cipher += ct[j];
}
}
}
if (flg) {
string t = cipher;
transform(t.begin(), t.end(), t.begin(), ::tolower);
cout << "After Encryption: " << t << endl;
}
else
cout << "After Encryption: " << cipher << endl;
k = 0;
for (i = 0; i < cipher.size(); i++)
{
if (cipher[i] == ' ') {
decipher += ' ';
continue;
}
for (j = 0; j < 26; j++)
{
if (ct[j] == cipher[i])
{
decipher += pt[j];
}
}
}
if (flg) {
string t = decipher;
transform(t.begin(), t.end(), t.begin(), ::tolower);
cout << "After Decryption: " << t << endl;
}
else
cout << "After Decryption: " << decipher << endl;
}


*********Hill Cipher**********
#include <stdio.h>
#include <string.h>
int main()
{
unsigned int a[3][3] = {{6, 24, 1}, {13, 16, 10}, {20, 17, 15}};
unsigned int b[3][3] = {{8, 5, 10}, {21, 8, 21}, {21, 12, 8}};
int i, j;
unsigned int c[20], d[20];
char msg[20];
int determinant = 0, t = 0;
printf("Enter plain text: ");
scanf("%s", msg);
for (i = 0; i < 3; i++)
{
c[i] = msg[i] - 65;
printf("%d ", c[i]);
}
for (i = 0; i < 3; i++)
{ t = 0;
for (j = 0; j < 3; j++)
{
t = t + (a[i][j] * c[j]);
}
d[i] = t % 26;
}
printf("nEncrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", d[i] + 65);
for (i = 0; i < 3; i++)
{
t = 0;
for (j = 0; j < 3; j++)
{
t = t + (b[i][j] * d[j]);
}
c[i] = t % 26;
}
printf("nDecrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", c[i] + 65);
return 0;
}


***********Polyaphabetic*************

#include<bits/stdc++.h>
using namespace std;
string generateKey(string str, string key)
{
int x = str.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}
string cipherText(string str, string key)
{
string cipher_text;
for (int i = 0; i < str.size(); i++)
{
// converting in range 0-25
char x = (str[i] + key[i]) %26;
// convert into alphabets(ASCII)
x += 'A';
cipher_text.push_back(x);
}
return cipher_text;
}
string originalText(string cipher_text, string key)
{
string orig_text;
for (int i = 0 ; i < cipher_text.size(); i++)
{
// converting in range 0-25

char x = (cipher_text[i] - key[i] + 26) %26;
// convert into alphabets(ASCII)
x += 'A';
orig_text.push_back(x);
}
return orig_text;
}
int main()
{
string str;
string keyword;
cout<<"Enter Plain text: ";
cin>>str;
cout<<"Enter key: ";
cin>>keyword;
string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);
cout << "Ciphertext : "
<< cipher_text << "n";
cout << "Original/Decrypted Text : "
<< originalText(cipher_text, key);
return 0;
}

*********Railfence************
#include <bits/stdc++.h>
using namespace std;
string encryptRailFence(string text, int key)
{
char rail[key][(text.length())];
for (int i = 0; i < key; i++)
for (int j = 0; j < text.length(); j++)
rail[i][j] = 'n';
bool dir_down = false;
int row = 0, col = 0;
for (int i = 0; i < text.length(); i++)
{
if (row == 0 || row == key - 1)
dir_down = !dir_down;
rail[row][col++] = text[i];
dir_down ? row++ : row--;
}
string result;
for (int i = 0; i < key; i++)
for (int j = 0; j < text.length(); j++)
if (rail[i][j] != 'n')
result.push_back(rail[i][j]);
return result;
}
string decryptRailFence(string cipher, int key)
{
char rail[key][cipher.length()];
for (int i = 0; i < key; i++)
for (int j = 0; j < cipher.length(); j++)
rail[i][j] = 'n';
bool dir_down;
IU2041050128 | PARAM SUTHAR
Page | 15
int row = 0, col = 0;
for (int i = 0; i < cipher.length(); i++)
{
// check the direction of flow
if (row == 0)
dir_down = true;
if (row == key - 1)
dir_down = false;
rail[row][col++] = '*';
dir_down ? row++ : row--;
}
int index = 0;
for (int i = 0; i < key; i++)
for (int j = 0; j < cipher.length(); j++)
if (rail[i][j] == '*' && index < cipher.length())
rail[i][j] = cipher[index++];
string result;
row = 0, col = 0;
for (int i = 0; i < cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key - 1)
dir_down = false;
if (rail[row][col] != '*')
result.push_back(rail[row][col++]);
dir_down ? row++ : row--;
}
return result;
}
int main()
{
cout<<"Plain Text: Cryptography n";
cout <<"After Encryption: "<< encryptRailFence("Cryptography ", 3) <<
endl;
cout <<"After Decryption: "<< decryptRailFence("Cta rporpyygh", 3) <<
endl;
return 0;
}

************RSA******************
#include<iostream>
#include<math.h>
using namespace std;

int gcd(int a, int b) {
int t;
while(1) {
t= a%b;
if(t==0)
return b;
a = b;
b= t;
}
}

int main() {
double p = 13;
double q = 11;
double n=p*q;
double track;
double phi= (p-1)*(q-1);

double e=7;
while(e<phi) {
track = gcd(e,phi);
if(track==1)
break;
else
e++;
}

double d1=1/e;
double d=fmod(d1,phi);
double message = 9;
double c = pow(message,e);
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
cout<<"Original Message = "<<message;
cout<<"n"<<"First prime number, p = "<<p;
cout<<"n"<<"Second prime number, q = "<<q;
cout<<"n"<<"n = p*q = "<<n;
cout<<"n"<<"phi = (p-1)*(q-1) = "<<phi;
cout<<"n"<<"encrypt = "<<e;
cout<<"n"<<"decrypt = "<<d;
cout<<"n"<<"Encrypted message = "<<c;
cout<<"n"<<"Decrypted message = "<<m;
return 0;
}

**********Diffie-hellmen************
#include <iostream>
#include <cmath>
using namespace std;

long long int power(long long int a, long long int b, long long int P)
{
if (b == 1)
return a;
else
return (((long long int)pow(a, b)) % P);
}

int main()
{
long long int P, G, x, a, y, b, ka, kb;
P = 23;
cout << "A prime number P : " << P << endl;
G = 9;
cout << "A primitive root for P, G : " << G << endl;

a = 4;
cout << "The private key a for Person1 : " << a << endl;
x = power(G, a, P);

b = 3;
cout << "The private key b for Person2 : " << b << endl;
y = power(G, b, P);

ka = power(y, a, P);
kb = power(x, b, P);

cout << "Secret key for the Person1 is : " << ka << endl;
cout << "Secret key for the Person2 is : " << kb << endl;
return 0;
}



     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.