Browse Source

intial commit with cli demo

nas 3 years ago
commit
e7adcbd528
6 changed files with 213 additions and 0 deletions
  1. 35
    0
      Makefile
  2. 40
    0
      Makefile.old
  3. 21
    0
      include/Crypt.h
  4. 5
    0
      install.sh
  5. 15
    0
      main.cpp
  6. 97
    0
      src/Crypt.cpp

+ 35
- 0
Makefile View File

1
+CC=g++
2
+LDFLAGS=
3
+CFLAGS=-Wall -Wextra -O -std=c++17
4
+
5
+STATE=debug
6
+
7
+SRC=src/
8
+INCLUDE=include/
9
+OBJ=object/
10
+BIN=bin/$(STATE)/
11
+
12
+all: crypt
13
+
14
+run: crypt
15
+	$(BIN)reits
16
+
17
+rerun: clean run
18
+
19
+remake: clean reits
20
+
21
+crypt: $(OBJ)main.o $(OBJ)Crypt.o
22
+	@echo "\nCompilation de \033[0;36m $@ \033[0m"
23
+	$(CC) -o $(BIN)$@ $^ $(LDFLAGS)
24
+
25
+$(OBJ)main.o: main.cpp
26
+	@echo "\nCompilation de \033[0;36m $@ \033[0m"
27
+	$(CC) -o $@ -c $^ $(CFLAGS)
28
+
29
+$(OBJ)%.o: $(SRC)%.cpp $(INCLUDE)%.h
30
+	@echo "\nCompilation de \033[0;36m $@ \033[0m"
31
+	@$(CC) -o $@ -c $< $(CFLAGS)
32
+
33
+clean:
34
+	@echo "Nettoyage des objets dans \033[0;34m$(OBJ)\033[0m"
35
+	rm main.o $(OBJ)*

+ 40
- 0
Makefile.old View File

1
+CC=g++
2
+LDFLAGS=
3
+CFLAGS=-Wall -Wextra -O -std=c++17
4
+
5
+STATE=debug
6
+
7
+SRC=src/
8
+INCLUDE=include/
9
+OBJ=object/
10
+BIN=bin/$(STATE)/
11
+
12
+all: reits
13
+
14
+run: reits
15
+	$(BIN)reits
16
+
17
+reits: main.o $(OBJ)Game.o $(OBJ)Scene.o $(OBJ)Interface.o
18
+	@echo "\nCompilation de \033[0;36m $@ \033[0m"
19
+	$(CC) -o $(BIN)$@ $(OBJ)$^ $(LDFLAGS)
20
+
21
+main.o: main.cpp $(INCLUDE)Game.h
22
+	@echo "\nCompilation de \033[0;36m $@ \033[0m"
23
+	$(CC) -o $(OBJ)$@ -c $^ $(CFLAGS)
24
+
25
+Game.o: $(SRC)Game.cpp $(INCLUDE)Game.h $(INCLUDE)Scene.h
26
+	@echo "\nCompilation de \033[0;36m $@ \033[0m"
27
+	@$(CC) -o $(OBJ)$@ -c $< $(CFLAGS)
28
+
29
+Scene.o: $(SRC)Scene.cpp $(INCLUDE)Scene.h $(INCLUDE)Interface.h
30
+	@echo "\nCompilation de \033[0;36m $@ \033[0m"
31
+	@$(CC) -o $(OBJ)$@ -c $< $(CFLAGS)
32
+
33
+Interface.o: $(SRC)Interface.cpp $(INCLUDE)Interface.h
34
+	@echo "\nCompilation de \033[0;36m $@ \033[0m"
35
+	@$(CC) -o $(OBJ)$@ -c $< $(CFLAGS)
36
+
37
+clean:
38
+	echo "Nettoyage des objets"
39
+	rm  $(OBJ)*
40
+

+ 21
- 0
include/Crypt.h View File

1
+#ifndef Crypt_H
2
+#define Crypt_H
3
+
4
+#include <iostream>
5
+
6
+class Crypt {
7
+ public :
8
+  Crypt();
9
+
10
+  static std::string cesar( std::string sentance, int key );
11
+  static std::string vigenere( std::string sentance, std::string key );
12
+
13
+  static char replaceChar( char character, int key);
14
+  static char loopInString( std::string str, int key );
15
+  static int alphabetIndex( char ch );
16
+
17
+ private :
18
+  
19
+};
20
+
21
+#endif

+ 5
- 0
install.sh View File

1
+#!/bin/sh
2
+
3
+echo "Installation des dépendances"
4
+
5
+apt install rapidjson-dev

+ 15
- 0
main.cpp View File

1
+#include <iostream>
2
+#include "include/Crypt.h"
3
+
4
+/**
5
+ @brief Take a string on command line argument to encrypt it. For vigenere encnyption, the text is encrypted with itself as a key
6
+ **/
7
+int main( int argc, char* argv[]  )
8
+{
9
+  std::string txt = argv[1] ;
10
+  std::cout << "to encrypt :" << txt << std::endl;
11
+  std::cout << "code cesar :" <<  Crypt::cesar(txt, 3 ) << std::endl ;
12
+  std::cout << "code vigenere :" << Crypt::vigenere(txt, txt ) << std::endl ;
13
+
14
+  return 0;
15
+}

+ 97
- 0
src/Crypt.cpp View File

1
+#include "../include/Crypt.h"
2
+
3
+Crypt::Crypt(){
4
+
5
+}
6
+
7
+/** simple
8
+ @brief Simple Crypt replacement encrypted. It will replace each character from a sentances with the character at a defined distance in alphabetical.
9
+ @see replaceChar
10
+ @parameter string sentances, the sentance to encrypted
11
+ @parameter key, the distance to shift
12
+ @return string, the encrypted sentance
13
+ **/
14
+std::string Crypt::cesar( std::string sentances, int key ){
15
+  std::string result = "";
16
+
17
+  for( char character : sentances ){
18
+    result += Crypt::replaceChar( character, key ) ;
19
+  }
20
+  
21
+  return result;
22
+}
23
+
24
+/** vigenere
25
+ @brief A lot mof stronger than the previous, even it's still a weak encryption algorythm. This is the same principle, however the key is modified for each character. The most elegant way to defined this dequance is with another sentance. Each character of the key sentance will defined the "a", or the key if cast in an integer
26
+ @see loopInString
27
+ @see replaceChar
28
+ @see alphabet
29
+ @return string, the vigenere encrypted sentance
30
+ **/
31
+std::string Crypt::vigenere( std::string sentance, std::string key ){
32
+  std::string result = "";
33
+  int k = 0;
34
+  int count = 1;
35
+
36
+  for( char character : sentance ){
37
+    k = alphabetIndex( loopInString( key, count++ ));
38
+    result += Crypt::replaceChar( character, k ) ;
39
+  }
40
+  
41
+  return result;
42
+}
43
+
44
+/** replaceChar
45
+ @brief replace a character with the next character at key distance in the alphabetical order, and loop at z.
46
+ @parameter char ch, the chasacter to replace
47
+ @parameter int key, the distance to look at
48
+ @return string, the replacement character
49
+ **/
50
+char Crypt::replaceChar( char ch, int key ){
51
+  ch = (char)std::tolower( ch );
52
+  char a = 'a';
53
+  char z = 'z';
54
+  int max = (int)z;
55
+  int min = (int)a;
56
+
57
+  if(( (int)ch < min ) || ( ch > max )){
58
+    return ch;
59
+  }
60
+  
61
+  if( (ch + key) > max ){
62
+    key -= (max - (int)ch);
63
+    ch = 'a';
64
+  }
65
+
66
+  return (char)((int)ch + key);
67
+}
68
+
69
+/** loopInString
70
+ @brief return a character of the sentance, defined by the integer and the character place in the sentance. However, if the integer is longer than the sentance, it will loop back at the beginning.
71
+ @return char
72
+ **/
73
+char Crypt::loopInString( std::string str, int key ){
74
+  key -= ((int)str.length() < key )? str.length() : 0 ;
75
+
76
+  return str.c_str()[ key - 1 ];
77
+}
78
+
79
+/** alphabetIndex
80
+ @brief return the number of the character in the alphabet order. Capital letter are lowered, other character are return untouched.
81
+ @paramater char ch, the character to look at
82
+ @return int, ordered index in the alphabet
83
+ **/
84
+int Crypt::alphabetIndex( char ch ){
85
+  ch = (char)std::tolower( ch );
86
+
87
+  char a = 'a';
88
+  char z = 'z';
89
+  int max = (int)z;
90
+  int min = (int)a;
91
+
92
+  if(( (int)ch < min ) || ( (int)ch > max )){
93
+    return ch;
94
+  }
95
+  
96
+  return int(ch) - min + 1;
97
+}

Loading…
Cancel
Save