From d0a68b1dc79909be7c3271fba2cc0cd31bc2cfa9 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Fri, 2 Oct 2020 17:48:46 +0530 Subject: [PATCH 1/2] Create Vigener_cipher.cpp Added C++ code for vignere cipher --- CPP/Ciphers/Vigener_cipher.cpp | 81 ++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 CPP/Ciphers/Vigener_cipher.cpp diff --git a/CPP/Ciphers/Vigener_cipher.cpp b/CPP/Ciphers/Vigener_cipher.cpp new file mode 100644 index 0000000..cae2570 --- /dev/null +++ b/CPP/Ciphers/Vigener_cipher.cpp @@ -0,0 +1,81 @@ +//Vigenere Cipher +#include +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==="<>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) <>ch; + }while(ch!='n'); + return 0; +} From 4d3d6881871fb129e5dfe21f33dca757438373e0 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Fri, 2 Oct 2020 17:50:41 +0530 Subject: [PATCH 2/2] Create Multiplicative_cipher Added C++ implementation for multiplicative cipher --- CPP/Ciphers/Multiplicative_cipher | 74 +++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 CPP/Ciphers/Multiplicative_cipher diff --git a/CPP/Ciphers/Multiplicative_cipher b/CPP/Ciphers/Multiplicative_cipher new file mode 100644 index 0000000..a03d570 --- /dev/null +++ b/CPP/Ciphers/Multiplicative_cipher @@ -0,0 +1,74 @@ +//Multiplicative Cipher +#include +#include +#include +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==="<>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)<>key; + cout << "Decrypted Text is(Plain Text) : " << decrypt(text, key)<>ch; + }while(ch!='n'); + return 0; +}