Skip to content

Instantly share code, notes, and snippets.

@trapped
Last active May 11, 2021 19:06
Show Gist options
  • Save trapped/d3c674ae3755f2c5b771 to your computer and use it in GitHub Desktop.
Save trapped/d3c674ae3755f2c5b771 to your computer and use it in GitHub Desktop.
Scytale encryption (ancient greek technique) in C - https://en.wikipedia.org/wiki/Scytale
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void scytale(char* plain, int key, char* encrypted) {
int length = strlen(plain);
for(int i = 0; i < key; i++) {
for(int ii = i; ii < length; ii += key) {
encrypted[strlen(encrypted)] = plain[ii];
printf("col:%d row:%d current:'%c'\n",
i, ii, plain[ii]);
}
}
}
void main() {
char plain[] = "This is some text to encrypt";
char* encrypted = calloc(1, strlen(plain)+1);
char* decrypted = calloc(1, strlen(plain)+1);
int key = 4;
printf("Plain text: %s\n", plain);
scytale(plain, key, encrypted);
printf("Encrypted text: %s\n", encrypted);
if(!strlen(plain) % n) {
scytale(encrypted, strlen(plain) / key, decrypted);
printf("Decrypted: %s\n", decrypted);
if(!strcmp(plain, decrypted)) {
puts("Encryption/decryption successful!");
} else {
puts("Encryption/decryption failed!");
}
}
free(encrypted);
free(decrypted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment