Skip to content

Instantly share code, notes, and snippets.

@Yepoleb
Last active November 16, 2018 22:35
Show Gist options
  • Save Yepoleb/b1ae6bfd9dadf4c5acd869df2152e59a to your computer and use it in GitHub Desktop.
Save Yepoleb/b1ae6bfd9dadf4c5acd869df2152e59a to your computer and use it in GitHub Desktop.
/* License: CC0-1.0 */
/* Usage: shitty_streamcipher <key> < plaintext > ciphertext */
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
const uint64_t RAND_A = 6364136223846793005;
const uint64_t RAND_C = 1442695040888963407;
// Linear congruential generator
uint64_t rand_next(uint64_t old)
{
return old * RAND_A + RAND_C;
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "Missing key argument\n");
return 1;
}
// Seed random generator from first argument
uint64_t rand_state = strtol(argv[1], NULL, 0);
if (errno == ERANGE) {
fprintf(stderr, "Key value is out of range\n");
return 1;
}
if (rand_state == 0) {
fprintf(stderr, "Failed to convert key\n");
return 1;
}
char *rand_bytes = (char*)&rand_state;
size_t rand_pos = 0;
rand_state = rand_next(rand_state);
int c_in = 0;
while ((c_in = getchar()) != EOF) {
int key = rand_bytes[rand_pos++];
int c_out = c_in ^ key;
putchar(c_out);
if (rand_state >= sizeof(rand_state)) {
rand_state = rand_next(rand_state);
rand_pos = 0;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment