Browse Source

crypt core code

nas 2 years ago
parent
commit
e4f177f7ec
1 changed files with 23 additions and 14 deletions
  1. 23
    14
      src/Crypt.cpp

+ 23
- 14
src/Crypt.cpp View File

@@ -33,16 +33,25 @@ std::string Crypt::cesar( std::string sentances, int key, char start, char end )
33 33
 std::string Crypt::vigenere( std::string sentance, std::string key, char start, char end ){
34 34
   std::string result = "";
35 35
   int k = 0;
36
-  int count = 1;
37 36
 
38
-  for( char character : sentance ){
39
-    k = alphabetIndex( loopInString( key, count++ ), start, end);
40
-    result += Crypt::replaceChar( character, k, start, end ) ;
37
+  const char* c_sentance = sentance.c_str();
38
+
39
+  for( int count = 0 ;  count < (int)sentance.length(); count++ ){
40
+    char character = c_sentance[ count ] ;
41
+    k = alphabetIndex( loopInString( key, count+1 ), start, end);
42
+
43
+    if( k == 0 ){
44
+      result += character;
45
+    }else{
46
+      result += Crypt::replaceChar( character, k, start, end ) ;
47
+    }
41 48
   }
42
-  
49
+
43 50
   return result;
44 51
 }
45 52
 
53
+/**
54
+ **/
46 55
 std::string Crypt::atbash( std::string sentance, char start, char end ){
47 56
   std::string result = "";
48 57
   int k = 0;
@@ -56,27 +65,26 @@ std::string Crypt::atbash( std::string sentance, char start, char end ){
56 65
       result += character;
57 66
     }
58 67
   }
59
-  
68
+
60 69
   return result;
61 70
 }
62 71
 
63 72
 /** replaceChar
64 73
  @brief replace a character with the next character at key distance in the alphabetical order, and loop at z.
65
- @parameter char ch, the chasacter to replace
74
+ @parameter char ch, the character to replace
66 75
  @parameter int key, the distance to look at
67 76
  @return string, the replacement character
68 77
  **/
69 78
 char Crypt::replaceChar( char ch, int key, char start, char end ){
70 79
   ch = (char)std::tolower( ch );
71
-  int max = (int)end;
80
+  int max = (int)end ;
72 81
 
73 82
   if( !Crypt::isLetter( ch, start, end ) ){
74 83
     return ch;
75 84
   }
76
-  
77
-  if( (ch + key) > max ){
78
-    key -= (max - (int)ch);
79
-    ch = start;
85
+
86
+  if( ( (int)ch + key) > max ){
87
+    key -= (int)end - (int)start + 1;
80 88
   }
81 89
 
82 90
   return (char)((int)ch + key);
@@ -87,9 +95,10 @@ char Crypt::replaceChar( char ch, int key, char start, char end ){
87 95
  @return char
88 96
  **/
89 97
 char Crypt::loopInString( std::string str, int key ){
98
+  
90 99
   key -= ((int)str.length() < key )? str.length() : 0 ;
91 100
 
92
-  return str.c_str()[ key - 1 ];
101
+  return (Crypt::isLetter( str.c_str()[ key - 1 ] ))? str.c_str()[ key - 1 ] : 0 ;
93 102
 }
94 103
 
95 104
 /** alphabetIndex
@@ -113,7 +122,7 @@ int Crypt::alphabetIndex( char ch, char start, char end ){
113 122
  **/
114 123
 bool Crypt::isLetter( char ch, char start, char end ){
115 124
   ch = (char)std::tolower( ch );
116
-
125
+  
117 126
   if(( (int)ch < (int)start ) || ( (int)ch > (int)end )){
118 127
     return false;
119 128
   }

Loading…
Cancel
Save