// Widdows, Stephen // Assignment: HW:3 // File: encrypt.cc #include #include #include class BitFileIOs { int in_code, out_code; unsigned char in_mask, out_mask; unsigned int comforter; FILE *input, *output; public: BitFileIOs (char *argv1, char *argv2); int InputBit(); long InputBits(int n); void OutputBit(int bit); void OutputBits(unsigned long value, int n); void CloseOutput(); void CloseInput(); }; BitFileIOs::BitFileIOs(char *argv1, char *argv2) { if ((input = fopen (argv1, "rb")) == NULL) { cout << "Error in opening file " << argv1 << endl; exit(1); } if ((output = fopen (argv2, "wb")) == NULL) { cout << "Error in opening file " << argv2 << endl; } in_code = out_code = comforter = 0; in_mask = out_mask = 0x80; } int BitFileIOs::InputBit() { int value; if (in_mask == 0x80) { in_code = getc(input); if (in_code == EOF) { cout << "Out of data." << endl; return (-1); } if (!(comforter++ & 0x0fff)) cout << "."; } value = in_code & in_mask; in_mask >>= 1; if (in_mask == 0) in_mask = 0x80; return (value ? 1 : 0); } long BitFileIOs::InputBits(int n) { unsigned long temp_mask = 1L << (n-1); unsigned long return_value = 0; temp_mask = 1L << (n-1); while (temp_mask != 0) { if (in_mask == 0x80) { in_code = getc(input); if (in_code == EOF) { cout << "Out of data." << endl; return (-1); } if (!(comforter++ & 0x0fff)) cout << "."; } if (in_code & in_mask) return_value |= temp_mask; in_mask >>= 1; temp_mask >>= 1; if (in_mask == 0) in_mask = 0x80; } return (return_value); } void BitFileIOs::OutputBit(int bit) { if (bit) out_code |= out_mask; out_mask >>= 1; if (out_mask == 0) { putc (out_code, output); out_code = 0; out_mask = 0x80; if (!(comforter++ & 0x0fff)) cout << "."; } } void BitFileIOs::OutputBits(unsigned long value, int count) { unsigned long p; p = 1L << (count - 1); while (p != 0) { if (p & value) out_code |= out_mask; p >>= 1; if (out_mask == 0) { putc (out_code, output); out_mask = 0x80; out_code = 0; if (!(comforter++ & 0x0fff)) cout << "."; } } } void BitFileIOs::CloseOutput() { if (out_mask != 0x80) putc (out_code, output); fclose (output); } void encrypt(BitFileIOs f, int arr[]) { int bit = f.InputBit(), i; while (bit != -1) { for (i = 0; (i < 15) && (bit != -1); bit = f.InputBit()) { arr[i] = bit; ++i; } if (bit != -1) { f.OutputBit(arr[11]); f.OutputBit(arr[12]); for (int j = 0; (j < 11); ++j) f.OutputBit(arr[j]); f.OutputBit(arr[14]); f.OutputBit(arr[13]); } else for (i -= 1; i >= 0; --i) f.OutputBit(arr[i]); } } void decrypt(BitFileIOs f, int arr[]) { int bit = f.InputBit(), i; while (bit != -1) { for (i = 0; (i < 15) && (bit != -1); bit = f.InputBit()) { arr[i] = bit; ++i; } if (bit != -1) { for (int j = 2; (j < 13); ++j) f.OutputBit(arr[j]); f.OutputBit(arr[0]); f.OutputBit(arr[1]); f.OutputBit(arr[14]); f.OutputBit(arr[13]); } else for (i -= 1; i >= 0; --i) f.OutputBit(arr[i]); } } void main(int argc, char *argv[]) { int bit, arr[15]; char choice; if (argc < 3) { cout << "Usage:" << *argv << " infile outfile" << endl; exit (1); } argv++; BitFileIOs f(*argv, *(argv + 1)); cout << "Do you want to (e)ncrypt a file or (d)ecrypt one? " << endl; cin >> choice; switch (choice) { case 'e': encrypt(f, arr); break; case 'd': decrypt(f, arr); break; } }