Skip to content

Instantly share code, notes, and snippets.

@TinfoilPancakes
Last active September 22, 2018 22:17
Show Gist options
  • Save TinfoilPancakes/e2e693fb2484b4882f08a5023508638e to your computer and use it in GitHub Desktop.
Save TinfoilPancakes/e2e693fb2484b4882f08a5023508638e to your computer and use it in GitHub Desktop.
uint8_t get_byte_offset(uint64_t seed) {
uint8_t* seed_ptr = (uint8_t*)&seed;
uint8_t hash = 0;
for (size_t i = 0; i < sizeof(seed); ++i) {
hash ^= seed_ptr[i];
}
std::bitset<8> hash_bitset(hash);
uint8_t offset = 0;
offset |= hash_bitset[3];
offset <<= 1;
offset |= hash_bitset[5];
offset <<= 1;
offset |= hash_bitset[7];
return offset;
}
std::basic_string<uint8_t>
encrypt(uint64_t seed, uint8_t* msg, size_t len) {
std::random_device rd;
std::mt19937_64 mt_generator(rd());
std::uniform_int_distribution<uint64_t> dist(0, UINT64_MAX);
std::basic_string<uint8_t> encrypted;
uint64_t current_key = seed;
for (size_t i = 0; i < len; ++i) {
uint8_t offset = get_byte_offset(current_key);
uint64_t padding = dist(mt_generator);
uint64_t extended_msg = msg[i];
uint64_t mask = 0xff;
extended_msg <<= offset * 8;
mask <<= offset * 8;
mask = ~mask;
padding &= mask;
padding |= extended_msg;
uint64_t e_char = padding ^ current_key;
encrypted.append((uint8_t*)&e_char, sizeof(e_char));
current_key = (current_key * msg[i]) ^ padding;
}
return encrypted;
}
std::basic_string<uint8_t>
decrypt(uint64_t seed, uint8_t* msg, size_t len) {
std::basic_string<uint8_t> decrypted;
uint64_t current_key = seed;
for (size_t i = 0; i < len; i += sizeof(seed)) {
auto offset = get_byte_offset(current_key);
uint64_t e_char = *((uint64_t*)(&msg[i]));
uint64_t padded = e_char ^ current_key;
uint8_t original = (padded >> (offset * 8)) & 0xFF;
decrypted.push_back(original);
current_key = (current_key * original) ^ padded;
}
return decrypted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment