diff --git a/Additive Cipher/additive.h b/Additive Cipher/additive.h index 2d91501..a0d5450 100644 --- a/Additive Cipher/additive.h +++ b/Additive Cipher/additive.h @@ -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])) @@ -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])) diff --git a/Additive Cipher/main.cpp b/Additive Cipher/main.cpp index 3a1e708..5078fc2 100644 --- a/Additive Cipher/main.cpp +++ b/Additive Cipher/main.cpp @@ -10,7 +10,8 @@ using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); @@ -24,7 +25,8 @@ int main() { cout << " Enter Shift Number : "; cin >> key; - while (key < 0) { + while (key < 0) + { key += 26; } diff --git a/Affine Cipher/affine.h b/Affine Cipher/affine.h index bce16a9..b82f8e8 100644 --- a/Affine Cipher/affine.h +++ b/Affine Cipher/affine.h @@ -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])) @@ -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])) @@ -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_ */ diff --git a/Affine Cipher/main.cpp b/Affine Cipher/main.cpp index 629b3e1..44e932a 100644 --- a/Affine Cipher/main.cpp +++ b/Affine Cipher/main.cpp @@ -9,7 +9,8 @@ using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); diff --git a/Double Transposition Cipher/doubletrans.h b/Double Transposition Cipher/doubletrans.h index 1e7a81b..93e7b2c 100644 --- a/Double Transposition Cipher/doubletrans.h +++ b/Double Transposition Cipher/doubletrans.h @@ -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 @@ -56,15 +60,18 @@ 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])) @@ -72,13 +79,15 @@ class Transposition { } 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++; } @@ -90,22 +99,25 @@ 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])) @@ -113,13 +125,15 @@ class Transposition { } 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++; } @@ -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; } @@ -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; diff --git a/Double Transposition Cipher/main.cpp b/Double Transposition Cipher/main.cpp index 1045429..b2eb530 100644 --- a/Double Transposition Cipher/main.cpp +++ b/Double Transposition Cipher/main.cpp @@ -8,7 +8,8 @@ #include "doubletrans.h" using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); diff --git a/Enigma Machine/enigma.h b/Enigma Machine/enigma.h index 15c0ac3..fd6ae89 100644 --- a/Enigma Machine/enigma.h +++ b/Enigma Machine/enigma.h @@ -16,11 +16,14 @@ #define MAX 101 using namespace std; -class Enigma { +class Enigma +{ char *cipher, *decipher; int count[3]; + public: - Enigma() { + Enigma() + { cipher = new char[MAX]; decipher = new char[MAX]; count[0] = 0; @@ -28,49 +31,60 @@ class Enigma { count[2] = 0; } - char* Encrypt(char* plain, char key[3][27], int n[3]) { + char *Encrypt(char *plain, char key[3][27], int n[3]) + { int i = 0; bool flag = false; for (i = 0; i < n[0]; i++) - RightShift((char*) key[0]); + RightShift((char *)key[0]); for (i = 0; i < n[1]; i++) - RightShift((char*) key[1]); + RightShift((char *)key[1]); for (i = 0; i < n[2]; i++) - RightShift((char*) key[2]); + RightShift((char *)key[2]); char temp = 0; - for (i = 0; plain[i] != '\0'; i++) { - if (isupper(plain[i])) { - temp = (char) (key[0][plain[i] - 65]); - temp = (char) (key[1][temp - 65]); - cipher[i] = (char) (key[2][temp - 65]); + for (i = 0; plain[i] != '\0'; i++) + { + if (isupper(plain[i])) + { + temp = (char)(key[0][plain[i] - 65]); + temp = (char)(key[1][temp - 65]); + cipher[i] = (char)(key[2][temp - 65]); flag = true; - } else if (islower(plain[i])) { - temp = (char) (key[0][plain[i] - 97] - 65 + 97); - temp = (char) (key[1][temp - 97] - 65 + 97); - cipher[i] = (char) (key[2][temp - 97] - 65 + 97); + } + else if (islower(plain[i])) + { + temp = (char)(key[0][plain[i] - 97] - 65 + 97); + temp = (char)(key[1][temp - 97] - 65 + 97); + cipher[i] = (char)(key[2][temp - 97] - 65 + 97); flag = true; - } else { + } + else + { cipher[i] = plain[i]; flag = false; } - if(flag) { - RightShift((char*) key[2]); + if (flag) + { + RightShift((char *)key[2]); n[2]++; - if (n[2] > 25) { + if (n[2] > 25) + { n[2] = n[2] % 26; - RightShift((char*) key[1]); + RightShift((char *)key[1]); n[1]++; - if (n[1] > 25) { + if (n[1] > 25) + { n[1] = n[1] % 26; - RightShift((char*) key[0]); + RightShift((char *)key[0]); n[0]++; - if (n[0] > 25) { + if (n[0] > 25) + { n[0] = n[0] % 26; } } @@ -79,11 +93,12 @@ class Enigma { cout << n[0] << " " << n[1] << " " << n[2] << endl; for (int i = 0; i < 26; i++) - cout << (char) (i + 65); + cout << (char)(i + 65); cout << endl; cout << key[0] << endl; cout << key[1] << endl; - cout << key[2] << endl << endl; + cout << key[2] << endl + << endl; } if (i < MAX) @@ -93,18 +108,20 @@ class Enigma { return cipher; } - char* Decrypt(char* plain, char key[3][27], int n[3]) { + char *Decrypt(char *plain, char key[3][27], int n[3]) + { int i = 0, pos = 0; bool flag = false; for (i = 0; i < n[0]; i++) - RightShift((char*) key[0]); + RightShift((char *)key[0]); for (i = 0; i < n[1]; i++) - RightShift((char*) key[1]); + RightShift((char *)key[1]); for (i = 0; i < n[2]; i++) - RightShift((char*) key[2]); + RightShift((char *)key[2]); - for (i = 0; plain[i] != '\0'; i++) { + for (i = 0; plain[i] != '\0'; i++) + { int tempos = 0; pos = 0; @@ -112,38 +129,47 @@ class Enigma { if (toupper(plain[i]) == key[2][j]) pos = j; for (int j = 0; j < 26; j++) - if ((char) (pos + 65) == key[1][j]) + if ((char)(pos + 65) == key[1][j]) tempos = j; for (int j = 0; j < 26; j++) - if ((char) (tempos + 65) == key[0][j]) + if ((char)(tempos + 65) == key[0][j]) pos = j; - if (isupper(plain[i])) { + if (isupper(plain[i])) + { decipher[i] = char(pos) + 65; flag = true; - } else if (islower(plain[i])) { + } + else if (islower(plain[i])) + { decipher[i] = char(pos) + 97; flag = true; - } else { + } + else + { decipher[i] = plain[i]; flag = false; } - if (flag) { - RightShift((char*) key[2]); + if (flag) + { + RightShift((char *)key[2]); n[2]++; - if (n[2] > 25) { + if (n[2] > 25) + { n[2] = n[2] % 26; - RightShift((char*) key[1]); + RightShift((char *)key[1]); n[1]++; - if (n[1] > 25) { + if (n[1] > 25) + { n[1] = n[1] % 26; - RightShift((char*) key[0]); + RightShift((char *)key[0]); n[0]++; - if (n[0] > 25) { + if (n[0] > 25) + { n[0] = n[0] % 26; } } @@ -159,7 +185,8 @@ class Enigma { } private: - void RightShift(char* key) { + void RightShift(char *key) + { int temp = key[25]; for (int i = 25; i > 0; i--) key[i] = key[i - 1]; diff --git a/Enigma Machine/main.cpp b/Enigma Machine/main.cpp index f2945e1..127c17c 100644 --- a/Enigma Machine/main.cpp +++ b/Enigma Machine/main.cpp @@ -8,7 +8,8 @@ #include "enigma.h" using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); @@ -17,8 +18,8 @@ int main() { char *plain, *cipher, *decipher; plain = new char[MAX]; - char key[3][27] = { "NOATRBECFUXDQGYLKHVIJMPZSW", - "BDFHJLCPRTXVZNYEIWGAKMUSQO", "EKMFLGDQVZNTOWYHXUSPAIBRCJ" }; + char key[3][27] = {"NOATRBECFUXDQGYLKHVIJMPZSW", + "BDFHJLCPRTXVZNYEIWGAKMUSQO", "EKMFLGDQVZNTOWYHXUSPAIBRCJ"}; char oriKey[3][27]; strcpy(oriKey[0], key[0]); @@ -29,7 +30,8 @@ int main() { gets(plain); cout << " Enter Initial Positions of Enigma : "; - for (int i = 0; i < 3; i++) { + for (int i = 0; i < 3; i++) + { cin >> n[i]; orin[i] = n[i]; } diff --git a/Hill Cipher/hillcipher.h b/Hill Cipher/hillcipher.h index 726ed5f..d5123aa 100644 --- a/Hill Cipher/hillcipher.h +++ b/Hill Cipher/hillcipher.h @@ -16,17 +16,21 @@ #define MAX 51 using namespace std; -class Hillcipher { +class Hillcipher +{ char *cipher, *decipher; int size; + public: - Hillcipher() { + Hillcipher() + { cipher = new char[MAX]; decipher = new char[MAX]; size = 0; } - char* Encrypt(char* plain, int key, int **kmatrix) { + char *Encrypt(char *plain, int key, int **kmatrix) + { size = key; int svector[key]; @@ -34,20 +38,25 @@ class Hillcipher { unsigned int fcounter = 0; bool flag = true; - while (fcounter < strlen(plain)) { - for (int i = 0; i < key; i++) { + while (fcounter < strlen(plain)) + { + for (int i = 0; i < key; i++) + { flag = true; if (plain[fcounter + i] == '\0') flag = false; - if (flag) { + if (flag) + { if (isupper(plain[fcounter + i])) svector[i] = plain[fcounter + i] - 65; else if (islower(plain[fcounter + i])) svector[i] = plain[fcounter + i] - 97; else svector[i] = plain[fcounter + i]; - } else { + } + else + { svector[i] = 23; } } @@ -55,7 +64,8 @@ class Hillcipher { int *res; res = Matrixmultiply(kmatrix, svector); - for (int i = 0; i < size; i++) { + for (int i = 0; i < size; i++) + { while (res[i] < 0) res[i] += 26; final[fcounter] = res[i] % 26; @@ -74,20 +84,26 @@ class Hillcipher { return cipher; } - char* Decrypt(char* plain, int key, int **kmatrix) { + char *Decrypt(char *plain, int key, int **kmatrix) + { int det = Matrixdeterminant(kmatrix); - if (gcd(det, 26) != 1) { + if (gcd(det, 26) != 1) + { cout << " Choose a different Matrix" << endl; -// exit(0); + // exit(0); } int inv = Inverse(det); - cout << endl << " Inv(Det) : " << inv; - int** invmatrix = Matrixadjoint(inv, kmatrix); + cout << endl + << " Inv(Det) : " << inv; + int **invmatrix = Matrixadjoint(inv, kmatrix); - cout << endl << " Inverse Matrix : " << endl; - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { + cout << endl + << " Inverse Matrix : " << endl; + for (int i = 0; i < size; i++) + { + for (int j = 0; j < size; j++) + { invmatrix[i][j] = (invmatrix[i][j] * inv) % 26; if (invmatrix[i][j] < 0) invmatrix[i][j] += 26; @@ -102,18 +118,23 @@ class Hillcipher { unsigned int fcounter = 0; bool flag = true; - while (fcounter < strlen(plain)) { - for (int i = 0; i < key; i++) { + while (fcounter < strlen(plain)) + { + for (int i = 0; i < key; i++) + { flag = true; if (plain[fcounter + i] == '\0') flag = false; - if (flag) { + if (flag) + { if (isupper(plain[fcounter + i])) svector[i] = plain[fcounter + i] - 65; else svector[i] = plain[fcounter + i]; - } else { + } + else + { svector[i] = 23; } } @@ -121,7 +142,8 @@ class Hillcipher { int *res; res = Matrixmultiply(invmatrix, svector); - for (int i = 0; i < size; i++) { + for (int i = 0; i < size; i++) + { while (res[i] < 0) res[i] += 26; final[fcounter] = res[i] % 26; @@ -141,7 +163,8 @@ class Hillcipher { } private: - int* Matrixmultiply(int **kmatrix, int svector[]) { + int *Matrixmultiply(int **kmatrix, int svector[]) + { int *result; result = new int[size]; @@ -149,27 +172,24 @@ class Hillcipher { result[i] = 0; for (int j = 0; j < size; j++) - for (int k = 0; k < size; k++) + for (int k = 0; k < size; k++) result[j] = result[j] + (kmatrix[j][k] * svector[k]); return result; } - int Matrixdeterminant(int **kmatrix) { + int Matrixdeterminant(int **kmatrix) + { int result = 0; - if (size == 2) { - result = kmatrix[0][0] * kmatrix[1][1] - - kmatrix[0][1] * kmatrix[1][0]; - } else if (size == 3) { - result += kmatrix[0][0] - * (kmatrix[1][1] * kmatrix[2][2] - - kmatrix[1][2] * kmatrix[2][1]); - result -= kmatrix[0][1] - * (kmatrix[1][0] * kmatrix[2][2] - - kmatrix[1][2] * kmatrix[2][0]); - result += kmatrix[0][2] - * (kmatrix[1][0] * kmatrix[2][1] - - kmatrix[1][1] * kmatrix[2][0]); + if (size == 2) + { + result = kmatrix[0][0] * kmatrix[1][1] - kmatrix[0][1] * kmatrix[1][0]; + } + else if (size == 3) + { + result += kmatrix[0][0] * (kmatrix[1][1] * kmatrix[2][2] - kmatrix[1][2] * kmatrix[2][1]); + result -= kmatrix[0][1] * (kmatrix[1][0] * kmatrix[2][2] - kmatrix[1][2] * kmatrix[2][0]); + result += kmatrix[0][2] * (kmatrix[1][0] * kmatrix[2][1] - kmatrix[1][1] * kmatrix[2][0]); } result %= 26; @@ -179,26 +199,32 @@ class Hillcipher { return result; } - int Inverse(int number) { + int Inverse(int number) + { - for (int i = 1; i < 26; i++) { + for (int i = 1; i < 26; i++) + { if ((i * number) % 26 == 1) return i; } return 1; } - int** Matrixadjoint(int inv, int **mat) { - int **res = new int*[size]; + int **Matrixadjoint(int inv, int **mat) + { + int **res = new int *[size]; for (int i = 0; i < size; i++) res[i] = new int[size]; - if (size == 2) { + if (size == 2) + { res[0][0] = mat[1][1]; res[0][1] = -mat[0][1]; res[1][0] = -mat[1][0]; res[1][1] = mat[0][0]; - } else if (size == 3) { + } + else if (size == 3) + { res[0][0] = mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]; res[0][1] = -(mat[0][1] * mat[2][2] - mat[2][1] * mat[0][2]); res[0][2] = mat[0][1] * mat[1][2] - mat[0][2] * mat[1][1]; @@ -211,7 +237,8 @@ class Hillcipher { } for (int i = 0; i < size; i++) - for (int j = 0; j < size; j++) { + for (int j = 0; j < size; j++) + { while (res[i][j] < 0) res[i][j] += 26; res[i][j] = res[i][j] % 26; @@ -220,7 +247,8 @@ class Hillcipher { return res; } - int gcd(int a, int b) { + int gcd(int a, int b) + { return b == 0 ? a : gcd(b, a % b); } }; diff --git a/Hill Cipher/main.cpp b/Hill Cipher/main.cpp index 191f68f..8bf6007 100644 --- a/Hill Cipher/main.cpp +++ b/Hill Cipher/main.cpp @@ -8,7 +8,8 @@ using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); @@ -21,7 +22,7 @@ int main() { gets(plain); cout << " Enter Key Matrix Size (Greater than 0) : "; cin >> key; - int **kmatrix = new int*[key]; + int **kmatrix = new int *[key]; for (int i = 0; i < key; i++) kmatrix[i] = new int[key]; @@ -30,8 +31,10 @@ int main() { for (int j = 0; j < key; j++) cin >> kmatrix[i][j]; - for (int i = 0; i < key; i++) { - for (int j = 0; j < key; j++) { + for (int i = 0; i < key; i++) + { + for (int j = 0; j < key; j++) + { cout << " " << kmatrix[i][j]; } cout << endl; @@ -39,8 +42,10 @@ int main() { char *newtext = new char[MAX]; int j = 0; - for (unsigned int i = 0; i <= strlen(plain); i++) { - if (isupper(plain[i]) || islower(plain[i])) { + for (unsigned int i = 0; i <= strlen(plain); i++) + { + if (isupper(plain[i]) || islower(plain[i])) + { newtext[j] = plain[i]; j++; } diff --git a/Monoalphabetic Substitution Cipher/main.cpp b/Monoalphabetic Substitution Cipher/main.cpp index 26d034a..6a16769 100644 --- a/Monoalphabetic Substitution Cipher/main.cpp +++ b/Monoalphabetic Substitution Cipher/main.cpp @@ -8,7 +8,8 @@ #include "monoalpha.h" using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); diff --git a/Monoalphabetic Substitution Cipher/monoalpha.h b/Monoalphabetic Substitution Cipher/monoalpha.h index ae2a04c..de3df9e 100644 --- a/Monoalphabetic Substitution Cipher/monoalpha.h +++ b/Monoalphabetic Substitution Cipher/monoalpha.h @@ -16,19 +16,23 @@ #define MAX 51 using namespace std; -class Monoalpha { +class Monoalpha +{ char *cipher, *decipher; public: - Monoalpha() { + Monoalpha() + { cipher = new char[MAX]; decipher = new char[MAX]; } - char* Encrypt(char* plain, char* key) { + char *Encrypt(char *plain, char *key) + { int i = 0; - for (i = 0; plain[i] != '\0'; i++) { + for (i = 0; plain[i] != '\0'; i++) + { if (isupper(plain[i])) cipher[i] = key[plain[i] - 65]; else if (islower(plain[i])) @@ -44,9 +48,11 @@ class Monoalpha { return cipher; } - char* Decrypt(char* plain, char* key) { + char *Decrypt(char *plain, char *key) + { int i = 0, pos = 0; - for (i = 0; plain[i] != '\0'; i++) { + for (i = 0; plain[i] != '\0'; i++) + { for (int j = 0; j < 26; j++) if (toupper(plain[i]) == key[j]) diff --git a/Multiplicative Cipher/main.cpp b/Multiplicative Cipher/main.cpp index 5cb5ae2..c394342 100644 --- a/Multiplicative Cipher/main.cpp +++ b/Multiplicative Cipher/main.cpp @@ -9,7 +9,8 @@ using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); diff --git a/Multiplicative Cipher/multiplicative.h b/Multiplicative Cipher/multiplicative.h index 20b8bbf..f53be20 100644 --- a/Multiplicative Cipher/multiplicative.h +++ b/Multiplicative Cipher/multiplicative.h @@ -17,18 +17,22 @@ using namespace std; #define MAX 51 -class Multiplicative { +class Multiplicative +{ char *cipher, *decipher; public: - Multiplicative() { + Multiplicative() + { cipher = new char[MAX]; decipher = new char[MAX]; } - char* Encrypt(char* plain, int a) { + char *Encrypt(char *plain, int a) + { 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) % 26) + 65; else if (islower(plain[i])) @@ -44,11 +48,13 @@ class Multiplicative { return cipher; } - char* Decrypt(char* plain, int a) { + char *Decrypt(char *plain, int a) + { 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) % 26) + 65; else if (islower(plain[i])) @@ -65,14 +71,15 @@ class Multiplicative { } 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 /* MULTIPLICATIVE_H_ */ diff --git a/OneTimePad Cipher/main.cpp b/OneTimePad Cipher/main.cpp index fe100fb..0261fae 100644 --- a/OneTimePad Cipher/main.cpp +++ b/OneTimePad Cipher/main.cpp @@ -8,12 +8,13 @@ #include "onetimepad.h" using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); - srand((unsigned) time(0)); + srand((unsigned)time(0)); char *plain, *key, *cipher, *decipher, *newkey; plain = new char[MAX]; @@ -28,7 +29,8 @@ int main() { unsigned int i = 0; bool flag = false; - for (i = 0; i < strlen(plain); i++) { + for (i = 0; i < strlen(plain); i++) + { flag = rand() % 2; if (flag) key[i] = rand() % 26 + 65; @@ -50,5 +52,3 @@ int main() { cout << "\n Decipher Text : " << decipher; return 0; } - - diff --git a/OneTimePad Cipher/onetimepad.h b/OneTimePad Cipher/onetimepad.h index b2c31a9..b68539d 100644 --- a/OneTimePad Cipher/onetimepad.h +++ b/OneTimePad Cipher/onetimepad.h @@ -18,22 +18,26 @@ #define MAX 51 using namespace std; -class Onetimepad { +class Onetimepad +{ char *cipher, *decipher, *newkey; public: - Onetimepad() { + Onetimepad() + { cipher = new char[MAX]; decipher = new char[MAX]; newkey = new char[MAX]; } - char* Encrypt(char* plain, char* key) { + char *Encrypt(char *plain, char *key) + { int i = 0; newkey = GenNewKey(key, plain); - for (i = 0; plain[i] != '\0'; i++) { + for (i = 0; plain[i] != '\0'; i++) + { if (isupper(plain[i])) cipher[i] = (((plain[i] - 65) + (newkey[i] - 65)) % 26) + 65; else if (islower(plain[i])) @@ -49,17 +53,17 @@ class Onetimepad { return cipher; } - char* Decrypt(char* plain, char* key) { + char *Decrypt(char *plain, char *key) + { int i = 0; newkey = GenNewKey(key, plain); - for (i = 0; plain[i] != '\0'; i++) { + for (i = 0; plain[i] != '\0'; i++) + { if (isupper(plain[i])) - decipher[i] = (((plain[i] - 65) + 26 - (newkey[i] - 65)) % 26) - + 65; + decipher[i] = (((plain[i] - 65) + 26 - (newkey[i] - 65)) % 26) + 65; else if (islower(plain[i])) - decipher[i] = (((plain[i] - 97) + 26 - (newkey[i] - 65)) % 26) - + 97; + decipher[i] = (((plain[i] - 97) + 26 - (newkey[i] - 65)) % 26) + 97; else decipher[i] = plain[i]; } @@ -71,9 +75,11 @@ class Onetimepad { return decipher; } - char* GenNewKey(char* key, char* plain) { + char *GenNewKey(char *key, char *plain) + { int i = 0, j = 0; - for (i = 0; plain[i] != '\0'; i++, j++) { + for (i = 0; plain[i] != '\0'; i++, j++) + { if (key[j] == '\0') j = 0; newkey[i] = toupper(key[j]); @@ -82,10 +88,10 @@ class Onetimepad { return newkey; } - char* Getkey() { + char *Getkey() + { return newkey; } - }; #endif /* ONETIMEPAD_H_ */ diff --git a/Play Cipher/main.cpp b/Play Cipher/main.cpp index 0a3a1c5..ff95dc4 100644 --- a/Play Cipher/main.cpp +++ b/Play Cipher/main.cpp @@ -8,7 +8,8 @@ #include "play.h" using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); @@ -26,16 +27,20 @@ int main() { bool flag = true; int length = strlen(key); - for (int i = 0; i < 26; i++) { + for (int i = 0; i < 26; i++) + { char ch = i + 97; - for (int j = 0; j < length; j++) { - if (key[j] == ch || ch == 'j') { + for (int j = 0; j < length; j++) + { + if (key[j] == ch || ch == 'j') + { flag = true; break; } flag = false; } - if (!flag) { + if (!flag) + { key[length] = ch; length++; key[length] = '\0'; @@ -48,8 +53,10 @@ int main() { for (int j = 0; j < 5; j++) keymat[i][j] = key[count++]; - for (int i = 0; i < 5; i++) { - for (int j = 0; j < 5; j++) { + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 5; j++) + { cout << " " << keymat[i][j]; } cout << endl; diff --git a/Play Cipher/play.h b/Play Cipher/play.h index 7256688..5ef47c9 100644 --- a/Play Cipher/play.h +++ b/Play Cipher/play.h @@ -16,21 +16,25 @@ #define MAX 51 using namespace std; -class Play { +class Play +{ char *cipher, *decipher; int mat[26][2]; public: - Play() { + Play() + { cipher = new char[MAX]; decipher = new char[MAX]; } - char* Encrypt(char* plain, char keymat[5][5]) { + char *Encrypt(char *plain, char keymat[5][5]) + { unsigned int i = 0; int length = 0; char *newtext = new char[MAX]; - for (i = 0; i < strlen(plain); i++) { + for (i = 0; i < strlen(plain); i++) + { if (islower(plain[i])) newtext[length++] = plain[i]; if (isupper(plain[i])) @@ -38,21 +42,26 @@ class Play { } newtext[length] = '\0'; - for (i = 1; i < strlen(newtext); i++) { - if (newtext[i - 1] == newtext[i]) { + for (i = 1; i < strlen(newtext); i++) + { + if (newtext[i - 1] == newtext[i]) + { newtext[i] = 'x'; } } length = strlen(newtext); - if (length % 2 == 1) { + if (length % 2 == 1) + { newtext[length++] = 'x'; newtext[length] = '\0'; } strcpy(plain, newtext); - for (i = 0; i < 5; i++) { - for (int j = 0; j < 5; j++) { + for (i = 0; i < 5; i++) + { + for (int j = 0; j < 5; j++) + { mat[keymat[i][j] - 97][0] = i; mat[keymat[i][j] - 97][1] = j; } @@ -61,25 +70,29 @@ class Play { mat[9][1] = mat[8][1]; int ch[2]; - for (i = 0; i < strlen(plain); i += 2) { + for (i = 0; i < strlen(plain); i += 2) + { ch[0] = plain[i] - 97; ch[1] = plain[i + 1] - 97; // Test 1 (Not Row or Column Match) - if (mat[ch[0]][0] != mat[ch[1]][0] - && mat[ch[0]][1] != mat[ch[1]][1]) { - cipher[i] = (char) keymat[mat[ch[0]][0]][mat[ch[1]][1]]; - cipher[i + 1] = (char) keymat[mat[ch[1]][0]][mat[ch[0]][1]]; - } else if (mat[ch[0]][0] == mat[ch[1]][0]) { // Test 2 (Same Row) + if (mat[ch[0]][0] != mat[ch[1]][0] && mat[ch[0]][1] != mat[ch[1]][1]) + { + cipher[i] = (char)keymat[mat[ch[0]][0]][mat[ch[1]][1]]; + cipher[i + 1] = (char)keymat[mat[ch[1]][0]][mat[ch[0]][1]]; + } + else if (mat[ch[0]][0] == mat[ch[1]][0]) + { // Test 2 (Same Row) cipher[i] = - (char) keymat[mat[ch[0]][0]][(mat[ch[0]][1] + 1) % 5]; - cipher[i + 1] = (char) keymat[mat[ch[1]][0]][(mat[ch[1]][1] + 1) - % 5]; - } else if (mat[ch[0]][1] == mat[ch[1]][1]) { // Test 3 (Same Column) + (char)keymat[mat[ch[0]][0]][(mat[ch[0]][1] + 1) % 5]; + cipher[i + 1] = (char)keymat[mat[ch[1]][0]][(mat[ch[1]][1] + 1) % 5]; + } + else if (mat[ch[0]][1] == mat[ch[1]][1]) + { // Test 3 (Same Column) cipher[i] = - (char) keymat[(mat[ch[0]][0] + 1) % 5][mat[ch[0]][1]]; + (char)keymat[(mat[ch[0]][0] + 1) % 5][mat[ch[0]][1]]; cipher[i + 1] = - (char) keymat[(mat[ch[1]][0] + 1) % 5][mat[ch[1]][1]]; + (char)keymat[(mat[ch[1]][0] + 1) % 5][mat[ch[1]][1]]; } } @@ -91,32 +104,36 @@ class Play { return cipher; } - char* Decrypt(char* plain, char keymat[5][5]) { + char *Decrypt(char *plain, char keymat[5][5]) + { unsigned int i = 0; int ch[2]; - for (i = 0; i < strlen(plain); i += 2) { + for (i = 0; i < strlen(plain); i += 2) + { ch[0] = plain[i] - 97; ch[1] = plain[i + 1] - 97; // Test 1 (Not Row or Column Match) - if (mat[ch[0]][0] != mat[ch[1]][0] - && mat[ch[0]][1] != mat[ch[1]][1]) { - decipher[i] = (char) keymat[mat[ch[0]][0]][mat[ch[1]][1]]; - decipher[i + 1] = (char) keymat[mat[ch[1]][0]][mat[ch[0]][1]]; - - } else if (mat[ch[0]][0] == mat[ch[1]][0]) { // Test 2 (Same Row) + if (mat[ch[0]][0] != mat[ch[1]][0] && mat[ch[0]][1] != mat[ch[1]][1]) + { + decipher[i] = (char)keymat[mat[ch[0]][0]][mat[ch[1]][1]]; + decipher[i + 1] = (char)keymat[mat[ch[1]][0]][mat[ch[0]][1]]; + } + else if (mat[ch[0]][0] == mat[ch[1]][0]) + { // Test 2 (Same Row) decipher[i] = - (char) keymat[mat[ch[0]][0]][mod( + (char)keymat[mat[ch[0]][0]][mod( mat[ch[0]][1] - 1, 5)]; - decipher[i + 1] = (char) keymat[mat[ch[1]][0]][mod( - mat[ch[1]][1] - 1, 5)]; - - } else if (mat[ch[0]][1] == mat[ch[1]][1]) { // Test 3 (Same Column) + decipher[i + 1] = (char)keymat[mat[ch[1]][0]][mod( + mat[ch[1]][1] - 1, 5)]; + } + else if (mat[ch[0]][1] == mat[ch[1]][1]) + { // Test 3 (Same Column) decipher[i] = - (char) keymat[mod(mat[ch[0]][0] - 1, 5)][mat[ch[0]][1]]; + (char)keymat[mod(mat[ch[0]][0] - 1, 5)][mat[ch[0]][1]]; decipher[i + 1] = - (char) keymat[mod(mat[ch[1]][0] - 1, 5)][mat[ch[1]][1]]; + (char)keymat[mod(mat[ch[1]][0] - 1, 5)][mat[ch[1]][1]]; } } @@ -129,7 +146,8 @@ class Play { return decipher; } - int mod(int a, int b) { + int mod(int a, int b) + { int res = a % b; while (res < 0) res += b; diff --git a/Rabin Cipher/main.cpp b/Rabin Cipher/main.cpp index b187c3a..80153ae 100644 --- a/Rabin Cipher/main.cpp +++ b/Rabin Cipher/main.cpp @@ -10,7 +10,8 @@ using namespace std; bool isprime(int); -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); @@ -27,7 +28,8 @@ int main() { cout << " Enter Number to Encrypt ( 0 to " << p * q << " ) : "; cin >> plain; - if (isprime(p) && isprime(q)) { + if (isprime(p) && isprime(q)) + { cout << " Public Key is : " << p * q << endl; Rabin rabin; cipher = rabin.Encrypt(plain, p * q); @@ -39,13 +41,16 @@ int main() { cout << "\n Decipher Text : "; for (int i = 0; i < 4; i++) cout << " " << decipher[i]; - } else { + } + else + { cout << p << " or " << q << " is/both not Prime"; } return 0; } -bool isprime(int number) { +bool isprime(int number) +{ bool flag = true; for (int i = 2; i <= number / 2; i++) if (number % i == 0) diff --git a/Rabin Cipher/rabin.h b/Rabin Cipher/rabin.h index 289a3d1..54841b1 100644 --- a/Rabin Cipher/rabin.h +++ b/Rabin Cipher/rabin.h @@ -16,21 +16,26 @@ using namespace std; -class Rabin { +class Rabin +{ int cipher; long *decipher; + public: - Rabin() { + Rabin() + { cipher = 0; decipher = new long[4]; } - int Encrypt(int plain, int pq) { + int Encrypt(int plain, int pq) + { cipher = Power(plain, 2) % pq; return cipher; } - long* Decrypt(int plain, int p, int q) { + long *Decrypt(int plain, int p, int q) + { int a[2], b[2]; a[0] = Power(plain, (p + 1) / 4) % p; @@ -44,7 +49,6 @@ class Rabin { cout << " " << b[0]; cout << " " << b[1]; - decipher[0] = ChineseRemainder(a[0], b[0], p, q); decipher[1] = ChineseRemainder(a[0], b[1], p, q); decipher[2] = ChineseRemainder(a[1], b[0], p, q); @@ -54,14 +58,16 @@ class Rabin { } private: - int Power(int a, int b) { + int Power(int a, int b) + { int res = a; for (int i = 1; i < b; i++) res *= a; return res; } - int ChineseRemainder(int a1, int a2, int p, int q) { + int ChineseRemainder(int a1, int a2, int p, int q) + { int res = 0; int M = p * q; @@ -75,8 +81,10 @@ class Rabin { return res; } - int Inverse(int number, int modd) { - for (int i = 1; i < modd; i++) { + int Inverse(int number, int modd) + { + for (int i = 1; i < modd; i++) + { if ((i * number) % modd == 1) return i; } diff --git a/Rotor Cipher/main.cpp b/Rotor Cipher/main.cpp index 8bdddd4..71df30d 100644 --- a/Rotor Cipher/main.cpp +++ b/Rotor Cipher/main.cpp @@ -8,7 +8,8 @@ #include "rotor.h" using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); diff --git a/Rotor Cipher/rotor.h b/Rotor Cipher/rotor.h index 6a5c637..e515f10 100644 --- a/Rotor Cipher/rotor.h +++ b/Rotor Cipher/rotor.h @@ -16,28 +16,36 @@ #define MAX 51 using namespace std; -class Rotor { +class Rotor +{ char *cipher, *decipher; int count; + public: - Rotor() { + Rotor() + { cipher = new char[MAX]; decipher = new char[MAX]; count = 0; } - char* Encrypt(char* plain, char* key, int n) { + char *Encrypt(char *plain, char *key, int n) + { int i = 0; for (i = 0; i < n; i++) RightShift(key); cout << "\n Key : " << key; - for (i = 0; plain[i] != '\0'; i++) { - if (isupper(plain[i])) { + for (i = 0; plain[i] != '\0'; i++) + { + if (isupper(plain[i])) + { cipher[i] = key[plain[i] - 65]; RightShift(key); - } else if (islower(plain[i])) { + } + else if (islower(plain[i])) + { cipher[i] = key[plain[i] - 97] - 65 + 97; RightShift(key); } @@ -53,26 +61,32 @@ class Rotor { return cipher; } - char* Decrypt(char* plain, char* key, int n) { + char *Decrypt(char *plain, char *key, int n) + { int i = 0, pos = 0; for (i = 0; i < n; i++) RightShift(key); cout << "\n Key : " << key; - for (i = 0; plain[i] != '\0'; i++) { + for (i = 0; plain[i] != '\0'; i++) + { for (int j = 0; j < 26; j++) if (toupper(plain[i]) == key[j]) pos = j; - if (isupper(plain[i])) { + if (isupper(plain[i])) + { decipher[i] = char(pos) + 65; RightShift(key); - } else if (islower(plain[i])) { + } + else if (islower(plain[i])) + { decipher[i] = char(pos) + 97; RightShift(key); - } else + } + else decipher[i] = plain[i]; } cout << "\n Key : " << key; @@ -84,12 +98,14 @@ class Rotor { return decipher; } - int getPosition() { + int getPosition() + { return count; } private: - void RightShift(char* key) { + void RightShift(char *key) + { int temp = key[25]; for (int i = 25; i > 0; i--) key[i] = key[i - 1]; @@ -97,7 +113,8 @@ class Rotor { count++; } - void Leftshift(char* key) { + void Leftshift(char *key) + { int temp = key[0]; for (int i = 0; i < 26; i++) key[i] = key[i + 1]; diff --git a/Stream (XOR) Cipher/main.cpp b/Stream (XOR) Cipher/main.cpp index 8331d52..36edd6e 100644 --- a/Stream (XOR) Cipher/main.cpp +++ b/Stream (XOR) Cipher/main.cpp @@ -11,7 +11,8 @@ using namespace std; void printBinary(char); -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); @@ -34,33 +35,38 @@ int main() { cout << "\n Decipher Text : " << decipher << endl; cout << endl; - for (unsigned int i = 0; i < strlen(plain); i++) { + for (unsigned int i = 0; i < strlen(plain); i++) + { cout << " "; -// printBinary(plain[i]); + // printBinary(plain[i]); cout << std::bitset<8>(plain[i]); } cout << endl; unsigned int position = 0; - for (unsigned int i = 0; i < strlen(plain); i++) { + for (unsigned int i = 0; i < strlen(plain); i++) + { cout << " "; -// printBinary(key[position]); + // printBinary(key[position]); cout << std::bitset<8>(key[position]); position++; if (position == strlen(key)) position = 0; } cout << endl; - for (unsigned int i = 0; i < strlen(plain); i++) { + for (unsigned int i = 0; i < strlen(plain); i++) + { cout << " "; -// printBinary(cipher[i]); + // printBinary(cipher[i]); cout << std::bitset<8>(cipher[i]); } return 0; } -void printBinary(char c) { - for (int i = 7; i >= 0; --i) { +void printBinary(char c) +{ + for (int i = 7; i >= 0; --i) + { std::cout << ((c & (1 << i)) ? '1' : '0'); } } diff --git a/Stream (XOR) Cipher/stream.h b/Stream (XOR) Cipher/stream.h index 140d9be..bc50b15 100644 --- a/Stream (XOR) Cipher/stream.h +++ b/Stream (XOR) Cipher/stream.h @@ -16,18 +16,23 @@ #define MAX 51 using namespace std; -class Stream { +class Stream +{ char *cipher, *decipher; + public: - Stream() { + Stream() + { cipher = new char[MAX]; decipher = new char[MAX]; } - char* Encrypt(char *plain, char *key) { + char *Encrypt(char *plain, char *key) + { unsigned int position = 0; unsigned int i = 0; - for (i = 0; i < strlen(plain); i++) { + for (i = 0; i < strlen(plain); i++) + { cipher[i] = (plain[i] ^ key[position]); position++; if (position == strlen(key)) @@ -40,10 +45,12 @@ class Stream { return cipher; } - char* Decrypt(char *plain, char *key) { + char *Decrypt(char *plain, char *key) + { unsigned int position = 0; unsigned int i = 0; - for (i = 0; i < strlen(plain); i++) { + for (i = 0; i < strlen(plain); i++) + { decipher[i] = (plain[i] ^ key[position]); position++; if (position == strlen(key)) diff --git a/Transposition Cipher/main.cpp b/Transposition Cipher/main.cpp index b849953..108f237 100644 --- a/Transposition Cipher/main.cpp +++ b/Transposition Cipher/main.cpp @@ -8,7 +8,8 @@ #include "transposition.h" using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); diff --git a/Transposition Cipher/transposition.h b/Transposition Cipher/transposition.h index 88b96bc..7b18403 100644 --- a/Transposition Cipher/transposition.h +++ b/Transposition Cipher/transposition.h @@ -16,26 +16,30 @@ #define MAX 51 using namespace std; -class Transposition { +class Transposition +{ char *cipher, *decipher; public: - Transposition() { + Transposition() + { cipher = new char[MAX]; decipher = new char[MAX]; } - char* Encrypt(char* plain, char* key) { + char *Encrypt(char *plain, char *key) + { unsigned int length = strlen(plain) / strlen(key) + 1; - 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 @@ -51,15 +55,18 @@ 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])) @@ -67,13 +74,15 @@ class Transposition { } 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++; } @@ -87,18 +96,20 @@ class Transposition { 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])) @@ -106,13 +117,15 @@ class Transposition { } 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++; } @@ -121,10 +134,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; } @@ -138,9 +153,12 @@ class Transposition { 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; diff --git a/Vigenere Cipher/main.cpp b/Vigenere Cipher/main.cpp index 1f769c0..08cbba8 100644 --- a/Vigenere Cipher/main.cpp +++ b/Vigenere Cipher/main.cpp @@ -7,24 +7,24 @@ #include "vigenere.h" using namespace std; -int main() { +int main() +{ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); char *plain, *key, *cipher, *decipher, *newkey; - plain = new char[MAX]; + plain = new char[MAX]; key = new char[MAX]; newkey = new char[MAX]; - cout << " Enter Plain Text (Max " << MAX - 1 << "): "; - gets(plain); - cout << " Enter Key Text (Max " << MAX - 1 << "): "; - gets(key); + cout << " Enter Plain Text (Max " << MAX - 1 << "): "; + gets(plain); + cout << " Enter Key Text (Max " << MAX - 1 << "): "; + gets(key); - - Vigenere vigenere; - cipher = vigenere.Encrypt(plain, key); + Vigenere vigenere; + cipher = vigenere.Encrypt(plain, key); decipher = vigenere.Decrypt(cipher, key); newkey = vigenere.Getkey(); @@ -32,5 +32,5 @@ int main() { cout << "\n New Key : " << newkey; cout << "\n Cipher Text : " << cipher; cout << "\n Decipher Text : " << decipher; - return 0; + return 0; } diff --git a/Vigenere Cipher/vigenere.h b/Vigenere Cipher/vigenere.h index 69a9d75..9811deb 100644 --- a/Vigenere Cipher/vigenere.h +++ b/Vigenere Cipher/vigenere.h @@ -15,22 +15,26 @@ #define MAX 51 using namespace std; -class Vigenere { +class Vigenere +{ char *cipher, *decipher, *newkey; public: - Vigenere() { + Vigenere() + { cipher = new char[MAX]; decipher = new char[MAX]; newkey = new char[MAX]; } - char* Encrypt(char* plain, char* key) { + char *Encrypt(char *plain, char *key) + { int i = 0; newkey = GenNewKey(key, plain); - for (i = 0; plain[i] != '\0'; i++) { + for (i = 0; plain[i] != '\0'; i++) + { if (isupper(plain[i])) cipher[i] = (((plain[i] - 65) + (newkey[i] - 65)) % 26) + 65; else if (islower(plain[i])) @@ -46,17 +50,17 @@ class Vigenere { return cipher; } - char* Decrypt(char* plain, char* key) { + char *Decrypt(char *plain, char *key) + { int i = 0; newkey = GenNewKey(key, plain); - for (i = 0; plain[i] != '\0'; i++) { + for (i = 0; plain[i] != '\0'; i++) + { if (isupper(plain[i])) - decipher[i] = (((plain[i] - 65) + 26 - (newkey[i] - 65)) % 26) - + 65; + decipher[i] = (((plain[i] - 65) + 26 - (newkey[i] - 65)) % 26) + 65; else if (islower(plain[i])) - decipher[i] = (((plain[i] - 97) + 26 - (newkey[i] - 65)) % 26) - + 97; + decipher[i] = (((plain[i] - 97) + 26 - (newkey[i] - 65)) % 26) + 97; else decipher[i] = plain[i]; } @@ -68,9 +72,11 @@ class Vigenere { return decipher; } - char* GenNewKey(char* key, char* plain) { + char *GenNewKey(char *key, char *plain) + { int i = 0, j = 0; - for (i = 0; plain[i] != '\0'; i++, j++) { + for (i = 0; plain[i] != '\0'; i++, j++) + { if (key[j] == '\0') j = 0; newkey[i] = toupper(key[j]); @@ -79,10 +85,10 @@ class Vigenere { return newkey; } - char* Getkey() { + char *Getkey() + { return newkey; } - }; #endif /* VIGENERE_H */