Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
e3cde4be54 | |||
3a2e110ad2 | |||
![]() |
c68c270dbf | ||
957d28ff06 | |||
![]() |
52769a7985 | ||
0242ed0256 |
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
|
2
exec.sh
2
exec.sh
@ -1,3 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
gcc -Wall src/main.c src/functions.c src/caesar.c src/vigenere.c -o output/main && ./output/main $1 $2 $3 $4
|
||||
gcc -Wall src/main.c src/functions.c src/caesar.c src/vigenere.c src/polybius.c -o main && ./main $1 $2 $3 $4
|
||||
|
25
src/caesar.c
25
src/caesar.c
@ -1,31 +1,6 @@
|
||||
#include "caesar.h"
|
||||
|
||||
#define BUFFER_SIZE 128
|
||||
|
||||
/*
|
||||
This function read the file block by block until the end of it
|
||||
*/
|
||||
int readFile(FILE *f, int *size, char *buffer){
|
||||
int index = 0;
|
||||
char c;
|
||||
int end = 0;
|
||||
|
||||
do{
|
||||
c = fgetc(f);
|
||||
buffer[index] = c;
|
||||
index++;
|
||||
*size += 1;
|
||||
|
||||
// End of the file, we stop it
|
||||
if (c == EOF){
|
||||
buffer[index - 1] = '\0';
|
||||
index = BUFFER_SIZE;
|
||||
end = 1;
|
||||
}
|
||||
}while(index < BUFFER_SIZE);
|
||||
|
||||
return end;
|
||||
}
|
||||
/*
|
||||
s -> path source
|
||||
d -> path destination
|
||||
|
@ -6,7 +6,6 @@
|
||||
#include "functions.h"
|
||||
|
||||
/* Functions */
|
||||
int foo(FILE *, int *, char *);
|
||||
int cryptCaesar(const char *, const char *, const int);
|
||||
int decryptCaesar(const char *, const char *, const int);
|
||||
|
||||
|
@ -12,12 +12,35 @@ int fileExist(const char *path) {
|
||||
FILE *f=NULL;
|
||||
f=fopen(path,"r");
|
||||
|
||||
if(f==NULL){
|
||||
if(f==NULL)
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
This function read the file block by block until the end of it
|
||||
*/
|
||||
int readFile(FILE *f, int *size, char *buffer){
|
||||
int index = 0;
|
||||
char c;
|
||||
int end = 0;
|
||||
|
||||
do{
|
||||
c = fgetc(f);
|
||||
buffer[index] = c;
|
||||
index++;
|
||||
*size += 1;
|
||||
|
||||
// End of the file, we stop it
|
||||
if (c == EOF){
|
||||
buffer[index - 1] = '\0';
|
||||
index = BUFFER_SIZE;
|
||||
end = 1;
|
||||
}
|
||||
}while(index < BUFFER_SIZE);
|
||||
|
||||
return end;
|
||||
}
|
||||
/*
|
||||
path -> path source
|
||||
This function copy all characters of the file in the buffer
|
||||
|
@ -5,8 +5,11 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
/* Functions */
|
||||
void usage();
|
||||
int readFile(FILE *, int *, char *);
|
||||
int fileExist(const char *path);
|
||||
int copyFile(const char *path, char *buffer);
|
||||
int addDataToFile(const char *data, const char *path);
|
||||
|
22
src/main.c
22
src/main.c
@ -1,6 +1,7 @@
|
||||
#include "functions.h"
|
||||
#include "caesar.h"
|
||||
#include "vigenere.h"
|
||||
#include "polybius.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int error = 0;
|
||||
@ -14,7 +15,7 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
// Check kind of encryption
|
||||
for(i = 0; i < 3; i++){
|
||||
for(i = 0; i < 4; i++){
|
||||
if(strcmp(argv[1], options[i]) == 0)
|
||||
error++;
|
||||
}
|
||||
@ -100,7 +101,24 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
// Polybius square
|
||||
else if(strcmp(argv[1], "-s") == 0){
|
||||
char key[26];
|
||||
int res;
|
||||
|
||||
printf("Your key: ");
|
||||
scanf("%s", key);
|
||||
|
||||
if (strlen(key) > 26){
|
||||
printf("Your key is too big. Choose a small one\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(strcmp(hook, "crypt") == 0)
|
||||
res = cryptPolybius(fileSrc, fileDst, key);
|
||||
else
|
||||
res = decryptPolybius(fileSrc, fileDst, key);
|
||||
|
||||
if (res != 0)
|
||||
printf("Failed to crypt/decrypt the file\n");
|
||||
}
|
||||
|
||||
/* Display error */
|
||||
@ -111,7 +129,5 @@ int main(int argc, char *argv[]) {
|
||||
*/
|
||||
|
||||
free(fileDst);
|
||||
//free(buffer);
|
||||
//free(data);
|
||||
return 0;
|
||||
}
|
||||
|
174
src/polybius.c
Normal file
174
src/polybius.c
Normal file
@ -0,0 +1,174 @@
|
||||
#include "polybius.h"
|
||||
|
||||
|
||||
int cryptPolybius(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);
|
||||
|
||||
/********* 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' || c == ' '){
|
||||
fputc(c, fDst);
|
||||
fputc(0, fDst); // For having a couple of number
|
||||
}
|
||||
}
|
||||
|
||||
size = 0;
|
||||
memset(data, 0, BUFFER_SIZE);
|
||||
memset(dataEncrypted, 0, BUFFER_SIZE);
|
||||
}while(end != 1);
|
||||
|
||||
/* Close files */
|
||||
fclose(f);
|
||||
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;
|
||||
|
||||
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] >= 48 && data[i] <= 53) {
|
||||
int c1 = data[i] - '0';
|
||||
int c2 = data[i+1] - '0';
|
||||
char c = polybius[c1][c2];
|
||||
printf("%c", c);
|
||||
fputc(c, fDst);
|
||||
}
|
||||
else{
|
||||
printf("%c", data[i]);
|
||||
fputc(data[i], fDst);
|
||||
}
|
||||
}
|
||||
|
||||
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 < POLYBIUS_SIZE; i++){
|
||||
polybius[pos][i] = p[*(posPolybius)];
|
||||
*posPolybius = *posPolybius + 1;
|
||||
}
|
||||
}
|
||||
static int findKeyInSquare(const char *polybius, char c){
|
||||
// We exclude the 'J' character
|
||||
for (int i = 0; i < ALPHABET_SIZE - 1; i++)
|
||||
if (c == polybius[i])
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
This function generate the polybius square
|
||||
*/
|
||||
void generateSquarePolybius(char polybius[POLYBIUS_SIZE][POLYBIUS_SIZE], const char *key){
|
||||
char a[ALPHABET_SIZE - 1] = {0}; // The size is 25, we exclude the J character
|
||||
|
||||
int pos = 0;
|
||||
for (int i = 0; i < strlen(key); i++){
|
||||
char k;
|
||||
if (key[i] >= 'a' && key[i] <= 'z')
|
||||
k = key[i] - 32; // 32 = 26 + special characters
|
||||
else k = key[i];
|
||||
int res = findKeyInSquare(a, k);
|
||||
if (res == 0)
|
||||
a[pos++] = k;
|
||||
}
|
||||
int n = 0;
|
||||
int t = pos;
|
||||
for (int i = pos; i < ALPHABET_SIZE + pos; i++){
|
||||
char c = 'A' + n;
|
||||
if (c == 'J'){
|
||||
// We check if I is in the array, if yes, we do not add it
|
||||
}
|
||||
/*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);
|
||||
|
||||
if (res == 0)
|
||||
a[t++] = c;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
pos = 0;
|
||||
fillSquare(0, &pos, a, polybius);
|
||||
fillSquare(1, &pos, a, polybius);
|
||||
fillSquare(2, &pos, a, polybius);
|
||||
fillSquare(3, &pos, a, polybius);
|
||||
fillSquare(4, &pos, a, polybius);
|
||||
}
|
15
src/polybius.h
Normal file
15
src/polybius.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef H_POLYBIUS
|
||||
#define H_POLYBIUS
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "functions.h"
|
||||
|
||||
#define POLYBIUS_SIZE 5
|
||||
#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