// ---------------------------------------------------------------------- // // Module: substitution_cipher.cc // Description: reads plaintext and outputs it after applying // a permutation of the characters. The permutation // is read from file "KEY". // Contributed by: Rene Peralta // Date: 2002 // Modified by: Michael Fischer // Date: 2005 // // ---------------------------------------------------------------------- #define BLANK 32 #define LOW_INDEX 32 #define HIGH_INDEX 126 #define ASCII_SIZE 256 #include #include #include #include #include using namespace std; int perm[ASCII_SIZE]; // permutation read from file "KEY" char encrypt(char c); void get_key(char* key_file_name); int main( int argc, char *argv[ ] ) { int i; char c; char *key_file_name; if ( argc==1 ) key_file_name = "KEY"; else key_file_name = argv[1]; get_key(key_file_name); while ( (i = getchar()) != EOF) { c = i; if ((int) c < LOW_INDEX || (int)c > HIGH_INDEX) cout << c; else cout << encrypt(c) ; } } char encrypt(char c) { return((char) perm[(int) c]); } void get_key(char* key_file_name) { ifstream key_file(key_file_name); int c_in, c_out; while (1) { key_file >> c_in; // this char goes to ... if (c_in == -1) break; // end of key condition key_file >> c_out; // this char. perm[c_in] = c_out; } }