Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
gsiddhad committed Feb 20, 2021
1 parent 0b807e6 commit 3206be8
Show file tree
Hide file tree
Showing 28 changed files with 524 additions and 310 deletions.
18 changes: 12 additions & 6 deletions Additive Cipher/additive.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@
#define MAX 51
using namespace std;

class Additive {
class Additive
{
char *cipher, *decipher;

public:
Additive() {
Additive()
{
cipher = new char[MAX];
decipher = new char[MAX];
}

char* Encrypt(char* plain, int key) {
char *Encrypt(char *plain, int key)
{
int i = 0;
for (i = 0; plain[i] != '\0'; i++) {
for (i = 0; plain[i] != '\0'; i++)
{
if (isupper(plain[i]))
cipher[i] = (((plain[i] - 65) + key) % 26) + 65;
else if (islower(plain[i]))
Expand All @@ -43,10 +47,12 @@ class Additive {
return cipher;
}

char* Decrypt(char* plain, int key) {
char *Decrypt(char *plain, int key)
{
key = 26 - key;
int i = 0;
for (i = 0; plain[i] != '\0'; i++) {
for (i = 0; plain[i] != '\0'; i++)
{
if (isupper(plain[i]))
decipher[i] = (((plain[i] - 65) + key) % 26) + 65;
else if (islower(plain[i]))
Expand Down
6 changes: 4 additions & 2 deletions Additive Cipher/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

using namespace std;

int main() {
int main()
{

setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
Expand All @@ -24,7 +25,8 @@ int main() {
cout << " Enter Shift Number : ";
cin >> key;

while (key < 0) {
while (key < 0)
{
key += 26;
}

Expand Down
25 changes: 16 additions & 9 deletions Affine Cipher/affine.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ using namespace std;

#define MAX 51

class Affine {
class Affine
{
char *cipher, *decipher;

public:
Affine() {
Affine()
{
cipher = new char[MAX];
decipher = new char[MAX];
}

char* Encrypt(char* plain, int a, int b) {
char *Encrypt(char *plain, int a, int b)
{
int i = 0;
for (i = 0; plain[i] != '\0'; i++) {
for (i = 0; plain[i] != '\0'; i++)
{
if (isupper(plain[i]))
cipher[i] = ((((plain[i] - 65) * a) + b) % 26) + 65;
else if (islower(plain[i]))
Expand All @@ -44,12 +48,14 @@ class Affine {
return cipher;
}

char* Decrypt(char* plain, int a, int b) {
char *Decrypt(char *plain, int a, int b)
{
int i = 0;

int inv = Inverse(a);

for (i = 0; plain[i] != '\0'; i++) {
for (i = 0; plain[i] != '\0'; i++)
{
if (isupper(plain[i]))
decipher[i] = ((inv * ((plain[i] - 65) + 104 - b)) % 26) + 65;
else if (islower(plain[i]))
Expand All @@ -66,14 +72,15 @@ class Affine {
}

private:
int Inverse(int number) {
for (int i = 1; i < 26; i++) {
int Inverse(int number)
{
for (int i = 1; i < 26; i++)
{
if ((i * number) % 26 == 1)
return i;
}
return 1;
}

};

#endif /* AFFINE_H_ */
3 changes: 2 additions & 1 deletion Affine Cipher/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

using namespace std;

int main() {
int main()
{

setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
Expand Down
68 changes: 44 additions & 24 deletions Double Transposition Cipher/doubletrans.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,35 @@
#define MAX 101
using namespace std;

class Transposition {
class Transposition
{
char *cipher, *decipher;
int counter;

public:
Transposition() {
Transposition()
{
cipher = new char[MAX];
decipher = new char[MAX];
counter = 0;
}

char* Encrypt(char* plain, char* key) {
char *Encrypt(char *plain, char *key)
{
counter = strlen(plain);
float len = (float) strlen(plain) / strlen(key);
float len = (float)strlen(plain) / strlen(key);
unsigned int length = len;
if (len > length)
length++;

char **mat = new char*[length];
char **mat = new char *[length];
for (unsigned int i = 0; i < length; i++)
mat[i] = new char[strlen(key)];

unsigned int count = 0;
for (unsigned int i = 0; i < length; i++)
for (unsigned int j = 0; j < strlen(key); j++) {
for (unsigned int j = 0; j < strlen(key); j++)
{
if (count < strlen(plain))
mat[i][j] = plain[count];
else
Expand All @@ -56,29 +60,34 @@ class Transposition {
cout << "--";
cout << endl;

for (unsigned int i = 0; i < length; i++) {
for (unsigned int j = 0; j < strlen(key); j++) {
for (unsigned int i = 0; i < length; i++)
{
for (unsigned int j = 0; j < strlen(key); j++)
{
cout << " " << mat[i][j];
}
cout << endl;
}

int *num = new int[strlen(key)];
for (unsigned int i = 0; i < strlen(key); i++) {
for (unsigned int i = 0; i < strlen(key); i++)
{
if (isupper(key[i]))
num[i] = key[i] - 65;
else if (islower(key[i]))
num[i] = key[i] - 97;
}

count = 0;
for (unsigned int i = 0; i < strlen(key); i++) {
for (unsigned int i = 0; i < strlen(key); i++)
{
int min = 0;
for (unsigned int j = 1; j < strlen(key); j++)
if (num[j] < num[min])
min = j;

for (unsigned int k = 0; k < length; k++) {
for (unsigned int k = 0; k < length; k++)
{
cipher[count] = mat[k][min];
count++;
}
Expand All @@ -90,36 +99,41 @@ class Transposition {
else
cipher[MAX - 1] = '\0';

cout << endl << " Cipher Text : " << cipher;
cout << endl
<< " Cipher Text : " << cipher;
return cipher;
}

char* Decrypt(char* plain, char *key) {
float len = (float) strlen(plain) / strlen(key);
char *Decrypt(char *plain, char *key)
{
float len = (float)strlen(plain) / strlen(key);
unsigned int length = len;
if (len > length)
length++;

char **mat = new char*[length];
char **mat = new char *[length];
for (unsigned int i = 0; i < length; i++)
mat[i] = new char[strlen(key)];

int *num = new int[strlen(key)];
for (unsigned int i = 0; i < strlen(key); i++) {
for (unsigned int i = 0; i < strlen(key); i++)
{
if (isupper(key[i]))
num[i] = key[i] - 65;
else if (islower(key[i]))
num[i] = key[i] - 97;
}

unsigned int count = 0;
for (unsigned int i = 0; i < strlen(key); i++) {
for (unsigned int i = 0; i < strlen(key); i++)
{
int min = 0;
for (unsigned int j = 1; j < strlen(key); j++)
if (num[j] < num[min])
min = j;

for (unsigned int k = 0; k < length; k++) {
for (unsigned int k = 0; k < length; k++)
{
mat[k][min] = plain[count];
count++;
}
Expand All @@ -128,10 +142,12 @@ class Transposition {

count = 0;
for (unsigned int i = 0; i < length; i++)
for (unsigned int j = 0; j < strlen(key); j++) {
for (unsigned int j = 0; j < strlen(key); j++)
{
if (count < strlen(plain))
decipher[count] = mat[i][j];
else if (count > strlen(plain)) {
else if (count > strlen(plain))
{
decipher[count] = '\0';
break;
}
Expand All @@ -144,13 +160,17 @@ class Transposition {
decipher[MAX - 1] = '\0';

decipher[counter] = '\0';
cout << endl << " Decipher Text : " << decipher;
cout << endl
<< " Decipher Text : " << decipher;
return decipher;
}

void printMatrix(char **mat, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
void printMatrix(char **mat, int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << " " << mat[n][m];
}
cout << endl;
Expand Down
3 changes: 2 additions & 1 deletion Double Transposition Cipher/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include "doubletrans.h"
using namespace std;

int main() {
int main()
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);

Expand Down
Loading

0 comments on commit 3206be8

Please sign in to comment.