Created
March 1, 2018 14:36
-
-
Save Spationaute/430e8be7f0cc66bbbe90b34863e548ab to your computer and use it in GitHub Desktop.
Simple Vernam Encryption C Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Algorithme de cryptage Vernam | |
*** Grabriel Lemieux | |
*** | |
**/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
typedef unsigned int uint; | |
uint flags; | |
void printv(const char toPrint[]); | |
uint verbose(); | |
uint encode(char toEncode[]); | |
uint decode(char toDecode[]); | |
int getFileSize(FILE *toGet); | |
int main(int args,char* argv[]){ | |
flags=0; | |
int opt=0; | |
while(opt != -1){ | |
opt = getopt(args,argv,"cdv"); | |
switch(opt){ | |
case 'c': | |
flags|=0x01; | |
printf("As it's use user generated entropy, the encryption will take some time.\n'"); | |
break; | |
case 'd': | |
flags|=0x02; | |
break; | |
case 'v': | |
flags|=0x04; | |
printf("Vernam V 0.1 -- Verbose Mode --\n"); | |
break; | |
case 'h': | |
break; | |
} | |
} | |
if( (flags & 0x02) && (flags & 0x01)){ | |
printf("Cannot do the both in same time!\n"); | |
return -1; | |
} | |
if(flags & 0x01){ | |
int i=0; | |
for(i=optind;i<args;++i){ | |
if(verbose()){ | |
printf("Encoding file: %s \n ",argv[i]); | |
} | |
encode(argv[i]); | |
} | |
} | |
if(flags & 0x02){ | |
int i=0; | |
for(i=optind;i<args;++i){ | |
if(verbose()){ | |
printf("Decoding file: %s \n",argv[i]); | |
} | |
decode(argv[i]); | |
} | |
} | |
return 0; | |
} | |
void printv(const char toPrint[]){ | |
if(flags & 0x04){ | |
printf("%s\n",toPrint); | |
} | |
} | |
uint verbose(){ | |
return (flags & 0x04)?1:0; | |
} | |
uint encode(char toEncode[]){ | |
//Le fichier à lire et le fichier à encoder | |
FILE* fichier = fopen(toEncode,"r"); | |
FILE* random = fopen("/dev/random","r"); | |
if(fichier == NULL){ | |
printf("File %s not found."); | |
return 0; | |
} | |
//Connaitre la taille du fichier | |
fseek(fichier,0,SEEK_END); | |
int size = ftell(fichier); | |
rewind(fichier); | |
printv("\tLoading file in memory."); | |
char encoded[size]; | |
char key[size]; | |
//Charger la clef en mémoire | |
int i; | |
for(i=0;i<size;i++){ | |
fread(&(key[i]),sizeof(char),1,random); | |
} | |
//Fermer le fichier random, le chargement est terminé | |
fclose(random); | |
printv("\tCoding the file:"); | |
//Appliquer la clef | |
for(i=0;i<size;i++){ | |
char temp; | |
fread(&temp,sizeof(char),1,fichier); | |
encoded[i] = temp ^ key[i]; | |
if(verbose()){ | |
printf("%c ^ %c (%c)",temp,key[i],encoded[i]); | |
} | |
} | |
printv(" "); | |
printv("\tSaving encoded file."); | |
//Éviter les attaques en assignant la bonne valeur à "toSavePath" | |
int strlengtin = strlen(toEncode) + strlen(".enc") ; | |
char toSavePath[strlengtin]; | |
char toSaveKPath[strlengtin]; | |
strcpy(toSavePath,toEncode); | |
strcat(toSavePath,".enc"); | |
strcpy(toSaveKPath,toEncode); | |
strcat(toSaveKPath,".key"); | |
//Sauvegarder le fichier | |
FILE* toSave = fopen(toSavePath,"w"); | |
FILE* toSaveKey = fopen(toSaveKPath,"w"); | |
for(i=0;i<size;++i){ | |
fwrite(&(encoded[i]),sizeof(char),1,toSave); | |
fwrite(&(key[i]),sizeof(char),1,toSaveKey); | |
} | |
fclose(toSave); | |
fclose(toSaveKey); | |
return 1; | |
} | |
uint decode(char toDecode[]){ | |
//Éviter les attaques en assignant la bonne valeur "toGetPath" | |
int strlengtin = strlen(toDecode) + strlen(".enc"); | |
char toGetPath[strlengtin]; | |
char toGetKPath[strlengtin]; | |
strcpy(toGetPath,toDecode); | |
strcat(toGetPath,".enc"); | |
strcpy(toGetKPath,toDecode); | |
strcat(toGetKPath,".key"); | |
printv("\tOpenning key and source file."); | |
FILE* inEncode = fopen(toGetPath,"r"); | |
FILE* inKeyCode= fopen(toGetKPath,"r"); | |
if( (inEncode==NULL) && (inKeyCode==NULL) ){ | |
printf("\tFile %s or %s not found.\n",inEncode,inKeyCode); | |
return 0; | |
} | |
int sizeC = getFileSize(inEncode); | |
int sizeD = getFileSize(inKeyCode); | |
if( sizeC != sizeD ){ | |
printf("\tThe size doesn't fit.\n"); | |
return 0; | |
} | |
printv("\tReading files."); | |
char decoded[sizeC]; | |
int i; | |
for(i=0;i<sizeC;++i){ | |
char tempKey; | |
char tempCoded; | |
fread(&(tempKey),sizeof(char),1,inKeyCode); | |
fread(&(tempCoded),sizeof(char),1,inEncode); | |
decoded[i]= tempKey ^ tempCoded; | |
if(verbose()){ | |
printf("%c ^ %c (%c)",tempCoded,tempKey,decoded[i]); | |
} | |
} | |
fclose(inEncode); | |
fclose(inKeyCode); | |
FILE* decodedFile = fopen(toDecode,"w"); | |
printv("\tWritting of the coded file."); | |
for(i=0;i<sizeC;++i){ | |
fwrite(&(decoded[i]),sizeof(char),1,decodedFile); | |
} | |
fclose(decodedFile); | |
return 1; | |
} | |
int getFileSize(FILE *toGet){ | |
fseek(toGet,0,SEEK_END); | |
int size = ftell(toGet); | |
rewind(toGet); | |
return size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment