init commit ; code done

This commit is contained in:
BigBlase 2023-09-27 10:31:40 -04:00
commit 06aabe1dbf
4 changed files with 332 additions and 0 deletions

16
.vimspector.json Normal file
View file

@ -0,0 +1,16 @@
{
"configurations": {
"Launch": {
"adapter": "vscode-cpptools",
"filetypes": [ "cpp", "c", "objc", "rust" ], // optional
"configuration": {
"request": "launch",
"program": "./build/main.o",
"cwd": "/home/madeleine/classes/S9/crypto/",
"externalConsole": true,
"MIMode": "gdb"
}
}
}
}

23
Makefile Normal file
View file

@ -0,0 +1,23 @@
SRC = src/main.c
OJB = $(SRC:.c=.o)
OUT = build
CC = /usr/bin/gcc
CFLAGS = -ansi -Wall -std=c99
CFDEBUG = -ansi -Wall -fsanitize=address -g -std=c99
RM = /bin/rm -f $(OUT)
%.o: %.c
$(CC) -c $(CFLAGS)
build:
$(CC) $(SRC) $(CFLAGS) -o $(OUT)/main.o
debug:
$(CC) $(SRC) $(CFDEBUG) -o $(OUT)/main.o
clean:
$(RM) $(OBJ) $(OUT)/main.o
run: $(SRC)
$(CC) $(SRC) $(CFLAGS) -o $(OUT)/main.o && ./build/main.o

33
main.typ Normal file
View file

@ -0,0 +1,33 @@
#import "@preview/algo:0.3.2": algo, i, d, comment, code
#set page(
numbering: "1 / 1",
header: [
#set text(8pt)
_IFT436, Devoir 1 #h(1fr) Paulin M, 2023_
],
)
#let title(content) = {
pagebreak(weak:true)
set text(size:15pt)
set align(center)
v(10pt)
[#content]
v(10pt)
}
#title[= Devoir 1]
#line(length: 100%)
#v(70pt)
#line(length: 100%)
= Notes
Le code présenté est retrouvable sur ssh://bigblase.xyz:/srv/git/crypto1
#set heading(numbering: "I) a) i)")
= Chiffre de César

260
src/main.c Normal file
View file

@ -0,0 +1,260 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define REMOVE_SIGN(x) ( \
(x) & ~ (1u << 8)\
)
#define SIZE_ALPH 26
#define SIZE_MSG 32
#define SIZE_OTP 64
static char *e1(char *, char);
static char *d1(char *, char);
static inline char gen1();
static char *e2(char *, char*);
static char *d2(char *, char*);
static char *gen2();
static char *e3(char *, char*);
static char *d3(char *, char*);
static char *gen3();
static inline char genChar();
static inline char genByte();
int
main(int argc, char *argv[])
{
srand(time(0)); /* init rand */
printf("-------PART 1-------\n");
int key = gen1();
/* SYMBOLS */
char msg[] = "coding w/ freebsd style :)";
printf("key: %d\n", key);
printf("msg before: %s\n", msg);
char *cypher = e1(msg, key);
printf("cypher: %s\n", cypher);
char *res = d1(cypher, key);
printf("msg after: %s\n", res);
free(cypher);
free(res);
/* MESSAGE */
/* len = 32 (dont forget nullbyte!) */
char msg32[] = "ceciestlemessageclairadechiffre";
printf("key: %d\n", key);
printf("msg before: %s\n", msg32);
char *cypher32 = e1(msg32, key);
printf("cypher: %s\n", cypher32);
char *res32 = d1(cypher32, key);
printf("msg after: %s\n", res32);
free(cypher32);
free(res32);
/* PART 2 */
printf("-------PART 2-------\n");
char *key_otp = gen2();
/* 64 value max */
char msg_otp[] =
"ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd";
printf("key: %s\n", key_otp);
printf("msg before: %s\n", msg_otp);
char *cypher_otp = e2(msg_otp, key_otp);
printf("cypher: %s\n", cypher_otp);
char *res_otp = d2(cypher_otp, key_otp);
printf("msg after: %s\n", res_otp);
free(cypher_otp);
free(res_otp);
free(key_otp);
/* PART 3 */
printf("-------PART 3-------\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);
char *res_vignere = d3(cypher_vignere, key_vignere);
printf("msg after: %s\n", res_vignere);
free(res_vignere);
free(cypher_vignere);
free(key_vignere);
return 0;
}
char*
e1(char *clearText, char key)
{
char *res = calloc(strlen(clearText), 1);
for (short k = 0 ; clearText[k] != '\0' ; ++k) {
if (clearText[k] >= 'a' && clearText[k] <= 'z') /* we encrypt a-z */
res[k] = (clearText[k] - 'a' + key) % 26 + 'a'; /* sign always + */
else
res[k] = clearText[k];
}
return res;
}
char*
d1(char *cypher, char key)
{
char *res = calloc(strlen(cypher), 1);
for (short k = 0; cypher[k] != '\0' ; ++k) {
if (cypher[k] >= 'a' && cypher[k] <= 'z') {/* we decrypt a-z */
/* in C, % is the rest, not modulo. We take care of the sign by
* always setting the signed bit to false :) */
res[k] = 'a' + REMOVE_SIGN((cypher[k] - 'a' - key)) % 26;
}
else
res[k] = cypher[k];
}
return res;
}
static inline char
gen1()
{
return rand() % SIZE_ALPH;
}
static inline char*
e2(char *cypher, char *pad)
{
char *res = calloc(SIZE_OTP, 1);
/* loop unwraping, might help cpu / compilator to use parallelisation */
for (char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) {
res[k] = cypher[k] ^ pad[k];
res[k + 1] = cypher[k + 1] ^ pad[k + 1];
res[k + 2] = cypher[k + 2] ^ pad[k + 2];
res[k + 3] = cypher[k + 3] ^ pad[k + 3];
res[k + 4] = cypher[k + 4] ^ pad[k + 4];
res[k + 5] = cypher[k + 5] ^ pad[k + 5];
res[k + 6] = cypher[k + 6] ^ pad[k + 6];
res[k + 7] = cypher[k + 7] ^ pad[k + 7];
}
res[SIZE_OTP - 1] = '\0';
return res;
}
static inline char*
d2(char *clearText, char *pad)
{
char *res = calloc(SIZE_OTP, 1);
/* loop unwraping, might help cpu / compilator to use parallelisation
* NOTE prob gcc / clag already does that as a default, now we sure */
for (char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) {
res[k] = clearText[k] ^ pad[k];
res[k + 1] = clearText[k + 1] ^ pad[k + 1];
res[k + 2] = clearText[k + 2] ^ pad[k + 2];
res[k + 3] = clearText[k + 3] ^ pad[k + 3];
res[k + 4] = clearText[k + 4] ^ pad[k + 4];
res[k + 5] = clearText[k + 5] ^ pad[k + 5];
res[k + 6] = clearText[k + 6] ^ pad[k + 6];
res[k + 7] = clearText[k + 7] ^ pad[k + 7];
}
res[SIZE_OTP - 1] = '\0';
return res;
}
static char*
gen2()
{
char *res = calloc(sizeof(char) * SIZE_OTP, 1);
for (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();
}
return res;
}
static inline char
genByte()
{
return rand() % 256;
}
static char*
e3(char *cypher, char *key)
{
char *res = calloc(sizeof(char) * SIZE_MSG, 1);
for(char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) {
/* we remove 2*a since cypher AND key contain 'a' */
res[k] = (cypher[k] + key[k] - 2 * 'a') % SIZE_ALPH + 'a';
res[k + 1] = (cypher[k + 1] + key[k + 1] - 2 * 'a') % SIZE_ALPH + 'a';
res[k + 2] = (cypher[k + 2] + key[k + 2] - 2 * 'a') % SIZE_ALPH + 'a';
res[k + 3] = (cypher[k + 3] + key[k + 3] - 2 * 'a') % SIZE_ALPH + 'a';
res[k + 4] = (cypher[k + 4] + key[k + 4] - 2 * 'a') % SIZE_ALPH + 'a';
res[k + 5] = (cypher[k + 5] + key[k + 5] - 2 * 'a') % SIZE_ALPH + 'a';
res[k + 6] = (cypher[k + 6] + key[k + 6] - 2 * 'a') % SIZE_ALPH + 'a';
res[k + 7] = (cypher[k + 7] + key[k + 7] - 2 * 'a') % SIZE_ALPH + 'a';
}
res[SIZE_MSG - 1] = '\0';
return res;
}
static char*
d3(char *cypher, char *key)
{
char *res = calloc(sizeof(char) * SIZE_MSG, 1);
for(char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) {
/* we DONT remove 'a' since cypher AND key contain 'a' */
res[k] = REMOVE_SIGN(cypher[k] - key[k]) % SIZE_ALPH + 'a';
res[k + 1] = REMOVE_SIGN(cypher[k + 1] - key[k + 1]) % SIZE_ALPH + 'a';
res[k + 2] = REMOVE_SIGN(cypher[k + 2] - key[k + 2]) % SIZE_ALPH + 'a';
res[k + 3] = REMOVE_SIGN(cypher[k + 3] - key[k + 3]) % SIZE_ALPH + 'a';
res[k + 4] = REMOVE_SIGN(cypher[k + 4] - key[k + 4]) % SIZE_ALPH + 'a';
res[k + 5] = REMOVE_SIGN(cypher[k + 5] - key[k + 5]) % SIZE_ALPH + 'a';
res[k + 6] = REMOVE_SIGN(cypher[k + 6] - key[k + 6]) % SIZE_ALPH + 'a';
res[k + 7] = REMOVE_SIGN(cypher[k + 7] - key[k + 7]) % SIZE_ALPH + 'a';
}
res[SIZE_MSG - 1] = '\0';
return res;
}
static inline char
genChar()
{
return rand() % SIZE_ALPH;
}
static char*
gen3()
{
char *res = calloc(sizeof(char) * SIZE_MSG, 1);
for (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[SIZE_MSG - 1] = '\0';
return res;
}