Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- /*Hello everyone,
- today I'm going to show you how to make a little encryption program using a for loop.
- Basically you take a (pre-defined) textstring and xor (exclusive or) it with a
- character (char) to encrypt/decrypt that string.
- xor works like this:
- Each and every piece of code will be translated in bits or ones and zeros ( 1 , 0)
- Xor does this: it will check whether the bits are equal or not (true or false);
- 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
- or both of them are equal to 1 the result is 0.
- e.g.
- 00111 a;
- 01010 b;
- 01101 deliver this */
- int main()
- {
- // lets start coding:
- std::string original = "Hello World! Don't forget to subscribe to mausy131 :D";
- std::string encrypted;
- std::string decrypted;
- std::string newOriginal;
- char key = 'x'; // we will use this to XOR our original string with
- /*optional
- cout << "Enter the sting you want to encrypt: \n";
- getline(cin, newOriginal)
- Place the for loop here and replace "original" with newOriginal
- Also place the rest of the stuff here.
- */
- //we will now make the algorithm
- for(int i = 0; i < original.size(); ++i){
- encrypted += original[i] ^ (int(key) + i) % 20; // here we will do our XOR operation..
- }
- cout << "Here is my secret that nobody may know: \n\n" << encrypted << endl;
- cout << "\nHere is the decrypted string: \n" << endl;
- for(int i = 0; i < encrypted.size(); ++i){
- decrypted += encrypted[i] ^ (int(key) + i) % 20; // here we will do our XOR operation to decrypt our string
- }
- cout << endl << decrypted << endl;
- cin.get();
- return 0;
- }
- /*
- This is a very weak protection, you should use Shifts and Rolls and such
- stuff but this is good for learning!
- I hope you liked it and learned something.
- Please subscribe to my channel :D
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement