Docsity

Caesar and Beale Ciphers Encryption and Decryption, Study notes of Computer Science

Toggle sidebar
bg1
CS 1713, Caesar Cipher, Mon Apr 13 1998, Page 1 of 1
runner% cat caesar2.text
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.
runner% cat caesar.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char rotate(char c, int key)
{
int i = 0;
char s[] = "abcdefghijklmnopqrstuvwxyz";
if (!islower(c)) return(c);
while (i < 26) {
if (c == s[i]) return s[(i + key)%26];
i++;
}
}
void main(int argc, char *argv[])
{
int encrypt;
int key;
char c;
if (argc != 3) {
fprintf(stderr, "Usage: caesar (-d | -e) key\n");
exit(1);
}
if (strcmp(argv[1], "-e") == 0) encrypt = 1;
else if (strcmp(argv[1], "-d") == 0) encrypt = 0;
else {
fprintf(stderr, "Usage: caesar (-d | -e) key\n");
exit(1);
}
key = atoi(argv[2]);
if (!encrypt) key = (-key + 26)%26;
while ((c = getchar()) != EOF) {
if (islower(c))
c = rotate(c, key);
putchar(c);
}
}
runner% caesar -d
Usage: caesar (-d | -e) key
runner% caesar -e 3 <caesar2.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.
runner% caesar -e 3 <caesar2.text | caesar -d 3
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.
Note: rotate could have just contained:
char s[] = "abcdefghijklmnopqrstuvwxyz";
return s[(c - ’a’ + key)%26];
or even just
return (c - ’a’ + key)%26 + ’a’;
pf2

Partial preview of the text

Download Caesar and Beale Ciphers Encryption and Decryption and more Study notes Computer Science in PDF only on Docsity!

CS 1713, Caesar Cipher, Mon Apr 13 1998, Page 1 of 1

runner% cat caesar2.text

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.

runner% cat caesar.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

char rotate(char c, int key)

int i = 0;

char s[] = "abcdefghijklmnopqrstuvwxyz";

if (!islower(c)) return(c);

while (i < 26) {

if (c == s[i]) return s[(i + key)%26];

i++;

void main(int argc, char *argv[])

int encrypt;

int key;

char c;

if (argc != 3) {

fprintf(stderr, "Usage: caesar (-d | -e) key\n");

exit(1);

if (strcmp(argv[1], "-e") == 0) encrypt = 1;

else if (strcmp(argv[1], "-d") == 0) encrypt = 0;

else {

fprintf(stderr, "Usage: caesar (-d | -e) key\n");

exit(1);

key = atoi(argv[2]);

if (!encrypt) key = (-key + 26)%26;

while ((c = getchar()) != EOF) {

if (islower(c))

c = rotate(c, key);

putchar(c);

runner% caesar -d

Usage: caesar (-d | -e) key

runner% caesar -e 3 <caesar2.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.

runner% caesar -e 3 <caesar2.text | caesar -d 3

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.

Note: rotate could have just contained:

char s[] = "abcdefghijklmnopqrstuvwxyz";

return s[(c - ’a’ + key)%26];

or even just

return (c - ’a’ + key)%26 + ’a’;

CS 1713, Beale Cipher, Mon Apr 13 1998, Page 1 of 1

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.