/* 5. Uebungsaufgabe: crypt_a.c * verschluesseln ueber die ASCII-Ordnung */ #include#define MAXLINE 100 /* maximale Laenge einer Eingabezeile */ /* getline: Zeile in s, Laenge der Zeile als Returnwert */ int getline(char s[],int lim) { int c, i=0; for(i=0; i< lim-1 && ( c=getchar()) !=EOF && c!='\n'; i++) s[i] = c; if ( c== '\n'){ s[i] =c; ++i; } s[i] = '\0'; return i; } int main( void ){ char instring[ MAXLINE ], outstring [ MAXLINE ], c; int length, index = 0, j, key; /* Eingabezeile einlesen */ length = getline(instring,MAXLINE); while (length > 0){ for (j=0; j < length-1; j++){ key = j % 26; /* gelesenes Zeichen ist ein Kleinbuchstabe */ if ((instring[j] <= 'z') && (instring[j] >= 'a')){ if ( (instring[j] + key +1 ) > 'z' ){ outstring[j] = instring[j] + key - ('z'-'a'); } else { outstring[j] = instring[j] + key +1; } } /* gelesenes Zeichen ist ein Grossbuchstabe */ else if ((instring[j] <= 'Z') && (instring[j] >= 'A')){ if ( (instring[j] + key +1 ) > 'Z' ){ outstring[j] = instring[j] + key - ('Z'-'A'); } else { outstring[j] = instring[j] + key +1; } } /* andere Zeichen ohne Verschluesselung uebernehmen */ else { outstring[j] = instring[j]; } } for (j=0; j < length-1; j++){ putchar(outstring[j]); } printf("\n"); length = getline(instring,MAXLINE); } return 0; } /* 5. Uebungsaufgabe: decrypt_a.c * verschluesseln ueber die ASCII-Ordnung */ #include #define MAXLINE 100 /* maximale Laenge einer Eingabezeile */ /* getline: Zeile in s, Laenge der Zeile als Returnwert */ int getline(char s[],int lim) { int c, i=0; for(i=0; i< lim-1 && ( c=getchar()) !=EOF && c!='\n'; i++) s[i] = c; if ( c== '\n'){ s[i] =c; ++i; } s[i] = '\0'; return i; } int main( void ){ char instring[ MAXLINE ], outstring [ MAXLINE ], c; int length, index = 0, j, key; /* Eingabezeile einlesen */ length = getline(instring,MAXLINE); while (length > 0 ) { for (j=0; j < length; j++){ key = j % 26; /* gelesenes Zeichen ist ein Kleinbuchstabe */ if ((instring[j] <= 'z') && (instring[j] >= 'a')){ if ( (instring[j] - key -1 ) < 'a' ){ outstring[j] = instring[j] - key + ('z'-'a'); } else { outstring[j] = instring[j] -key-1; } } /* gelesenes Zeichen ist ein Grossbuchstabe */ else if ((instring[j] <= 'Z') && (instring[j] >= 'A')){ if ( (instring[j] - key -1 ) < 'A' ){ outstring[j] = instring[j] - key + ('Z'-'A'); } else { outstring[j] = instring[j] -key-1; } } else { outstring[j] = instring[j]; } } for (j=0; j < length; j++){ putchar(outstring[j]); } length = getline(instring,MAXLINE); } return 0; } /* 6. Uebungsaufgabe: decrypt_a.c * verschluesseln ueber XOR-Verknuepfung */ #include /* Makro zum Codieren / Dekodieren (xor) */ #define XOR_KEY 0x1fa001f1 void crypt() { int ch; int i = 0; unsigned long int number = 0; while ( (ch = getchar() ) != EOF ) { if (i < 4) { i++; number |= ch; if ( i < 4) number <<= 8; } else { number = number ^ XOR_KEY; printf("%ld\n", number); ungetc(ch, stdin); number = 0; i = 0; }; } switch (i) { case 0: break; case 1: number <<= 8; case 2: number <<= 8; case 3: number = number ^ XOR_KEY; printf("%ld\n", number); break; default: fprintf(stderr, "FEHLER!!!\n"); exit(1); } } void main() { crypt(); } /* 6. Uebungsaufgabe: decrypt_a.c * verschluesseln ueber XOR-Verknuepfung */ #include /* Makro zum Codieren / Dekodieren (xor) */ #define XOR_KEY 0x1fa001f1 /*Makros zum Dekodieren :*/ #define FIRST 0xff000000 #define SECOND 0xff0000 #define THIRD 0xff00 #define FOURTH 0xff void decrypt() { unsigned long int number; char a,b,c,d; while ( scanf("%ld", &number) > 0 ) { number = number ^ XOR_KEY; a = ((number & FIRST)>>24) & 0xff; b = ((number & SECOND)>>16) & 0377; c = ((number & THIRD)>>8) & 0377; d = (number & FOURTH) & 0377; if (a>0) printf("%c", a); if (b>0) printf("%c", b); if (c>0) printf("%c", c); if (d>0) printf("%c", d); } } void main() { decrypt(); }
Martina Maier, 18. November 1998