Upgrade polybius
This commit is contained in:
parent
c68c270dbf
commit
3a2e110ad2
File diff suppressed because one or more lines are too long
16
examples/sonnet116.txt
Normal file
16
examples/sonnet116.txt
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
Let me not to the marriage of true minds
|
||||||
|
Admit impediments. Love is not love
|
||||||
|
Which alters when it alteration finds,
|
||||||
|
Or bends with the remover to remove:
|
||||||
|
O no! it is an ever-fixed mark
|
||||||
|
That looks on tempests and is never shaken;
|
||||||
|
It is the star to every wandering bark,
|
||||||
|
Whose worth’s unknown, although his height be taken.
|
||||||
|
Love’s not Time’s fool, though rosy lips and cheeks
|
||||||
|
Within his bending sickle’s compass come:
|
||||||
|
Love alters not with his brief hours and weeks,
|
||||||
|
But bears it out even to the edge of doom.
|
||||||
|
If this be error and upon me proved,
|
||||||
|
I never writ, nor no man ever loved.
|
||||||
|
|
||||||
|
William Shakespeare
|
@ -5,7 +5,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define BUFFER_SIZE 4092
|
#define BUFFER_SIZE 4096
|
||||||
|
|
||||||
/* Functions */
|
/* Functions */
|
||||||
void usage();
|
void usage();
|
||||||
|
@ -113,6 +113,8 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
if(strcmp(hook, "crypt") == 0)
|
if(strcmp(hook, "crypt") == 0)
|
||||||
cryptPolybius(fileSrc, fileDst, key);
|
cryptPolybius(fileSrc, fileDst, key);
|
||||||
|
else
|
||||||
|
decryptPolybius(fileSrc, fileDst, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Display error */
|
/* Display error */
|
||||||
|
@ -6,12 +6,13 @@ int cryptPolybius(const char *filenameSrc, const char *filenameDst, const char *
|
|||||||
FILE *f = NULL, *fDst = NULL;
|
FILE *f = NULL, *fDst = NULL;
|
||||||
char data[BUFFER_SIZE];
|
char data[BUFFER_SIZE];
|
||||||
char dataEncrypted[BUFFER_SIZE];
|
char dataEncrypted[BUFFER_SIZE];
|
||||||
int size = 0, end = 0;
|
int size = 0, end = 0, i = 0, j = 0;
|
||||||
|
char c = 0;
|
||||||
|
|
||||||
generateSquarePolybius(polybius, key);
|
generateSquarePolybius(polybius, key);
|
||||||
|
|
||||||
for (int i = 0; i < POLYBIUS_SIZE; i++){
|
for (i = 0; i < POLYBIUS_SIZE; i++){
|
||||||
for (int j = 0; j < POLYBIUS_SIZE; j++)
|
for (j = 0; j < POLYBIUS_SIZE; j++)
|
||||||
printf("%c ", polybius[i][j]);
|
printf("%c ", polybius[i][j]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
@ -28,6 +29,23 @@ int cryptPolybius(const char *filenameSrc, const char *filenameDst, const char *
|
|||||||
end = readFile(f, &size, data);
|
end = readFile(f, &size, data);
|
||||||
|
|
||||||
/********* Encryption **********/
|
/********* Encryption **********/
|
||||||
|
for (i = 0; i < size; i++){
|
||||||
|
if (data[i] >= 'a' && data[i] <= 'z')
|
||||||
|
c = data[i] - 32;
|
||||||
|
else c = data[i];
|
||||||
|
|
||||||
|
for (j = 0; j < POLYBIUS_SIZE; j++){
|
||||||
|
for (int x = 0; x < POLYBIUS_SIZE; x++){
|
||||||
|
if (c == polybius[j][x]){
|
||||||
|
fputc(j + '0', fDst);
|
||||||
|
fputc(x + '0', fDst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (c == '\n')
|
||||||
|
fputc('\n', fDst);
|
||||||
|
}
|
||||||
|
|
||||||
size = 0;
|
size = 0;
|
||||||
memset(data, 0, BUFFER_SIZE);
|
memset(data, 0, BUFFER_SIZE);
|
||||||
memset(dataEncrypted, 0, BUFFER_SIZE);
|
memset(dataEncrypted, 0, BUFFER_SIZE);
|
||||||
@ -38,11 +56,64 @@ int cryptPolybius(const char *filenameSrc, const char *filenameDst, const char *
|
|||||||
fclose(fDst);
|
fclose(fDst);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
int decryptPolybius(const char *filenameSrc, const char *filenameDst, const char *key){
|
||||||
|
char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE];
|
||||||
|
FILE *f = NULL, *fDst = NULL;
|
||||||
|
char data[BUFFER_SIZE];
|
||||||
|
char dataEncrypted[BUFFER_SIZE];
|
||||||
|
int size = 0, end = 0, i = 0, j = 0;
|
||||||
|
char c = 0;
|
||||||
|
|
||||||
|
generateSquarePolybius(polybius, key);
|
||||||
|
|
||||||
|
for (i = 0; i < POLYBIUS_SIZE; i++){
|
||||||
|
for (j = 0; j < POLYBIUS_SIZE; j++)
|
||||||
|
printf("%c ", polybius[i][j]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
// Open the file
|
||||||
|
f = fopen(filenameSrc, "r+");
|
||||||
|
if (f == NULL) return -1;
|
||||||
|
|
||||||
|
fDst = fopen(filenameDst, "w");
|
||||||
|
if (fDst == NULL) {
|
||||||
|
fclose(f);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
end = readFile(f, &size, data);
|
||||||
|
|
||||||
|
/********* Decryption **********/
|
||||||
|
for (i = 0; i < size; i += 2){
|
||||||
|
if (data[i] >= 0 && data[i] <= POLYBIUS_SIZE) {
|
||||||
|
int c1 = data[i] - '0';
|
||||||
|
int c2 = data[i+1] - '0';
|
||||||
|
char c = polybius[c1][c2];
|
||||||
|
//printf("%d%d ", c1, c2);
|
||||||
|
//printf("%d ", c1);
|
||||||
|
printf("%c", c);
|
||||||
|
//printf("%c", data[i]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("%c", data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
size = 0;
|
||||||
|
memset(data, 0, BUFFER_SIZE);
|
||||||
|
memset(dataEncrypted, 0, BUFFER_SIZE);
|
||||||
|
}while(end != 1);
|
||||||
|
|
||||||
|
/* Close files */
|
||||||
|
fclose(f);
|
||||||
|
fclose(fDst);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
This function fill the polybius square
|
This function fill the polybius square
|
||||||
*/
|
*/
|
||||||
static void fillSquare(int pos, int *posPolybius, char *p, char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE]){
|
static void fillSquare(int pos, int *posPolybius, char *p, char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE]){
|
||||||
for (int i = 0; i < 5; i++){
|
for (int i = 0; i < POLYBIUS_SIZE; i++){
|
||||||
polybius[pos][i] = p[*(posPolybius)];
|
polybius[pos][i] = p[*(posPolybius)];
|
||||||
*posPolybius = *posPolybius + 1;
|
*posPolybius = *posPolybius + 1;
|
||||||
}
|
}
|
||||||
@ -77,14 +148,14 @@ void generateSquarePolybius(char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE], const c
|
|||||||
if (c == 'J'){
|
if (c == 'J'){
|
||||||
// We check if I is in the array, if yes, we do not add it
|
// We check if I is in the array, if yes, we do not add it
|
||||||
}
|
}
|
||||||
if (c == 'I'){
|
/*if (c == 'I'){
|
||||||
// We check if J is in the array, is yes, we do not add it
|
// We check if J is in the array, is yes, we do not add it
|
||||||
int res = findKeyInSquare(a, 'J');
|
int res = findKeyInSquare(a, 'J');
|
||||||
// J not in the array, we add it
|
// J not in the array, we add it
|
||||||
if (res == 0){
|
if (res == 0){
|
||||||
a[t++] = 'A';
|
a[t++] = 'A';
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
if (c != 'J'){
|
if (c != 'J'){
|
||||||
int res = findKeyInSquare(a, c);
|
int res = findKeyInSquare(a, c);
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#define ALPHABET_SIZE 26
|
#define ALPHABET_SIZE 26
|
||||||
|
|
||||||
int cryptPolybius(const char *, const char *, const char *);
|
int cryptPolybius(const char *, const char *, const char *);
|
||||||
|
int decryptPolybius(const char *, const char *, const char *);
|
||||||
void generateSquarePolybius(char polybius[5][5], const char *);
|
void generateSquarePolybius(char polybius[5][5], const char *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user