Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions CPP/Ciphers/Multiplicative_cipher
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//Multiplicative Cipher
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
string encrypt(string text, int key) {
string result = "";
for (int i = 0; i < text.length(); i++)
{
if (isupper(text[i]))
result +=(char) (((key * (text[i]-65)) % 26) + 65);
else
result +=(char) (((key * (text[i]-97)) % 26) + 97);
}
return result;
}

string decrypt(string text, int key) {
string result= "";
int a_inv = 0;
int flag = 0;
for (int i = 0; i < 26; i++)
{
flag = (key * i) % 26;
if (flag == 1)
{
a_inv = i;
}
}
for (int i = 0; i < text.length(); i++)
{
if (isupper(text[i]))
result += (char) (((a_inv * (text[i]-65)) % 26) + 65);
else
result += (char) (((a_inv * (text[i]-97)) % 26) + 97);
}
return result;
}
int main()
{
string text="";
int key = 0, sel=0;
char ch='y';
cout<<"===MULTIPLICATIVE CIPHER==="<<endl;
do {
cout<<"\nMenu\n1.Encrypt\n2.Decrypt\n";
cout << "Enter the input : ";
cin>>sel;
switch(sel) {
case 1:
cout << "Enter the text : ";
cin.ignore();
getline(cin,text);
cout<< "Enter the key : ";
cin>>key;
cout << "Encrypted Text is(Cipher Text) : " << encrypt(text, key)<<endl;
break;
case 2:
cout << "Enter the text : ";
cin.ignore();
getline(cin,text);
cout<< "Enter the key : ";
cin>>key;
cout << "Decrypted Text is(Plain Text) : " << decrypt(text, key)<<endl;
break;
default:
cout<<"Enter the correct option";
break;
}
cout<<"If you wish to exit press n else press any key: ";
cin>>ch;
}while(ch!='n');
return 0;
}
81 changes: 81 additions & 0 deletions CPP/Ciphers/Vigener_cipher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//Vigenere Cipher
#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 encrypt(string str, string key)
{
string cipher_text;

for (int i = 0; i < str.size(); i++)
{
char x = (((str[i] + key[i]) %26) + 65);
cipher_text.push_back(x);
}
return cipher_text;
}

string decrypt(string cipher_text, string key)
{
string orig_text;

for (int i = 0 ; i < cipher_text.size(); i++)
{
char x = (((cipher_text[i] - key[i] + 26) %26) +65);
orig_text.push_back(x);
}
return orig_text;
}

int main()
{

string text="", keyword="", key="";
int sel=0;
char ch='y';
cout<<"===VIGENERE CIPHER==="<<endl;

do {
cout<<"\nMenu\n1.Encrypt\n2.Decrypt\n";
cout << "Enter the input : ";
cin>>sel;
switch(sel) {
case 1:
cout << "Enter the text : ";
cin.ignore();
getline(cin,text);
cout<< "Enter the key: ";
getline(cin,keyword);
key = generateKey(text,keyword);
cout << "Encrypted Text is(Cipher Text) : " << encrypt(text,key) <<endl;
break;
case 2:
cout << "Enter the text : ";
cin.ignore();
getline(cin,text);
cout<<"Using the key provided earlier"<<endl;
key = generateKey(text,keyword);
cout << "Decrypted Text is(Plain Text) : " << decrypt(text, key) <<endl;
break;
default:
cout<<"Enter the correct option";
break;
}
cout<<"If you wish to exit press n else press any key: ";
cin>>ch;
}while(ch!='n');
return 0;
}