Advertisement
Guest User

encrypt

a guest
Feb 8th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. /*Hello everyone,
  10. today I'm going to show you how to make a little encryption program using a for loop.
  11. Basically you take a (pre-defined) textstring and xor (exclusive or) it with a
  12. character (char) to encrypt/decrypt that string.
  13.  
  14. xor works like this:
  15.     Each and every piece of code will be translated in bits or ones and zeros ( 1 , 0)
  16.     Xor does this: it will check whether the bits are equal or not (true or false);
  17.     The result is 1 if either one of the two bits is 1, but not in the case that both are. There for, if neither
  18.     or both of them are equal to 1 the result is 0.
  19.  
  20.     e.g.
  21.  
  22.     00111    a;
  23.     01010    b;
  24.  
  25.     01101    deliver this */
  26.  
  27. int main()
  28. {
  29.     // lets start coding:
  30.     std::string original = "Hello World! Don't forget to subscribe to mausy131 :D";
  31.     std::string encrypted;
  32.     std::string decrypted;
  33.     std::string newOriginal;
  34.     char key = 'x'; // we will use this to XOR our original string with
  35.  
  36.     /*optional
  37.         cout << "Enter the sting you want to encrypt: \n";
  38.         getline(cin, newOriginal)
  39.        
  40.         Place the for loop here and replace "original" with newOriginal
  41.         Also place the rest of the stuff here.
  42.     */
  43.    
  44.    
  45.     //we will  now make the algorithm
  46.     for(int i = 0; i < original.size(); ++i){
  47.         encrypted += original[i] ^ (int(key) + i) % 20;  // here we will do our XOR operation..
  48.     }
  49.     cout << "Here is my secret that nobody may know: \n\n" << encrypted << endl;
  50.  
  51.     cout << "\nHere is the decrypted string: \n" << endl;
  52.  
  53.      for(int i = 0; i < encrypted.size(); ++i){
  54.         decrypted += encrypted[i] ^ (int(key) + i) % 20;  // here we will do our XOR operation to decrypt our string
  55.     }
  56.  
  57.     cout << endl << decrypted << endl;
  58.  
  59.     cin.get();
  60.     return 0;
  61. }
  62.  
  63. /*
  64.  
  65. This is a very weak protection, you should use Shifts and Rolls and such
  66. stuff but this is good for learning!
  67. I hope you liked it and learned something.
  68. Please subscribe to my channel :D
  69. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement