changing pc

This commit is contained in:
BigBlase 2023-09-27 17:07:47 -04:00
parent 06aabe1dbf
commit 742db2cf2a
6 changed files with 155 additions and 29 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
build/
massif*
dhat*
.vimspector.json

View file

@ -6,7 +6,7 @@
"configuration": { "configuration": {
"request": "launch", "request": "launch",
"program": "./build/main.o", "program": "./build/main.o",
"cwd": "/home/madeleine/classes/S9/crypto/", "cwd": "/home/violette/classes/S9/crypto/",
"externalConsole": true, "externalConsole": true,
"MIMode": "gdb" "MIMode": "gdb"
} }

View file

@ -3,9 +3,10 @@ OJB = $(SRC:.c=.o)
OUT = build OUT = build
CC = /usr/bin/gcc CC = /usr/bin/gcc
CFLAGS = -ansi -Wall -std=c99 CFLAGS = -ansi -Wall -std=c99 -O0
CFDEBUG = -ansi -Wall -fsanitize=address -g -std=c99 CFDEBUG = -ansi -Wall -fsanitize=address -g -std=c99
RM = /bin/rm -f $(OUT) RM = /bin/rm -f $(OUT)
PART = -D PART
%.o: %.c %.o: %.c
$(CC) -c $(CFLAGS) $(CC) -c $(CFLAGS)
@ -14,10 +15,10 @@ build:
$(CC) $(SRC) $(CFLAGS) -o $(OUT)/main.o $(CC) $(SRC) $(CFLAGS) -o $(OUT)/main.o
debug: debug:
$(CC) $(SRC) $(CFDEBUG) -o $(OUT)/main.o $(CC) $(SRC) $(PART)$(part) $(CFDEBUG) -o $(OUT)/main.o
clean: clean:
$(RM) $(OBJ) $(OUT)/main.o $(RM) $(OBJ) $(OUT)/main.o
run: $(SRC) run:
$(CC) $(SRC) $(CFLAGS) -o $(OUT)/main.o && ./build/main.o $(CC) $(SRC) $(PART)$(part) $(CFLAGS) -o $(OUT)/main.o && ./build/main.o

BIN
main.pdf Normal file

Binary file not shown.

View file

@ -26,8 +26,103 @@
#line(length: 100%) #line(length: 100%)
= Notes = Notes
Le code présenté est retrouvable sur ssh://bigblase.xyz:/srv/git/crypto1 Le code présenté est retrouvable sur ssh://bigblase.xyz:/srv/git/crypto1 ;
mot de passe FDS8EbKiDNoJh2QN
#set heading(numbering: "I) a) i)") #set heading(numbering: "I) a) i)")
k
= Chiffre de César = Chiffre de César
On lance le code si dessus 3 fois: (`make run part=1`) et on observe les sortie.
On obtient quelque chose comme:
#align(center)[
`
-------PART 1-------
key: 5
msg before: coding w/ freebsd style :)
cypher: htinsl b/ kwjjgxi xydqj :)
msg after: coding w/ freebsd style :)
key: 5
msg before: ceciestlemessageclairadechiffre
cypher: hjhnjxyqjrjxxfljhqfnwfijhmnkkwj
msg after: ceciestlemessageclairadechiffre
-------PART 1-------
key: 15
msg before: ceciestlemessageclairadechiffre
cypher: rtrxthiatbthhpvtrapxgpstrwxuugt
msg after: ceciestlemessageclairadechiffre
-------PART 1-------
key: 3
msg before: ceciestlemessageclairadechiffre
cypher: fhflhvwohphvvdjhfodludghfkliiuh
msg after: ceciestlemessageclairadechiffre
`
]
J'ai laissé le premier message pour monter que l'on peut quand m^eme envoyer des
symboles. Je l'ai enlevé après par soucis de place / redondance.
De leurs c^oté, Alice et Bob voient la clef key, le message msg et le cryptogramme
cypher.
==
Dans cette situation, Eve recevrait uniquement le cypher. On voit que quand on a
des symboles autre que des lettre, il devient facile de voir des motifs, des mots.
De plus, une analyse fréquentielle nous montrerais qu'il peut s'agir de texte
français. Comme Eve n'a pas la clef, elle pourrait alors éssayer de bruteforce
toutes les clefs du chiffrement de césar, ce que lui retournerait une solution
rapidement (il n'y a que 26 clef possible).
= OTP
==
Comme on veut coder un message en char (1 octet) sur 512 bits (64 octets), j'ai
pris la liberté de tester avec un message de cette taille, plutot que 32 octets.
Pour faire l'OTP, on fait un `XOR` entre le message et une chaine de 512 bits
aléatoire. En faisant ceci, on doit faire attention de bien terminer la string
avec un nullbyte, pour que notre programme fonctionne bien. Cela nous laisse donc
63 octets pour faire un message.
J'ai représenté dans mon programme les valeurs des cryptogrammes et messages en
clair ainsi que leur représentation en hexa, pour pouvoir comparer et montrer que
nous avons bien fait un `XOR` Cependant, le compilateur à décider l'allouer des int,
plutot que des char, et nous nous retrouvons avec des choses de la forme de FFFFFFD6
#align(center)[````
-------PART 2-------
key: <20>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>v<EFBFBD><76><EFBFBD><EFBFBD>_\<5C>jXS<58><53>9<EFBFBD>#<23><>0<EFBFBD>O|<7C>1<EFBFBD>T<EFBFBD>2<EFBFBD><32>d<EFBFBD><64><EFBFBD>BM<42>Ͽ<EFBFBD>wi<77>u<EFBFBD>z<EFBFBD>)
msg before: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd
msg as hex: 63 65 63 69 65 73 74 6C 65 6D 65 73 73 61 67 65 63 6C 61 69 72 61 64 65 63 6
6C 6F 6E 67 78 64
cypher: <20>W<EFBFBD><57><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϻ>;<3B> 42<34><32>X<EFBFBD>F<EFBFBD><46>Y<EFBFBD>)<29>X<EFBFBD>7h<37>B<EFBFBD><42>ܕ<EFBFBD><50>0.}<7D>ʺ<EFBFBD><CABA>l<EFBFBD>
cypher as hex: FFFFFF81 57 FFFFFFD6 FFFFFFBF FFFFFFBE FFFFFFAD FFFFFF99 FFFFFF91 13 FFFF
FCB FFFFFFC1 59 FFFFFF98 29 0E FFFFFFFA 58 FFFFFFC2 37 68 FFFFFFE7 42 FFFFFF9A FFFFFFF5
FFFFFA3 FFFFFFCA FFFFFFBA 18 1D FFFFFF92 1A FFFFFFD6 1D 6C FFFFFF8D
msg after: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd
-------PART 2-------
˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>py<EFBFBD>F)SG<53><47>HP<48>-<2D>D<EFBFBD><44><EFBFBD> <20><>ƱZ<C6B1>f<EFBFBD><66><EFBFBD><EFBFBD> <20>c<EFBFBD><63><EFBFBD>
msg before: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd
msg as hex: 63 65 63 69 65 73 74 6C 65 6D 65 73 73 61 67 65 63 6C 61 69 72 61 64 65 63 68 69 66 66 72 65 69 6C 63 6F 6D 70 6F 72 74 65 36 34 63 68 61 72 63 65 73 74 70 6C 75 74 6F 74 6C 6F 6E 67 78 64
cypher: <20><>9<EFBFBD>P]?"<22><>;#<23>J<EFBFBD><4A><EFBFBD>%<25><><EFBFBD><EFBFBD>j<EFBFBD><6A><EFBFBD><EFBFBD>(<28><><EFBFBD>ؠ<EFBFBD><D8A0>Ro<52>W<EFBFBD><57>l}~<7E>ʫ<EFBFBD><CAAB>z<EFBFBD><7A><EFBFBD>
cypher as hex: FFFFFFD6 19 FFFFFFAE 39 FFFFFF93 50 5D 3F 22 FFFFFFC8 FFFFFFE0 3B 23 FFFFFFB8 4A FFFFFF96 FFFFFFF3 FFFFFFE4 25 FFFFFFD5 FFFFFFD0 FFFFFFD8 7F FFFFFFAC 6A FFFFFF90 FFFFFFFB FFFFFFA0 FFFFFFD7 28 FFFFFF86 0F FFFFFFBA FFFFFFD3 FFFFFFD8 FFFFFFA0 FFFFFFA4 FFFFFF8F 52 6F FFFFFFE0 FFFFFF93 57 FFFFFFB6 17 FFFFFFF1 FFFFFFBB 6C 7D 7E FFFFFFBF FFFFFFCA FFFFFFAB FFFFFF93 FFFFFFF0 FFFFFFBF FFFFFFAA 7A FFFFFFF9 FFFFFFE1 17 01 FFFFFF92
msg after: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd
-------PART 2-------
key: <20><><EFBFBD>
ڍ-<2D>.)<29><<3C>W9~vs`<60>
uN<75><4E>5<EFBFBD>#
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m<EFBFBD><6D>Ek<45><6B><EFBFBD>J8<4A><38><EFBFBD> 6k<36>(w<>
msg before: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd
msg as hex: 63 65 63 69 65 73 74 6C 65 6D 65 73 73 61 67 65 63 6C 61 69 72 61 64 65 63 68 69 66 66 72 65 69 6C 63 6F 6D 70 6F 72 74 65 36 34 63 68 61 72 63 65 73 74 70 6C 75 74 6F 74 6C 6F 6E 67 78 64
cypher: <20>fûnoe<6F><65>@<40>]Z<>[<5B>4U<34>n&<26>S<EFBFBD>Fb<46><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>y<EFBFBD><79>&<26><><EFBFBD>t9L<39>ǝ<EFBFBD>OBuj<75>P
cypher as hex: FFFFFFB5 66 FFFFFFC3 FFFFFFBB 6E 6F 65 FFFFFFB6 FFFFFFE8 40 FFFFFF84 5D 5A FFFFFF80 5B FFFFFFE2 34 55 1F 1F 01 01 FFFFFFCB 6E 16 26 FFFFFFE3 FFFFFF83 53 FFFFFFB2 46 62 FFFFFFAF FFFFFFA0 FFFFFFB1 FFFFFFA2 FFFFFF90 FFFFFF80 FFFFFFDB 19 79 FFFFFFBC FFFFFFAF 26 03 FFFFFFB6 FFFFFFBE FFFFFFA1 74 39 4C FFFFFFF4 FFFFFFC7 FFFFFF9D FFFFFFFB 4F 42 75 6A 05 FFFFFFBE 50 13
msg after: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd
````]

View file

@ -3,8 +3,9 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
/* nuke sign from signed char */
#define REMOVE_SIGN(x) ( \ #define REMOVE_SIGN(x) ( \
(x) & ~ (1u << 8)\ ((x) & ~ (1u << 8))\
) )
#define SIZE_ALPH 26 #define SIZE_ALPH 26
@ -15,16 +16,17 @@ static char *e1(char *, char);
static char *d1(char *, char); static char *d1(char *, char);
static inline char gen1(); static inline char gen1();
static char *e2(char *, char*); static char *e2(char *, char *);
static char *d2(char *, char*); static char *d2(char *, char *);
static char *gen2(); static char *gen2();
static char *e3(char *, char*); static char *e3(char *, char *);
static char *d3(char *, char*); static char *d3(char *, char *);
static char *gen3(); static char *gen3();
static inline char genChar(); static inline char genChar();
static inline char genByte(); static inline char genByte();
static void printHex(char *);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -32,6 +34,7 @@ main(int argc, char *argv[])
srand(time(0)); /* init rand */ srand(time(0)); /* init rand */
#ifdef PART1
printf("-------PART 1-------\n"); printf("-------PART 1-------\n");
int key = gen1(); int key = gen1();
/* SYMBOLS */ /* SYMBOLS */
@ -58,7 +61,9 @@ main(int argc, char *argv[])
free(cypher32); free(cypher32);
free(res32); free(res32);
#endif
#ifdef PART2
/* PART 2 */ /* PART 2 */
printf("-------PART 2-------\n"); printf("-------PART 2-------\n");
char *key_otp = gen2(); char *key_otp = gen2();
@ -68,8 +73,12 @@ main(int argc, char *argv[])
"ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd"; "ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd";
printf("key: %s\n", key_otp); printf("key: %s\n", key_otp);
printf("msg before: %s\n", msg_otp); printf("msg before: %s\n", msg_otp);
printf("msg as hex: ");
printHex(msg_otp);
char *cypher_otp = e2(msg_otp, key_otp); char *cypher_otp = e2(msg_otp, key_otp);
printf("cypher: %s\n", cypher_otp); printf("cypher: %s\n", cypher_otp);
printf("cypher as hex: ");
printHex(cypher_otp);
char *res_otp = d2(cypher_otp, key_otp); char *res_otp = d2(cypher_otp, key_otp);
printf("msg after: %s\n", res_otp); printf("msg after: %s\n", res_otp);
@ -77,6 +86,9 @@ main(int argc, char *argv[])
free(res_otp); free(res_otp);
free(key_otp); free(key_otp);
#endif
#ifdef PART3
/* PART 3 */ /* PART 3 */
printf("-------PART 3-------\n"); printf("-------PART 3-------\n");
char *key_vignere = gen3(); char *key_vignere = gen3();
@ -92,6 +104,7 @@ main(int argc, char *argv[])
free(res_vignere); free(res_vignere);
free(cypher_vignere); free(cypher_vignere);
free(key_vignere); free(key_vignere);
#endif
return 0; return 0;
} }
@ -99,8 +112,8 @@ main(int argc, char *argv[])
char* char*
e1(char *clearText, char key) e1(char *clearText, char key)
{ {
char *res = calloc(strlen(clearText), 1); char *res = (char *)calloc(strlen(clearText) + 1, 1);
for (short k = 0 ; clearText[k] != '\0' ; ++k) { for (unsigned short k = 0 ; clearText[k] != '\0' ; ++k) {
if (clearText[k] >= 'a' && clearText[k] <= 'z') /* we encrypt a-z */ if (clearText[k] >= 'a' && clearText[k] <= 'z') /* we encrypt a-z */
res[k] = (clearText[k] - 'a' + key) % 26 + 'a'; /* sign always + */ res[k] = (clearText[k] - 'a' + key) % 26 + 'a'; /* sign always + */
else else
@ -112,12 +125,12 @@ e1(char *clearText, char key)
char* char*
d1(char *cypher, char key) d1(char *cypher, char key)
{ {
char *res = calloc(strlen(cypher), 1); char *res = (char *)calloc(strlen(cypher) + 1, 1);
for (short k = 0; cypher[k] != '\0' ; ++k) { for (unsigned short k = 0; cypher[k] != '\0' ; ++k) {
if (cypher[k] >= 'a' && cypher[k] <= 'z') {/* we decrypt a-z */ 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 /* in C, % is the rest, not modulo. We take care of the sign by
* always setting the signed bit to false :) */ * always setting the signed bit to false :) */
res[k] = 'a' + REMOVE_SIGN((cypher[k] - 'a' - key)) % 26; res[k] = 'a' + REMOVE_SIGN(cypher[k] - 'a' - key) % 26;
} }
else else
res[k] = cypher[k]; res[k] = cypher[k];
@ -134,9 +147,9 @@ gen1()
static inline char* static inline char*
e2(char *cypher, char *pad) e2(char *cypher, char *pad)
{ {
char *res = calloc(SIZE_OTP, 1); char *res = (char *)calloc(SIZE_OTP, 1);
/* loop unwraping, might help cpu / compilator to use parallelisation */ /* loop unwraping, might help cpu / compilator to use parallelisation */
for (char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) { for (unsigned char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) {
res[k] = cypher[k] ^ pad[k]; res[k] = cypher[k] ^ pad[k];
res[k + 1] = cypher[k + 1] ^ pad[k + 1]; res[k + 1] = cypher[k + 1] ^ pad[k + 1];
res[k + 2] = cypher[k + 2] ^ pad[k + 2]; res[k + 2] = cypher[k + 2] ^ pad[k + 2];
@ -153,10 +166,10 @@ e2(char *cypher, char *pad)
static inline char* static inline char*
d2(char *clearText, char *pad) d2(char *clearText, char *pad)
{ {
char *res = calloc(SIZE_OTP, 1); char *res = (char *)calloc(SIZE_OTP, 1);
/* loop unwraping, might help cpu / compilator to use parallelisation /* loop unwraping, might help cpu / compilator to use parallelisation
* NOTE prob gcc / clag already does that as a default, now we sure */ * NOTE prob gcc / clag already does that as a default, now we sure */
for (char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) { for (unsigned char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) {
res[k] = clearText[k] ^ pad[k]; res[k] = clearText[k] ^ pad[k];
res[k + 1] = clearText[k + 1] ^ pad[k + 1]; res[k + 1] = clearText[k + 1] ^ pad[k + 1];
res[k + 2] = clearText[k + 2] ^ pad[k + 2]; res[k + 2] = clearText[k + 2] ^ pad[k + 2];
@ -173,8 +186,8 @@ d2(char *clearText, char *pad)
static char* static char*
gen2() gen2()
{ {
char *res = calloc(sizeof(char) * SIZE_OTP, 1); char *res = (char *)calloc(sizeof(char) * SIZE_OTP, 1);
for (char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) { for (unsigned char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) {
res[k] = genByte(); res[k] = genByte();
res[k + 1] = genByte(); res[k + 1] = genByte();
res[k + 2] = genByte(); res[k + 2] = genByte();
@ -185,6 +198,8 @@ gen2()
res[k + 7] = genByte(); res[k + 7] = genByte();
} }
res[SIZE_OTP - 1] = '\0';
return res; return res;
} }
@ -197,8 +212,8 @@ genByte()
static char* static char*
e3(char *cypher, char *key) e3(char *cypher, char *key)
{ {
char *res = calloc(sizeof(char) * SIZE_MSG, 1); char *res = (char *)calloc(sizeof(char) * SIZE_MSG, 1);
for(char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) { for(unsigned char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) {
/* we remove 2*a since cypher AND key contain 'a' */ /* we remove 2*a since cypher AND key contain 'a' */
res[k] = (cypher[k] + key[k] - 2 * 'a') % SIZE_ALPH + '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 + 1] = (cypher[k + 1] + key[k + 1] - 2 * 'a') % SIZE_ALPH + 'a';
@ -217,8 +232,8 @@ e3(char *cypher, char *key)
static char* static char*
d3(char *cypher, char *key) d3(char *cypher, char *key)
{ {
char *res = calloc(sizeof(char) * SIZE_MSG, 1); char *res = (char *)calloc(sizeof(char) * SIZE_MSG, 1);
for(char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) { for(unsigned char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) {
/* we DONT remove 'a' since cypher AND key contain 'a' */ /* we DONT remove 'a' since cypher AND key contain 'a' */
res[k] = REMOVE_SIGN(cypher[k] - key[k]) % SIZE_ALPH + '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 + 1] = REMOVE_SIGN(cypher[k + 1] - key[k + 1]) % SIZE_ALPH + 'a';
@ -243,8 +258,8 @@ genChar()
static char* static char*
gen3() gen3()
{ {
char *res = calloc(sizeof(char) * SIZE_MSG, 1); char *res = (char *)calloc(sizeof(char) * SIZE_MSG, 1);
for (char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) { for (unsigned char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) {
res[k] = genChar() + 'a'; res[k] = genChar() + 'a';
res[k + 1] = genChar() + 'a'; res[k + 1] = genChar() + 'a';
res[k + 2] = genChar() + 'a'; res[k + 2] = genChar() + 'a';
@ -258,3 +273,14 @@ gen3()
res[SIZE_MSG - 1] = '\0'; res[SIZE_MSG - 1] = '\0';
return res; return res;
} }
static void
printHex(char *str)
{
short k = 0;
while (str[k] != '\0') {
printf("%02X ", str[k]);
++k;
}
printf("\n");
}