Skip to content

Instantly share code, notes, and snippets.

@orip
Last active July 17, 2020 02:06
Show Gist options
  • Save orip/0595f3bd3bfb959f25d03c4610d164f3 to your computer and use it in GitHub Desktop.
Save orip/0595f3bd3bfb959f25d03c4610d164f3 to your computer and use it in GitHub Desktop.
Using salsa20 to obfuscate strings, reference: https://stackoverflow.com/a/59929628/37020
// Relies on https://github.com/alexwebr/salsa20
// To compile and run: place it in a directory with salsa20.c and salsa20.h and run:
// % cc salsa20_obfuscator.c salsa20.c && ./a.out
// Then write strings in stdin. For example:
// % $ echo "Cancel" | ./a.out
// 'Cancel' -> [0x89, 0x88, 0x6a, 0xd1, 0x12, 0xc1]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "salsa20.h"
// hard-coded 128-bit key, can be anything
uint8_t k[32] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
// hard-coded nonce, can be anything
uint8_t n[8] = { 101, 102, 103, 104, 105, 106, 107, 108 };
int main() {
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
uint8_t encryption_buffer[1024];
while ((linelen = getline(&line, &linecap, stdin)) > 0) {
// strip trailing newline
if (line[linelen-1] == '\n') {
line[linelen-1] = '\0';
linelen--;
}
if (linelen == 0) {
continue;
}
if (linelen > sizeof(encryption_buffer)) {
printf("Skipping long word (len=%ld) '%s'\n", linelen, line);
continue;
}
memcpy(encryption_buffer, line, linelen);
s20_crypt(k, S20_KEYLEN_128, n, 0, encryption_buffer, linelen);
printf("'%s' -> [0x%x", line, encryption_buffer[0]);
for (size_t i=1; i < linelen; i++) {
printf(", 0x%x", encryption_buffer[i]);
}
printf("]\n");
}
if (line != NULL) {
free(line);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment