0% found this document useful (0 votes)
18 views31 pages

Codes For Cryptography

The document provides various implementations of cryptographic algorithms in C++, including Caesar Cipher, Vernam Cipher, Playfair Cipher, Single and Double Transposition Ciphers, Substitution Ciphers, Polyalphabetic Cipher (Vigenère Cipher), and Hill Cipher. Each section includes code snippets for encryption and decryption processes, along with user input prompts for plaintext and keys. The document serves as a practical guide for understanding and implementing these cryptographic techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views31 pages

Codes For Cryptography

The document provides various implementations of cryptographic algorithms in C++, including Caesar Cipher, Vernam Cipher, Playfair Cipher, Single and Double Transposition Ciphers, Substitution Ciphers, Polyalphabetic Cipher (Vigenère Cipher), and Hill Cipher. Each section includes code snippets for encryption and decryption processes, along with user input prompts for plaintext and keys. The document serves as a practical guide for understanding and implementing these cryptographic techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

IAR/14661

SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

CEASER CIPHER
Input:
#include <iostream>
#include <string>

using namespace std;

string caesarCipher(string text, int key, bool decrypt = false) {


string result = "";
for (char& character : text) {
if (isalpha(character)) {
char base = isupper(character) ? 'A' : 'a';
int shiftedChar = (character - base + (decrypt ? -key : key) + 26) % 26 + base;
result += (char)shiftedChar;
} else {
result += character;
}
}
return result;
}

int main() {
string text;
int key;
char choice;

cout << "Enter text: ";

1
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

getline(cin, text);

cout << "Enter key: ";


cin >> key;

cout << "Encrypt or decrypt? (E/D): ";


cin >> choice;

string result;
if (choice == 'E') {
result = caesarCipher(text, key);
cout << "Encrypted: " << result << endl;
} else if (choice == 'D') {
result = caesarCipher(text, key, true);
cout << "Decrypted: " << result << endl;
} else {
cout << "Invalid choice." << endl;
return 1;
}

return 0;
}

2
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

Output:

VERNAM CIPHER
Input:
#include <iostream>
#include <string>
using namespace std;

string vernamCipher(const string& plaintext, const string& key) {


string ciphertext = "";
for (size_t i = 0; i < plaintext.length(); ++i) {
char encryptedChar = plaintext[i] ^ key[i];
ciphertext += encryptedChar;
}
return ciphertext;
}

string vernamDecipher(const string& ciphertext, const string& key) {


string decryptedText = "";
for (size_t i = 0; i < ciphertext.length(); ++i) {
char decryptedChar = ciphertext[i] ^ key[i];

3
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

decryptedText += decryptedChar;
}
return decryptedText;
}

int main() {
string plaintext, key;

cout << "Enter the plaintext: ";


getline(cin, plaintext);

cout << "Enter the key (must be the same length as plaintext): ";
getline(cin, key);

if (key.length() != plaintext.length()) {
cerr << "Error: Key must be the same length as plaintext." << endl;
return 1;
}

string ciphertext = vernamCipher(plaintext, key);


cout << "Ciphertext: ";
for (char c : ciphertext) {
cout << hex << (int)c;
}
cout << endl;

string decryptedText = vernamDecipher(ciphertext, key);

4
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

cout << "Decrypted text: " << decryptedText << endl; return 0;
}

Output:

PLAY FAIR CIPHER


Input:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

void createMatrix(const string& key, char matrix[5][5]) {


string uniqueKey = "";
bool charPresent[26] = {false};

for (char c : key) {


if (isalpha(c)) {
c = toupper(c);
if (c == 'J') c = 'I';
if (!charPresent[c - 'A']) {

5
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

uniqueKey += c;
charPresent[c - 'A'] = true;
}
}
}

int index = 0;
for (char c : uniqueKey) {
matrix[index / 5][index % 5] = c;
index++;
}

for (char c = 'A'; c <= 'Z'; c++) {


if (c == 'J') continue;
if (!charPresent[c - 'A']) {
matrix[index / 5][index % 5] = c;
index++;
}
}
}

string preparePlaintext(string plaintext) {


string preparedText = "";
for (char c : plaintext) {
if (isalpha(c)) {
preparedText += toupper(c);
}

6
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

for (size_t i = 0; i < preparedText.length(); i += 2) {


if (i + 1 == preparedText.length()) {
preparedText += 'X';
} else if (preparedText[i] == preparedText[i + 1]) {
preparedText.insert(i + 1, "X");
}
}

return preparedText;
}

string encrypt(const string& plaintext, char matrix[5][5]) {


string ciphertext = "";
for (size_t i = 0; i < plaintext.length(); i += 2) {
char first = plaintext[i];
char second = plaintext[i + 1];
int row1, col1, row2, col2;

for (int r = 0; r < 5; r++) {


for (int c = 0; c < 5; c++) {
if (matrix[r][c] == first) {
row1 = r;
col1 = c;
}

7
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

if (matrix[r][c] == second) {
row2 = r;
col2 = c;
}
}
}

if (row1 == row2) {
ciphertext += matrix[row1][(col1 + 1) % 5];
ciphertext += matrix[row2][(col2 + 1) % 5];
} else if (col1 == col2) {
ciphertext += matrix[(row1 + 1) % 5][col1];
ciphertext += matrix[(row2 + 1) % 5][col2];
} else {
ciphertext += matrix[row1][col2];
ciphertext += matrix[row2][col1];
}
}
return ciphertext;
}

int main() {
string key, plaintext;

cout << "Enter the key: ";


getline(cin, key);

8
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

cout << "Enter the plaintext: ";


getline(cin, plaintext);

char matrix[5][5];
createMatrix(key, matrix);

string preparedText = preparePlaintext(plaintext);

string ciphertext = encrypt(preparedText, matrix);

cout << "Ciphertext: " << ciphertext << endl;

return 0;
}

Output:

SINGLE TRANSPOSITION CIPHERS


Input:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

9
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

using namespace std;

string encrypt(const string& plaintext, const string& key) {


int numCols = key.length();
int numRows = (plaintext.length() + numCols - 1) / numCols;
vector<string> grid(numRows, string(numCols, ' '));

for (size_t i = 0; i < plaintext.length(); ++i) {


grid[i / numCols][i % numCols] = plaintext[i];
}

vector<pair<char, int>> keyOrder;


for (int i = 0; i < numCols; ++i) {
keyOrder.push_back({key[i], i});
}

sort(keyOrder.begin(), keyOrder.end());

string ciphertext = "";


for (const auto& pair : keyOrder) {
int colIndex = pair.second;
for (int row = 0; row < numRows; ++row) {
if (grid[row][colIndex] != ' ') {
ciphertext += grid[row][colIndex];
}
}
}

10
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

return ciphertext;
}

int main() {
string plaintext, key;
cout << "Enter the plaintext: ";
getline(cin, plaintext);
cout << "Enter the key: ";
getline(cin, key);
string ciphertext = encrypt(plaintext, key);
cout << "Ciphertext: " << ciphertext << endl;
return 0;
}

Output:

DOUBLE TRANSPOSITION CIPHERS


Input:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

11
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

using namespace std;

string transpositionEncrypt(const string& plaintext, const string& key) {


int numCols = key.length();
int numRows = (plaintext.length() + numCols - 1) / numCols;
vector<string> grid(numRows, string(numCols, ' '));

for (size_t i = 0; i < plaintext.length(); ++i) {


grid[i / numCols][i % numCols] = plaintext[i];
}

vector<pair<char, int>> keyOrder;


for (int i = 0; i < numCols; ++i) {
keyOrder.push_back({key[i], i});
}

sort(keyOrder.begin(), keyOrder.end());
string ciphertext = "";
for (const auto& pair : keyOrder) {
int colIndex = pair.second;
for (int row = 0; row < numRows; ++row) {
if (grid[row][colIndex] != ' ') {
ciphertext += grid[row][colIndex];
}
}
}

12
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

return ciphertext;
}

string doubleTranspositionEncrypt(const string& plaintext, const string& key1, const string&


key2) {
string firstEncryption = transpositionEncrypt(plaintext, key1);
string secondEncryption = transpositionEncrypt(firstEncryption, key2);
return secondEncryption;
}

int main() {
string plaintext, key1, key2;
cout << "Enter the plaintext: ";
getline(cin, plaintext);
cout << "Enter the first key: ";
getline(cin, key1);
cout << "Enter the second key: ";
getline(cin, key2);
string ciphertext = doubleTranspositionEncrypt(plaintext, key1, key2);
cout << "Ciphertext: " << ciphertext << endl;

return 0;
}

13
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

Output:

SUBSTITUTION CIPHERS
Input:
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

unordered_map<char, char> createSubstitutionMap(const string& substitutionAlphabet) {


unordered_map<char, char> substitutionMap;
for (char c = 'A'; c <= 'Z'; ++c) {
substitutionMap[c] = substitutionAlphabet[c - 'A'];
}
return substitutionMap;
}

string encrypt(const string& plaintext, const unordered_map<char, char>& substitutionMap)


{
string ciphertext = "";
for (char c : plaintext) {

14
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

if (isalpha(c)) {
char upperChar = toupper(c);
ciphertext += substitutionMap.at(upperChar);
} else {
ciphertext += c;
}
}
return ciphertext;
}
int main() {
string plaintext, substitutionAlphabet;

cout << "Enter the substitution alphabet (26 unique uppercase letters): ";
getline(cin, substitutionAlphabet);

if (substitutionAlphabet.length() != 26) {
cerr << "Error: Substitution alphabet must contain exactly 26 unique letters." << endl;
return 1;
}

unordered_map<char, bool> charCheck;


for (char c : substitutionAlphabet) {
if (!isupper(c) || charCheck[c]) {
cerr << "Error: Substitution alphabet must contain unique uppercase letters." << endl;
return 1;
}
charCheck[c] = true;

15
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

cout << "Enter the plaintext: ";


getline(cin, plaintext);

unordered_map<char, char> substitutionMap =


createSubstitutionMap(substitutionAlphabet);
string ciphertext = encrypt(plaintext, substitutionMap);
cout << "Ciphertext: " << ciphertext << endl;

return 0;
}

Output:

POLYALPHABETIC CIPHER (VIGENÈRE CIPHER)


Input:
#include <iostream>
#include <string>
using namespace std;

string vigenereEncrypt(const string& plaintext, const string& keyword) {


string ciphertext = "";

16
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

int keywordIndex = 0;
int keywordLength = keyword.length();

for (char c : plaintext) {


if (isalpha(c)) {
char upperChar = toupper(c);
char keyChar = toupper(keyword[keywordIndex % keywordLength]);
char encryptedChar = 'A' + (upperChar - 'A' + keyChar - 'A') % 26;
ciphertext += encryptedChar;
keywordIndex++;
} else {
ciphertext += c;
}
}

return ciphertext;
}

int main() {
string plaintext, keyword;
cout << "Enter the plaintext: ";
getline(cin, plaintext);
cout << "Enter the keyword: ";
getline(cin, keyword);
string ciphertext = vigenereEncrypt(plaintext, keyword);
cout << "Ciphertext: " << ciphertext << endl;

17
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

return 0;
}

Output:

HILL CIPHER
Input:
#include<iostream>
#include<math.h>
using namespace std;

float en[3][1], de[3][1], a[3][3], b[3][3], msg[3][1], m[3][3];


void getKeyMatrix() {
int i, j;
char mes[3];
cout<<"Enter 3x3 matrix for KEY:\n";
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cin>>a[i][j];
m[i][j] = a[i][j];
}
cout<<"\nEnter a PLAIN TEXT of 3 letter(use A through Z): ";

18
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

cin>>mes;
for(i = 0; i < 3; i++)
msg[i][0] = mes[i] - 65;
}
void encrypt() {
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
en[i][j] = en[i][j] + a[i][k] * msg[k][j];
cout<<"\nCIPHER GENERATED is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(en[i][0], 26) + 65);
}
void inversematrix() {
int i, j, k;
float p, q;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}
for(k = 0; k < 3; k++) {
for(i = 0; i < 3; i++) {
p = m[i][k];

19
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

q = m[k][k];
for(j = 0; j < 3; j++) {
if(i != k) {
m[i][j] = m[i][j]*q - p*m[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / m[i][i];
cout<<"\n\nINVERSE MATRIX is:\n";
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
cout<<b[i][j]<<" ";
cout<<"\n";
}
}
void decrypt() {
int i, j, k;
inversematrix();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
de[i][j] = de[i][j] + b[i][k] * en[k][j];
cout<<"\nPLAIN TEXT is: ";

20
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

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


cout<<(char)(fmod(de[i][0], 26) + 65);
cout<<"\n";
}
int main() {
getKeyMatrix();
encrypt();
decrypt();
}

Output:

21
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

INVERSE OF A MATRIX
Input:
#include <iostream>
#include <vector>
#include <math.h>
#include <iomanip>
#include <stdexcept>

double getDeterminant(const std::vector<std::vector<double>> vect) {


if(vect.size() != vect[0].size()) {
throw std::runtime_error("Matrix is not quadratic");
}
int dimension = vect.size();

if(dimension == 0) {
return 1;
}

if(dimension == 1) {
return vect[0][0];
}

if(dimension == 2) {
return vect[0][0] * vect[1][1] - vect[0][1] * vect[1][0];
}

double result = 0;

22
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

int sign = 1;
for(int i = 0; i < dimension; i++) {

//Submatrix
std::vector<std::vector<double>> subVect(dimension - 1, std::vector<double>
(dimension - 1));
for(int m = 1; m < dimension; m++) {
int z = 0;
for(int n = 0; n < dimension; n++) {
if(n != i) {
subVect[m-1][z] = vect[m][n];
z++;
}
}
}

result = result + sign * vect[0][i] * getDeterminant(subVect);


sign = -sign;
}

return result;
}

std::vector<std::vector<double>> getTranspose(const std::vector<std::vector<double>>


matrix1) {
std::vector<std::vector<double>> solution(matrix1[0].size(), std::vector<double>
(matrix1.size()));
for(size_t i = 0; i < matrix1.size(); i++) {

23
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

for(size_t j = 0; j < matrix1[0].size(); j++) {


solution[j][i] = matrix1[i][j];
}
}
return solution;
}

std::vector<std::vector<double>> getCofactor(const std::vector<std::vector<double>> vect) {


if(vect.size() != vect[0].size()) {
throw std::runtime_error("Matrix is not quadratic");
}

std::vector<std::vector<double>> solution(vect.size(), std::vector<double> (vect.size()));


std::vector<std::vector<double>> subVect(vect.size() - 1, std::vector<double> (vect.size() -
1));

for(std::size_t i = 0; i < vect.size(); i++) {


for(std::size_t j = 0; j < vect[0].size(); j++) {

int p = 0;
for(size_t x = 0; x < vect.size(); x++) {
if(x == i) {
continue;
}
int q = 0;

for(size_t y = 0; y < vect.size(); y++) {


if(y == j) {

24
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

continue;
}

subVect[p][q] = vect[x][y];
q++;
}
p++;
}
solution[i][j] = pow(-1, i + j) * getDeterminant(subVect);
}
}
return solution;
}

std::vector<std::vector<double>> getInverse(const std::vector<std::vector<double>> vect) {


if(getDeterminant(vect) == 0) {
throw std::runtime_error("Determinant is 0");
}
double d = 1.0/getDeterminant(vect);
std::vector<std::vector<double>> solution(vect.size(), std::vector<double> (vect.size()));

for(size_t i = 0; i < vect.size(); i++) {


for(size_t j = 0; j < vect.size(); j++) {
solution[i][j] = vect[i][j] * d;
}
}

25
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

return getTranspose(getCofactor(solution));
}

void printMatrix(const std::vector<std::vector<double>> vect) {


for(std::size_t i = 0; i < vect.size(); i++) {
for(std::size_t j = 0; j < vect[0].size(); j++) {
std::cout << std::setw(8) << vect[i][j] << " ";
}
std::cout << "\n";
}
}

int main() {
std::vector<std::vector<double>> matrix(3, std::vector<double> (3));
matrix = {
{0,24,0},
{0,0,26},
{24,0,0}
};
printMatrix(getInverse(matrix));
return 0; }

Output:

26
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

RAIL FENCE CIPHER


Input:
#include<bits/stdc++.h>
using namespace std;
int main(){
int t,n,m,i,j,k,sum=0;
string s;
cout<<"Enter the message"<<'\n';
cin>>s;
cout<<"Enter key"<<'\n';
cin>>n;
vector<vector<char>> a(n,vector<char>(s.size(),' '));
j=0;
int flag=0;
for(i=0;i<s.size();i++){
a[j][i] = s[i];
if(j==n-1){
flag=1;
}
else if(j==0)
flag=0;
if(flag==0){
j++;
}
else j--;
}
for(i=0;i<n;i++){

27
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

for(j=0;j<s.size();j++){
if(a[i][j]!=' ')
cout<<a[i][j];
}
}
cout<<'\n';
return 0;
}

Output:

MATRIX MULTIPLICATION
Input:
#include <iostream>
using namespace std;

int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;

cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;

28
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;

while (c1!=r2)
{
cout << "Error! column of first matrix not equal to row of second.";

cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;

cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}

cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < r2; ++i)
for(j = 0; j < c2; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";

29
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

cin >> b[i][j];


}

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


for(j = 0; j < c2; ++j)
{
mult[i][j]=0;
}

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


for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{
mult[i][j] += a[i][k] * b[k][j];
}

cout << endl << "Output Matrix: " << endl;


for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
cout << " " << mult[i][j];
if(j == c2-1)
cout << endl;
}
}

Output:

30
IAR/14661
SEMESTER : 4TH
DIVISION : C
CRYPTOGRAPHY

31

You might also like