Last active
May 11, 2021 19:06
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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