// ---------------------------------------------------------------------- // // Module: perm_gen.cc // Description: constructs a random key for permutation of characters. // Allowed characters are upper case and punctuation. // Contributed by: Rene Peralta // Date: 2002 // Modified by: Michael Fischer // Date: 2005 // // ---------------------------------------------------------------------- #define ASCII_SIZE 256 #define LOW_INDEX 32 #define HIGH_INDEX 126 #include #include #include #include #include using namespace std; extern "C" void srand(unsigned int seed); extern "C" int rand(); void permute(int L[], int l, int h); // permutes the elements of L[l..h] int randint(int u); // returns a random integer in the range 0..u-1 ofstream out_file("KEY"); int main( int argc, char *argv[ ] ) { int key[ASCII_SIZE]; int plain[ASCII_SIZE]; int i,j; unsigned int seed; // get seed for random number generator . WARNING toy C generator cout << "enter seed " << endl; cin >> seed ; srand(seed); // fill the key array with the numbers to be permuted for (int i=LOW_INDEX; i <= HIGH_INDEX; i++) { plain[i] = i; key[i] = i; } // permute permute(key,LOW_INDEX,HIGH_INDEX); // output key for (int i = LOW_INDEX; i <= HIGH_INDEX; i++) out_file << plain[i] << " " << key[i] << endl; out_file << -1 << endl; // -1 is end-of-list } void permute(int L[], int l, int h) // permutes the elements of L[l..h] { if (l == h) return; // base case // exchange L[l] with a random element in L int hit = l + randint(h-l+1); int aux = L[hit]; L[hit] = L[l]; L[l] = aux; l++; permute(L,l,h); } int randint(int u) // in the range 0..u-1 { int r = rand(); if (r < 0) r = -r; // cout << "randint(" << u << ") returning " << r % u << endl; return (r % u); }