UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

Captain Crunch Decoder Ring

Knowing that strings are really arrays allows us to access the elements of the array and change them directly.

For example, we can now write an implementation of the Captain Crunch secret decoder ring easily.

The Program

/********************************************* ** File: crunch.c ** Author: S. Bogar ** Date: 1/2/44 ** Section: 101 ** SSN: 123-45-6789 ** EMail: bogar@cs.umbc.edu * ** Description: ** An implementation of the Captain Crunch ** secret decoder ring. **********************************************/ #include <stdio.h> #include <ctype.h> #define SIZE 25 int main() { char c, str[SIZE] = "this is a secret message"; int index, i; /* Initialize our code */ char code[26] = {'t','f','h','x','q','j','e','m','u','p', 'i','d','c','k','v','b','a','o','l','r', 'z','w','g','n','s','y'} ; /* Print the original phrase */ printf ("Original phrase: %s\n", str); /* Encrypt */ for (i = 0 ; str[i] != '\0' ; i++) { if ( isalpha(str[i]) ) { c = tolower(str[i]) ; index = (int) c - 'a' ; str[i] = code[index] ; } } printf(" Encrypted: %s\n", str ) ; /* Decrypt */ for (i = 0 ; str[i] != '\0' ; i++) { if ( isalpha(str[i]) ) { c = tolower(str[i]) ; /* find matching character */ for (index = 0 ; index < 26 ; index++) { if ( code[index] == c ) { str[i] = (char) 'a' + index ; } } } } printf(" Decrypted: %s\n", str ) ; return 0; }

The Sample Run

Original phrase: this is a secret message Encrypted: rmul ul t lqhoqr cqllteq Decrypted: this is a secret message


CSEE | 201 | 201 S'02 | lectures | news | help

Last Modified - Thursday, 17-Jan-2002 13:52:13 EST