Skip to content

Instantly share code, notes, and snippets.

@Acarus
Created May 9, 2017 17:04
Show Gist options
  • Save Acarus/bf128c7a795eac1b21d734e2acbde922 to your computer and use it in GitHub Desktop.
Save Acarus/bf128c7a795eac1b21d734e2acbde922 to your computer and use it in GitHub Desktop.
Шифрування через XOR
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
string encrypt(string text) {
char key = 15;
for (int i = 0; i < text.size(); ++i) {
text[i] = text[i] ^ key;
}
return text;
}
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
cin.tie(0);
srand(time(0));
#ifdef HOME
// freopen("/home/acarus/Desktop/io/input.txt", "r", stdin);
// freopen("/home/acarus/Desktop/io/output.txt", "w", stdout);
#endif
// 2^0, 2^1, 2^2
// 101 - 2^0*1 + 2^1*0 + 2^2*1 = 1*1 + 0 + 4*1 = 1 + 4 = 5
// &&:
// true && true = true
// true && false = false
// false && true = false
// false && false = false
// ||:
// true || true = true
// true || false = true
// false || true = true
// false || false = false
// 0 - false
// 1 - true
// &:
// 0 & 0 = 0
// 1 & 0 = 0
// 0 & 1 = 0
// 1 & 1 = 1
// |:
// 0 | 0 = 0
// 1 | 0 = 1
// 0 | 1 = 1
// 1 | 1 = 1
// ^ (правдою має бути тільки одне з них, виключне "І", XOR):
// 1 ^ 1 = 0
// 1 ^ 0 = 1
// 0 ^ 1 = 1
// 0 ^ 0 = 0
// 5 ^ 3 -> 101 ^ 011 (виконуєш операцію по елементно) = 110 = 2 + 4 = 6
// Починається магія :)
// 6 ^ 3 = 110 ^ 011 = 101 = 1 + 4 = 5
// Висновок: (5 ^ 3) ^ 3 = 5 => (x ^ y) ^ y = x
// ASCI таблиця символів:
// Дуже важлива операція в програмуванні:
// байт - 8 біт (0/1): 00000000 - 0, 11111111 - 255
cout << "Secret: " << encrypt("Kbf{}`") << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment