fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. unsigned int data = 0x00000000;
  7. char mask = 0b00001111; // mask for 4 bits
  8. char c1 = 0, c2 = 0, c3 = 0, c4 = 0; // characters for encryption
  9. char d1 = 0, d2 = 0, d3 = 0, d4 = 0; // characters for decryption
  10.  
  11. cout << "Please input 4 characters:" << endl;
  12. cin >> c1 >> c2 >> c3 >> c4;
  13. cout << endl;
  14.  
  15. data =
  16. (static_cast<unsigned int>(mask << 4 & c4) << 24) | (static_cast<unsigned int>(mask & c4) << 20) |
  17. (static_cast<unsigned int>(mask << 4 & c3) << 20) | (static_cast<unsigned int>(mask & c3) << 16) |
  18. (static_cast<unsigned int>(mask << 4 & c2) << 8) | (static_cast<unsigned int>(mask & c2) << 4) |
  19. (static_cast<unsigned int>(mask << 4 & c1) << 4) | (static_cast<unsigned int>(mask & c1));
  20.  
  21. cout << "The encrypted result is (hex): 0x" << hex << data << endl;
  22. cout << endl;
  23.  
  24.  
  25.  
  26.  
  27.  
  28. cout << endl;
  29. cout << "The decrypted characters are: " << d1 << ' ' << d2 << ' ' << d3 << ' ' << d4 << endl;
  30.  
  31. cout << endl;
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5272KB
stdin
8
stdout
Please input 4 characters:

The encrypted result is (hex): 0x308


The decrypted characters are: