Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <iomanip>
- #include <bitset>
- typedef unsigned char Byte;
- typedef unsigned __int16 Block;
- static int SHIFT = 1;
- static int ENCRYPT_WAY = 1;
- static int HUMAN_OUTPUT = 1;
- // Contains data (2 Bytes)
- // Can be circle-shifted, XORed and compared
- struct CircularData
- {
- friend CircularData operator>>(CircularData l, int n);
- friend CircularData operator<<(CircularData l, int n);
- friend CircularData operator^(CircularData l, Block r);
- friend bool operator==(CircularData l, CircularData r);
- Block data = 0;
- };
- // Circle-shift
- CircularData operator>>(CircularData l, int n)
- {
- Block mask = 0x0;
- for (int i = 0; i < n; i++) {
- mask <<= 1;
- mask |= 1;
- }
- Block cycle = (l.data & mask) << (16 - n); // Save right N bits
- return CircularData({
- static_cast<Block>((l.data >> n) | cycle)
- }); // Default shift and append saved bits
- }
- CircularData operator<<(CircularData l, int n)
- {
- Block mask = 0x0;
- for (int i = 0; i < n; i++) {
- mask <<= 1;
- mask |= 1;
- }
- Block cycle = (l.data >> (16 - n)) & mask; // Save left N bits
- return CircularData({
- static_cast<Block>((l.data << n) | cycle)
- }); // Default shift and append saved bits
- }
- CircularData operator^(CircularData l, Block r)
- {
- return CircularData({
- static_cast<Block>(l.data ^ r)
- }); // Just XOR
- }
- bool operator==(CircularData l, CircularData r)
- {
- return l.data == r.data;
- }
- // Output for vector of CircularData
- std::ostream &operator<<(std::ostream &s, const std::vector<CircularData> &v)
- {
- for (auto &it : v) {
- Block data = it.data;
- s << static_cast<char>(data >> 8) << static_cast<char>(static_cast<Block>(data << 8) >> 8);
- }
- return s;
- }
- // Encrypt data
- void encrypt(CircularData *data, Block salt)
- {
- if (ENCRYPT_WAY == 0) {
- (*data) = (*data ^ salt) << SHIFT;
- } else {
- (*data) = (*data ^ salt) >> SHIFT;
- }
- }
- // Decrypt data
- // (var ^ v) ^ v == var
- void decrypt(CircularData *data, Block salt)
- {
- if (ENCRYPT_WAY == 0) {
- (*data) = (*data >> SHIFT) ^ salt;
- } else {
- (*data) = (*data << SHIFT) ^ salt;
- }
- }
- int main(int argc, char *argv[])
- {
- // For tests
- if (argc >= 3) {
- ENCRYPT_WAY = atoi(argv[1]);
- SHIFT = atoi(argv[2]);
- if (argc >= 4)
- HUMAN_OUTPUT = atoi(argv[3]);
- }
- if (HUMAN_OUTPUT) {
- std::cout << "Way : " << (ENCRYPT_WAY == 0 ? "<<" : ">>") << std::endl;
- std::cout << "Shift : " << SHIFT << std::endl;
- }
- std::string input; // String for encoding/decoding
- if (HUMAN_OUTPUT)
- std::cout << "Enter string: ";
- std::getline(std::cin, input);
- std::vector<CircularData> data; // Vector with 2 Byte elements
- for (auto &c : input) {
- static size_t index = 0; // Current index
- static Block block = 0; // 2 Bytes block
- if (index % 2 == 0) {
- block = static_cast<Byte>(c) << 8;
- if (index == input.size() - 1) { // Save last char (if input size is odd)
- data.push_back({block});
- }
- } else {
- block |= c;
- data.push_back({block}); // Calling constructor with initializer-list
- }
- index++;
- }
- std::string key; // Will be used only 4 symbols
- if (HUMAN_OUTPUT)
- std::cout << "Enter key : ";
- std::getline(std::cin, key);
- // Include shift size and way in srand argument
- Byte srandKeyPart = static_cast<Byte>(SHIFT) | static_cast<Byte>(ENCRYPT_WAY == 0) << 7;
- unsigned int srandKey = (srandKeyPart ^ key[0]) << 24; // Add first symbol
- for (size_t i = 1; i < 4; i++) { // Append 3 symbols to srandKey
- auto current = static_cast<Byte>((key.size() > i) ? key[i] : '\0');
- srandKey |= current << 8 * (3 - i);
- }
- srand(srandKey); // Apply key
- auto salt = static_cast<Block>(rand()); // Generate salt
- for (auto &d : data)
- encrypt(&d, salt);
- if (HUMAN_OUTPUT)
- std::cout << "Encrypted : ";
- std::cout << data << std::endl;
- for (auto &d : data)
- decrypt(&d, salt);
- if (HUMAN_OUTPUT)
- std::cout << "Decrypted : ";
- std::cout << data << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement