Advertisement
Taraxacum

ECB Encrypt&Decrypt

Nov 21st, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. #define ECB_CYCLE 7
  6.  
  7. void ecb_encrypt(const int* key, const char* msg, char*& enc)
  8. {
  9.     if (enc == nullptr) {
  10.         enc = new char[strlen(msg) + 1];
  11.     }
  12.  
  13.     for (int i = 0; msg[i] != '\0'; i++) {
  14.         enc[i] = (msg[i] + key[i % ECB_CYCLE] - 32) % 91 + 32;
  15.     }
  16. }
  17.  
  18. void ecv_decrypt(const int* key, const char* enc, char*& dec)
  19. {
  20.     if (dec == nullptr) {
  21.         dec = new char[strlen(enc) + 1];
  22.     }
  23.  
  24.     for (int i = 0; enc[i] != '\0'; i++) {
  25.         dec[i] = (enc[i] - key[i % ECB_CYCLE] + 59) % 91 + 32;
  26.     }
  27. }
  28.  
  29. int main()
  30. {
  31.     const int key[] = { 8, 7, 3, 4, 9, 6, 2 };
  32.  
  33.     cout << "Please input plain text:";
  34.     char str[128];
  35.     cin.getline(str, 100);
  36.  
  37.     char *enc = nullptr, *dec = nullptr;
  38.     ecb_encrypt(key, str, enc);
  39.     cout << "Encrypt: " << enc << endl;
  40.  
  41.     ecv_decrypt(key, enc, dec);
  42.     cout << "Decrypt: " << dec << endl;
  43.  
  44.     delete[] enc;
  45.     delete[] dec;
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement