

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


runner% cat beale.text (same as the text for Caesar cipher) runner% cat key.text dddddddddddddddddddddddddddddddddddddddddddddd dddddddddddddddddddddddddddddddddddddddddddddd dddddddddddddddddddddddddddddddddddddddddddddd (all d letters) runner% cat beale.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> char rotate(char c, int key); void main(int argc, char *argv[]) { FILE keyfile; int direction = 1; char c, key_ch; if (argc != 2) { fprintf(stderr, "Usage: beale (-d | -e)\n"); exit(1); } if (strcmp(argv[1], "-e") == 0) direction = 1; else if (strcmp(argv[1], "-d") == 0) direction = -1; else { fprintf(stderr, "Usage: beale (-d | -e)\n"); exit(1); } keyfile = fopen("key.text", "r"); if (keyfile == NULL) { printf("Couldn’t open file\n"); exit(1); } while ((c = getchar()) != EOF) { if (islower(c)) { while (!islower(key_ch = fgetc(keyfile))) ; c = rotate(c, ((direction(key_ch - ’a’))+26)%26); } putchar(c); } } char rotate(char c, int key) { return (c - ’a’ + key)%26 + ’a’; } ENCRYPT beale.text, using the simple file of all d characters: runner% beale -e <beale.text l uhwxuqhg, dqg vdz xqghu wkh vxq, wkdw wkh udfh lv qrw wr wkh vzliw, qru wkh edwwoh wr wkh vwurqj, qhlwkhu bhw euhdg wr wkh zlvh, qru bhw ulfkhv wr phq ri xqghuvwdqglqj, qru bhw idyrxu wr phq ri vnloo; exw wlph dqg fkdqfh kdsshqhwk wr wkhp doo. ENCRYPT and then DECRYPT: runner% beale -e <beale.text | beale -d i returned, and saw under the sun, that the race is not to the swift, nor the battle to the strong, neither yet bread to the wise, nor yet riches to men of understanding, nor yet favour to men of skill; but time and chance happeneth to them all. Now use a different file as the key, not all the same character: runner% cat key.text A Golden Age, whether of art or music or science or peace or plenty, is out of reach of our economic and governmental techniques. something may be done by accident, as it has from time to time in the past, but not by deliberate intent. At this very moment enormous numbers of intelligent men and women of good will are trying to build a better world. But problems are born faster than they can be solved. ENCRYPT beale.text, using the the more complex file: runner% beale -e < beale.text w chxhxrak, egk wrk znuxf kty kcp, hysv blr teqv xw nqx hf isi fpgnl, bik hmv favazj hi klg ggfavi, nrlzvzv prf fexao ms vor eymi, fgf kim yqpnqs rp qhb bj vldgtaweawifo, gvr qjk tmowgv mc fmz sn fdppa; bmm ucfr oge akeykf lrpiivrml gh taxt idg. ENCRYPT and then DECRYPT: runner% beale -e <beale.text | beale -d i returned, and saw under the sun, that the race is not to the swift, nor the battle to the strong, neither yet bread to the wise, nor yet riches to men of understanding, nor yet favour to men of skill; but time and chance happeneth to them all.