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 <string.h>
|
||||
|
||||
#define BUFFER_SIZE 4092
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
/* Functions */
|
||||
void usage();
|
||||
|
@ -113,6 +113,8 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
if(strcmp(hook, "crypt") == 0)
|
||||
cryptPolybius(fileSrc, fileDst, key);
|
||||
else
|
||||
decryptPolybius(fileSrc, fileDst, key);
|
||||
}
|
||||
|
||||
/* Display error */
|
||||
|
@ -6,12 +6,13 @@ int cryptPolybius(const char *filenameSrc, const char *filenameDst, const char *
|
||||
FILE *f = NULL, *fDst = NULL;
|
||||
char data[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);
|
||||
|
||||
for (int i = 0; i < POLYBIUS_SIZE; i++){
|
||||
for (int j = 0; j < POLYBIUS_SIZE; j++)
|
||||
for (i = 0; i < POLYBIUS_SIZE; i++){
|
||||
for (j = 0; j < POLYBIUS_SIZE; j++)
|
||||
printf("%c ", polybius[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
@ -28,6 +29,23 @@ int cryptPolybius(const char *filenameSrc, const char *filenameDst, const char *
|
||||
end = readFile(f, &size, data);
|
||||
|
||||
/********* 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;
|
||||
memset(data, 0, BUFFER_SIZE);
|
||||
memset(dataEncrypted, 0, BUFFER_SIZE);
|
||||
@ -38,11 +56,64 @@ int cryptPolybius(const char *filenameSrc, const char *filenameDst, const char *
|
||||
fclose(fDst);
|
||||
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
|
||||
*/
|
||||
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)];
|
||||
*posPolybius = *posPolybius + 1;
|
||||
}
|
||||
@ -77,14 +148,14 @@ void generateSquarePolybius(char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE], const c
|
||||
if (c == 'J'){
|
||||
// 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
|
||||
int res = findKeyInSquare(a, 'J');
|
||||
// J not in the array, we add it
|
||||
if (res == 0){
|
||||
a[t++] = 'A';
|
||||
}
|
||||
}
|
||||
}*/
|
||||
if (c != 'J'){
|
||||
int res = findKeyInSquare(a, c);
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
#define ALPHABET_SIZE 26
|
||||
|
||||
int cryptPolybius(const char *, const char *, const char *);
|
||||
int decryptPolybius(const char *, const char *, const char *);
|
||||
void generateSquarePolybius(char polybius[5][5], const char *);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user