You are on page 1of 13

RAILFENCE Cipher

This is a type of transposition cipher.

This type of cipher is not as strong but due to the variable distances between consecutive letters of the
plain text when encrypted in the cipher text it is relatably reliable. But if we brute force all no. of rows
the plain text can be obtained as length is same.

#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;
}

int main()
{
cout << encryptRailFence("ttyl", 2) << endl;

return 0;
}

One Time Pad Cipher

#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
int i,j,len1,len2,numstr[100],numkey[100],numcipher[100];
char str[100],key[100],cipher[100];
printf("Enter a string text to encrypt\n");
gets(str);
for(i=0,j=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j]=toupper(str[i]);
j++;
}
}
str[j]='\0';
for(i=0;i<strlen(str);i++)
{
numstr[i]=str[i]-'A';
}
printf("Enter key string of random text\n");
gets(key);
for(i=0,j=0;i<strlen(key);i++)
{
if(key[i]!=' ')
{
key[j]=toupper(key[i]);
j++;
}
}
key[j]='\0';
for(i=0;i<strlen(key);i++)
{
numkey[i]=key[i]-'A';
}

for(i=0;i<strlen(str);i++)
{
numcipher[i]=numstr[i]+numkey[i];
}
for(i=0;i<strlen(str);i++)
{
if(numcipher[i]>25)
{
numcipher[i]=numcipher[i]-26;
}
}
printf("One Time Pad Cipher text is\n");
for(i=0;i<strlen(str);i++)
{
printf("%c",(numcipher[i]+'A'));
}
printf("\n");

}
Vigenère cipher (polyalphabetic substitution)

The Vigenère cipher is a method of encrypting messages by using a series of different Caesar ciphers
based on the letters of a particular keyword. The Vigenère cipher is more powerful than a single Caesar
cipher and is much harder to crack.

#include<bits/stdc++.h>

using namespace std;

string cipherText(string str, string key)

string cipher_text;

for (int i = 0; i<str.size(); i++)

int x = (str[i] + key[i] - 64) %26;

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++)

{
int x = (cipher_text[i] - key[i] + 26 + 32) %26;

x += 'a';

orig_text.push_back(x);

return orig_text;

int main()

string str = "wearediscoveredsaveyourself";

string keyword = "deceptivedeceptivedeceptive";

string cipher_text = cipherText(str, keyword);

cout<< "Ciphertext is : "<<cipher_text<< "\n";

cout<< "Original Text is : "<<originalText(cipher_text, keyword);

return 0;

}
Hill Cipher

The Hill cipher is a classical symmetric encryption algorithm that succumbs to the know-
plaintext attack. Although its vulnerability to cryptanalysis has rendered it unusable in practice, it
still serves an important pedagogical role in cryptology and linear algebra.

#include <iostream>

using namespace std;

void encrypt(int cipherMatrix[][1],

int keyMatrix[][3],

int messageVector[][1])

int x, i, j;

for (i = 0; i< 3; i++)

for (j = 0; j < 1; j++)

cipherMatrix[i][j] = 0;
for (x = 0; x < 3; x++)

cipherMatrix[i][j] +=

keyMatrix[i][x] * messageVector[x][j];

cipherMatrix[i][j] = cipherMatrix[i][j] % 26;

void HillCipher(string message, int key[][3])

int messageVector[3][1];

for (int i = 0; i< 3; i++)

messageVector[i][0] = (message[i]) % 65;

int cipherMatrix[3][1];

encrypt(cipherMatrix, key, messageVector);

string CipherText;

for (int i = 0; i< 3; i++)

CipherText += cipherMatrix[i][0] + 65;

cout<< "Ciphertext is: " <<CipherText;

}
int main()

string plaintext = "pay";

int key[3][3] = { {17,17,5},

{21,18,21},

{2, 2, 19} };

cout<<"Key is :"<<endl;

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

cout<<key[i][j]<<" ";

cout<<endl;

cout<<endl;

HillCipher(plaintext, key);

return 0;

}
Caeser Cipher

It is considered a weak method of cryptography, as it is easy to decode the message owing to its
minimum security techniques.

#include<bits/stdc++.h>

using namespace std;

int main()

int key = 3;

char a[100];

cout<<"Enter plain text";

for(int i=0;i<26;i++)

cin>>a[i];

vector<char>v;

char ch;

for(int i=0;i<26;i++)
{

ch = a[i] - 32 + 3;

if(ch>90)

ch = ch-26;

v.push_back(ch);

` int n = v.size();

for(int i=0; i< n; i++)

cout<<v[i];

Transpositional Cipher

#include<bits/stdc++.h>

using namespace std;

int main()

vector<int>v1,v2;

int n;

cout<<"Enter the size of key";


cin>>n;

for(int i=0;i<n;i++)

int e;

cin>>e;

v1.push_back(e);

v2.push_back(e);

int rows;

cout<<"Enter row size of matrix";

cin>>rows;

cout<<endl;

char a[rows][n];

for(int i=0;i<rows;i++)

for(int j=0;j<n;j++)

cin>>a[i][j];

for(int i=0;i<rows;i++)

for(int j=0;j<n;j++)

cout<<a[i][j]<<" ";
}

cout<<endl;

cout<<endl;

int s = v1.size();

int cnt = 0;

while(cnt < s)

int index = min_element(v1.begin(),v1.end()) - v1.begin();

for(int i=0;i<rows;i++)

cout<<a[i][index]<<" ";

v1[index] = INT_MAX;

cnt++;

// v1.erase(find(v1.begin(),v1.end(),e));

//s = v1.size();

//cout<<endl;

return 0;

You might also like