Browse Source

ad atbash encryption

nas 3 years ago
parent
commit
44032b1560
3 changed files with 57 additions and 27 deletions
  1. 7
    5
      include/Crypt.h
  2. 5
    1
      main.cpp
  3. 45
    21
      src/Crypt.cpp

+ 7
- 5
include/Crypt.h View File

@@ -7,13 +7,15 @@ class Crypt {
7 7
  public :
8 8
   Crypt();
9 9
 
10
-  static std::string cesar( std::string sentance, int key );
11
-  static std::string vigenere( std::string sentance, std::string key );
10
+  static std::string cesar( std::string sentance, int key, char start = 'a', char end = 'z' );
11
+  static std::string vigenere( std::string sentance, std::string key, char start = 'a', char end = 'z' );
12
+  static std::string atbash( std::string sentane, char start = 'a', char end = 'z' );
12 13
 
13
-  static char replaceChar( char character, int key);
14
+  static char replaceChar( char character, int key, char start = 'a', char end = 'z' );
14 15
   static char loopInString( std::string str, int key );
15
-  static int alphabetIndex( char ch );
16
-
16
+  static int alphabetIndex( char ch, char start = 'a', char end = 'z' );
17
+  static bool isLetter( char ch, char start = 'a', char end = 'z' );
18
+  
17 19
  private :
18 20
   
19 21
 };

+ 5
- 1
main.cpp View File

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

+ 45
- 21
src/Crypt.cpp View File

@@ -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
 }

Loading…
Cancel
Save