Advertisement
obernardovieira

Caesar Cypher (Cryptography)

Apr 5th, 2016
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using std::string;
  5. using std::cout;
  6. using std::endl;
  7.  
  8. const string chars = "abcdefghijklmnopqrstuvwxyz !?.";
  9. const int n = 3;
  10.  
  11. string encode(string input)
  12. {
  13.     string output;
  14.     int pos;
  15.     for(int c = 0; c < input.length(); c++)
  16.     {
  17.         pos = chars.find( tolower(input[c]) );
  18.         if(pos != string::npos)
  19.         {
  20.             output.append( chars.substr( ((pos + n) % 30), 1) );
  21.         }
  22.     }
  23.     return output;
  24. }
  25.  
  26. string decode(string input)
  27. {
  28.     string output;
  29.     int pos;
  30.     for(int c = 0; c < input.length(); c++)
  31.     {
  32.         pos = chars.find( tolower(input[c]) );
  33.         if(pos != string::npos)
  34.         {
  35.             output.append( chars.substr( ((pos - n) % 30), 1) );
  36.         }
  37.     }
  38.     return output;
  39. }
  40.  
  41. int main()
  42. {
  43.     string message = "hello world";
  44.     string encoded;
  45.  
  46.     cout << "Original -> " << message << endl;
  47.     encoded = encode(message);
  48.     cout << "Encoded -> " << encoded << endl;
  49.     cout << "Decoded -> " << decode(encoded) << endl;
  50.  
  51.     return 1;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement