//Pranav shelake
//06
#include <bits/stdc++.h>
#include <vector>
using namespace std;
string playfair[5][5]; // main playfair matrix
int ij_row=0, ij_col=0; // this will hold the index of I/J
string valid_plaintext(string keyword){ // it adds 'X' if any duplicate occurs
string new_keyword;
for(int i = 0; keyword[i] != '\0'; i+=2){
if(keyword[i+1]=='\0'){
new_keyword.push_back(keyword[i]);
new_keyword.push_back('X');
}
else if(keyword[i]!=keyword[i+1]){
new_keyword.push_back(keyword[i]);
new_keyword.push_back(keyword[i+1]);
}
else{
new_keyword.push_back(keyword[i]);
new_keyword.push_back('X');
i--;
}
}
return new_keyword;
}
string valid_decrypt_plaintext(string plaintext){
string new_keyword;
for(int i = 0; plaintext[i] != '\0'; i++){
if((plaintext[i]=='X') && i>0){
if(plaintext[i-1]==plaintext[i+1]){
continue;
}
else{
new_keyword.push_back(plaintext[i]);
}
}
else{
new_keyword.push_back(plaintext[i]);
}
}
return new_keyword;
}
void check(int &col1, int &col2){
if(col1==-1) col1=4;
if(col2==-1) col2=4;
if(col1==5) col1=0;
if(col2==5) col2=0;
}
void settext(char first, char second, string &ciphertext, int k){
int first_i, second_i, first_j, second_j;
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
if(first==playfair[i][j][0]){
first_i=i;
first_j=j;
}
if(second==playfair[i][j][0]){
second_i=i;
second_j=j;
}
}
}
if(first==playfair[ij_row][ij_col][2]){
first_i=ij_row;
first_j=ij_col;
}
if(second==playfair[ij_row][ij_col][2]){
second_i=ij_row;
second_j=ij_col;
}
if(first_i==second_i){ // same row then increase column
first_j=first_j+k;
second_j=second_j+k;
check(first_j, second_j);
}
else if(first_j==second_j){ // same column then increase row
first_i=first_i+k;
second_i=second_i+k;
check(first_i, second_i);
}
else{
int temp=first_j; // rectangle box
first_j=second_j;
second_j=temp;
}
ciphertext.push_back(playfair[first_i][first_j][0]);
ciphertext.push_back(playfair[second_i][second_j][0]);
}
void encrypt(string plaintext, string &ciphertext, int k){
for(int i=0; i<plaintext.size(); i+=2){
settext(plaintext[i], plaintext[i+1], ciphertext, k);
}
}
void make_matrix(vector<string> &playfair, string keyword) {
int check_ij=1;
for (int i = 0; i < keyword.size(); i++) {
int flag = 1;
for (int j = 0; j < playfair.size(); j++) {
if(playfair[j] == keyword.substr(i, 1)){
flag = 0;
break;
}
}
if(flag) {
if(keyword[i]=='I' || keyword[i]=='J') {
if(check_ij){
check_ij=0;
playfair.push_back("I/J");
}
}
else{
playfair.push_back(keyword.substr(i, 1));
}
}
}
for(char i = 'A'; i <= 'Z'; i++) {
int flag = 1;
for(int j = 0; j < playfair.size(); j++) {
if(playfair[j] == string(1, i)) {
flag = 0;
break;
}
}
if(i=='I' || i=='J') {
if(check_ij){
playfair.push_back("I/J");
check_ij=0;
}
}
else if(flag) {
playfair.push_back(string(1, i));
}
}
}
void fill_playfair(vector<string> arr){
int k=0;
cout<<"Playfair matrix: "<<endl<<endl;
for(int i=0; i<5; i++){
for (int j=0; j<5; j++) {
if(arr[k]=="I/J") {
ij_row=i;
ij_col=j;
}
playfair[i][j]=arr[k++];
cout<<playfair[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
int main() {
string key="BALLOON";
cout<<"Enter KEY: ";
// cin>>key;
for (char &i : key) {
i= toupper(i);
}
cout<<"KEYWORD: "<<key<<endl<<endl;
vector<string> arr;
make_matrix(arr, key); // making matrix in 1D array
fill_playfair(arr);
string plaintext="kitcollege";
// string plaintext;
// cin>>plaintext;
for (char &i : plaintext) {
i= toupper(i);
}
cout<<"PLAINTEXT: "<<plaintext<<endl;
plaintext=valid_plaintext(plaintext);
cout<<"VALID PLAINTEXT: "<<plaintext<<endl;
string ciphertext;
encrypt(plaintext, ciphertext, 1);
cout<<"CIPHERTEXT: "<<ciphertext<<endl;
string new_plaintext;
encrypt(ciphertext, new_plaintext, -1);
cout<<"DECRYPTED PLAINTEXT: "<<new_plaintext<<endl;
new_plaintext=valid_decrypt_plaintext(new_plaintext);
cout<<"VALID PLAINTEXT: "<<new_plaintext<<endl;
return 0;
} Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}