last commit

This commit is contained in:
theBigBlase 2023-09-27 18:37:18 -04:00
parent 742db2cf2a
commit 845c277cb0
3 changed files with 225 additions and 60 deletions

View file

@ -1,3 +1,11 @@
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <paum1202@usherbrooke.ca> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. -violette paulin
* ----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -23,10 +31,11 @@ static char *gen2();
static char *e3(char *, char *);
static char *d3(char *, char *);
static char *gen3();
static void eve_shenanigans(char *);
static inline char genChar();
static inline char genByte();
static void printHex(char *);
static inline char gen_char();
static inline char gen_byte();
static void print_hex(char *);
int
main(int argc, char *argv[])
@ -72,13 +81,15 @@ main(int argc, char *argv[])
char msg_otp[] =
"ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd";
printf("key: %s\n", key_otp);
printf("key as hex: ");
print_hex(key_otp);
printf("msg before: %s\n", msg_otp);
printf("msg as hex: ");
printHex(msg_otp);
print_hex(msg_otp);
char *cypher_otp = e2(msg_otp, key_otp);
printf("cypher: %s\n", cypher_otp);
printf("cypher as hex: ");
printHex(cypher_otp);
print_hex(cypher_otp);
char *res_otp = d2(cypher_otp, key_otp);
printf("msg after: %s\n", res_otp);
@ -94,10 +105,38 @@ main(int argc, char *argv[])
char *key_vignere = gen3();
char msg_vignere[] = "ceciestlemessageclairadechiffre";
printf("key: %s\n", key_vignere);
printf("key as hex: ");
print_hex(key_vignere);
printf("msg before: %s\n", msg_vignere);
printf("msg as hex: ");
print_hex(msg_vignere);
char *cypher_vignere = e3(msg_vignere, key_vignere);
printf("cypher: %s\n", cypher_vignere);
printf("cypher as hex: ");
print_hex(cypher_vignere);
char *res_vignere = d3(cypher_vignere, key_vignere);
printf("msg after: %s\n", res_vignere);
free(res_vignere);
free(cypher_vignere);
free(key_vignere);
#endif
#ifdef PART3C
/* PART 3 */
printf("-------PART3C-------\n");
char *key_vignere = gen3();
char msg_vignere[] = "ceciestlemessageclairadechiffre";
printf("key: %s\n", key_vignere);
printf("msg before: %s\n", msg_vignere);
char *cypher_vignere = e3(msg_vignere, key_vignere);
printf("cypher: %s\n", cypher_vignere);
eve_shenanigans(cypher_vignere);
printf("cypher tempered by Eve: %s\n", cypher_vignere);
char *res_vignere = d3(cypher_vignere, key_vignere);
printf("msg after: %s\n", res_vignere);
@ -188,14 +227,14 @@ gen2()
{
char *res = (char *)calloc(sizeof(char) * SIZE_OTP, 1);
for (unsigned char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) {
res[k] = genByte();
res[k + 1] = genByte();
res[k + 2] = genByte();
res[k + 3] = genByte();
res[k + 4] = genByte();
res[k + 5] = genByte();
res[k + 6] = genByte();
res[k + 7] = genByte();
res[k] = gen_byte();
res[k + 1] = gen_byte();
res[k + 2] = gen_byte();
res[k + 3] = gen_byte();
res[k + 4] = gen_byte();
res[k + 5] = gen_byte();
res[k + 6] = gen_byte();
res[k + 7] = gen_byte();
}
res[SIZE_OTP - 1] = '\0';
@ -204,7 +243,7 @@ gen2()
}
static inline char
genByte()
gen_byte()
{
return rand() % 256;
}
@ -250,7 +289,7 @@ d3(char *cypher, char *key)
}
static inline char
genChar()
gen_char()
{
return rand() % SIZE_ALPH;
}
@ -260,14 +299,14 @@ gen3()
{
char *res = (char *)calloc(sizeof(char) * SIZE_MSG, 1);
for (unsigned char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) {
res[k] = genChar() + 'a';
res[k + 1] = genChar() + 'a';
res[k + 2] = genChar() + 'a';
res[k + 3] = genChar() + 'a';
res[k + 4] = genChar() + 'a';
res[k + 5] = genChar() + 'a';
res[k + 6] = genChar() + 'a';
res[k + 7] = genChar() + 'a';
res[k] = gen_char() + 'a';
res[k + 1] = gen_char() + 'a';
res[k + 2] = gen_char() + 'a';
res[k + 3] = gen_char() + 'a';
res[k + 4] = gen_char() + 'a';
res[k + 5] = gen_char() + 'a';
res[k + 6] = gen_char() + 'a';
res[k + 7] = gen_char() + 'a';
}
res[SIZE_MSG - 1] = '\0';
@ -275,11 +314,36 @@ gen3()
}
static void
printHex(char *str)
eve_shenanigans(char *cypher)
{
unsigned short k = 0;
unsigned short c_count = 0;
while (cypher[k] != '\0' && c_count < 2) {
if (cypher[k] == 'c')
++c_count;
++k;
}
while (cypher[k] != '\0') {
if (cypher[k] == 'c')
cypher[k] += 9;
++k;
}
while (cypher[k] != '\0') {
if (cypher[k] == 'c')
cypher[k] += 18;
++k;
}
}
static void
print_hex(char *str)
{
short k = 0;
while (str[k] != '\0') {
printf("%02X ", str[k]);
printf("%02X ", str[k] & 0xff);
++k;
}
printf("\n");