|
@@ -11,11 +11,13 @@ Crypt::Crypt(){
|
11
|
11
|
@parameter key, the distance to shift
|
12
|
12
|
@return string, the encrypted sentance
|
13
|
13
|
**/
|
14
|
|
-std::string Crypt::cesar( std::string sentances, int key ){
|
|
14
|
+std::string Crypt::cesar( std::string sentances, int key, char start, char end ){
|
15
|
15
|
std::string result = "";
|
16
|
16
|
|
17
|
17
|
for( char character : sentances ){
|
18
|
|
- result += Crypt::replaceChar( character, key ) ;
|
|
18
|
+ result += Crypt::isLetter( character, start, end )?
|
|
19
|
+ Crypt::replaceChar( character, key, start, end ) :
|
|
20
|
+ character;
|
19
|
21
|
}
|
20
|
22
|
|
21
|
23
|
return result;
|
|
@@ -28,14 +30,31 @@ std::string Crypt::cesar( std::string sentances, int key ){
|
28
|
30
|
@see alphabet
|
29
|
31
|
@return string, the vigenere encrypted sentance
|
30
|
32
|
**/
|
31
|
|
-std::string Crypt::vigenere( std::string sentance, std::string key ){
|
|
33
|
+std::string Crypt::vigenere( std::string sentance, std::string key, char start, char end ){
|
32
|
34
|
std::string result = "";
|
33
|
35
|
int k = 0;
|
34
|
36
|
int count = 1;
|
35
|
37
|
|
36
|
38
|
for( char character : sentance ){
|
37
|
|
- k = alphabetIndex( loopInString( key, count++ ));
|
38
|
|
- result += Crypt::replaceChar( character, k ) ;
|
|
39
|
+ k = alphabetIndex( loopInString( key, count++ ), start, end);
|
|
40
|
+ result += Crypt::replaceChar( character, k, start, end ) ;
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ return result;
|
|
44
|
+}
|
|
45
|
+
|
|
46
|
+std::string Crypt::atbash( std::string sentance, char start, char end ){
|
|
47
|
+ std::string result = "";
|
|
48
|
+ int k = 0;
|
|
49
|
+ int max = (int)end ;
|
|
50
|
+
|
|
51
|
+ for( char character : sentance ){
|
|
52
|
+ if( Crypt::isLetter( character, start, end ) ){
|
|
53
|
+ k = Crypt::alphabetIndex( character, start, end ) ;
|
|
54
|
+ result += (char)( max - k + 1) ;
|
|
55
|
+ }else{
|
|
56
|
+ result += character;
|
|
57
|
+ }
|
39
|
58
|
}
|
40
|
59
|
|
41
|
60
|
return result;
|
|
@@ -47,20 +66,17 @@ std::string Crypt::vigenere( std::string sentance, std::string key ){
|
47
|
66
|
@parameter int key, the distance to look at
|
48
|
67
|
@return string, the replacement character
|
49
|
68
|
**/
|
50
|
|
-char Crypt::replaceChar( char ch, int key ){
|
|
69
|
+char Crypt::replaceChar( char ch, int key, char start, char end ){
|
51
|
70
|
ch = (char)std::tolower( ch );
|
52
|
|
- char a = 'a';
|
53
|
|
- char z = 'z';
|
54
|
|
- int max = (int)z;
|
55
|
|
- int min = (int)a;
|
|
71
|
+ int max = (int)end;
|
56
|
72
|
|
57
|
|
- if(( (int)ch < min ) || ( ch > max )){
|
|
73
|
+ if( !Crypt::isLetter( ch, start, end ) ){
|
58
|
74
|
return ch;
|
59
|
75
|
}
|
60
|
76
|
|
61
|
77
|
if( (ch + key) > max ){
|
62
|
78
|
key -= (max - (int)ch);
|
63
|
|
- ch = 'a';
|
|
79
|
+ ch = start;
|
64
|
80
|
}
|
65
|
81
|
|
66
|
82
|
return (char)((int)ch + key);
|
|
@@ -81,17 +97,25 @@ char Crypt::loopInString( std::string str, int key ){
|
81
|
97
|
@paramater char ch, the character to look at
|
82
|
98
|
@return int, ordered index in the alphabet
|
83
|
99
|
**/
|
84
|
|
-int Crypt::alphabetIndex( char ch ){
|
|
100
|
+int Crypt::alphabetIndex( char ch, char start, char end ){
|
85
|
101
|
ch = (char)std::tolower( ch );
|
|
102
|
+ int min = (int)start;
|
86
|
103
|
|
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;
|
|
104
|
+ if( Crypt::isLetter( ch, start, end ) ){
|
|
105
|
+ return int(ch) - min + 1;
|
94
|
106
|
}
|
95
|
107
|
|
96
|
|
- return int(ch) - min + 1;
|
|
108
|
+ return ch;
|
|
109
|
+}
|
|
110
|
+
|
|
111
|
+/** isLetter
|
|
112
|
+ @brief Return true is the character is a letter, which is the range between start and end
|
|
113
|
+ **/
|
|
114
|
+bool Crypt::isLetter( char ch, char start, char end ){
|
|
115
|
+ ch = (char)std::tolower( ch );
|
|
116
|
+
|
|
117
|
+ if(( (int)ch < (int)start ) || ( (int)ch > (int)end )){
|
|
118
|
+ return false;
|
|
119
|
+ }
|
|
120
|
+ return true;
|
97
|
121
|
}
|