Guest User

Learning Project #3: Caesar Cipher

a guest
Nov 14th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. const int SHIFT = 3;
  10.  
  11. string doEncrypt( string plainText );
  12. string doDecrypt( string encryptedText );
  13.  
  14. int main()
  15. {
  16.  
  17.     cout << "Learning Project #3: Caesar Chiper" << endl;
  18.     string textToEncrypt = "the quick brown fox jumps over the lazy dog";
  19.     cout << "Text to Encrypt: " << textToEncrypt << endl;
  20.     string textEncrypted = doEncrypt( textToEncrypt );
  21.     cout << "Encrypted Text: " << textEncrypted << endl;
  22.     string textDecrypted = doDecrypt(textEncrypted);
  23.     cout << "Decrypted Text: " << textDecrypted << endl;
  24.  
  25.     // Bad form, so sue me.
  26.     system("PAUSE");
  27.  
  28.     return 1;
  29. }
  30.  
  31. string doEncrypt( string plainText )
  32. {
  33.  
  34.     string encryptedText = "";
  35.     for( unsigned int i = 0; i < plainText.length(); i++ )
  36.     {
  37.  
  38.         int temp1 = int(plainText[i]) + SHIFT;
  39.         char temp2 = char(temp1);
  40.         encryptedText += temp2;
  41.     }
  42.  
  43.     return encryptedText;
  44. }
  45.  
  46. string doDecrypt( string plainText )
  47. {
  48.     string decryptedText = "";
  49.     for( unsigned int i = 0; i < plainText.length(); i++ )
  50.     {
  51.  
  52.         int temp1 = int(plainText[i]) - SHIFT;
  53.         char temp2 = char(temp1);
  54.         decryptedText += temp2;
  55.     }
  56.  
  57.     return decryptedText;
  58. }
Add Comment
Please, Sign In to add comment