#include #include #define BUFSIZE 5120 #define MAX_KEYLEN ( BUFSIZE / 100 ) const unsigned int num_letters = 'Z' - 'A' + 1; void encrypt(char* text, const char* key) { const char* c = key; while(*text) { // encrpypt *text = 'A' + ((*text - 'A') + (*c - 'A')) % num_letters ; // increase pointers ++c; if (*c == '\0') { c = key; // reset c at end of keyword } ++text; } } void decrypt(char* text, const char* key) { const char* c = key; while(*text) { // encrpypt int shift = (*text - 'A') - (*c - 'A'); if (shift < 0) shift += num_letters; *text = 'A' + shift ; // increase pointers ++c; if (*c == '\0') { c = key; // reset c at end of keyword } ++text; } } int main() { // read key char key[MAX_KEYLEN+1]; scanf("%s", key); char text[BUFSIZE + 1]; unsigned int len = 0; char c; // fill text-buffer while (len < BUFSIZE && (c = getchar()) != EOF) { if (c >= 'A' && c <= 'Z') { text[len++] = c; } } // terminate string with nullbyte text[len] = '\0'; encrypt(text,key); printf ("Geheimtext: %s\n\n", text); decrypt(text, key); printf("Wieder entschluesselt: %s\n", text); }