|
@@ -0,0 +1,97 @@
|
|
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
|
+}
|