Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- using namespace std;
- #define ECB_CYCLE 7
- void ecb_encrypt(const int* key, const char* msg, char*& enc)
- {
- if (enc == nullptr) {
- enc = new char[strlen(msg) + 1];
- }
- for (int i = 0; msg[i] != '\0'; i++) {
- enc[i] = (msg[i] + key[i % ECB_CYCLE] - 32) % 91 + 32;
- }
- }
- void ecv_decrypt(const int* key, const char* enc, char*& dec)
- {
- if (dec == nullptr) {
- dec = new char[strlen(enc) + 1];
- }
- for (int i = 0; enc[i] != '\0'; i++) {
- dec[i] = (enc[i] - key[i % ECB_CYCLE] + 59) % 91 + 32;
- }
- }
- int main()
- {
- const int key[] = { 8, 7, 3, 4, 9, 6, 2 };
- cout << "Please input plain text:";
- char str[128];
- cin.getline(str, 100);
- char *enc = nullptr, *dec = nullptr;
- ecb_encrypt(key, str, enc);
- cout << "Encrypt: " << enc << endl;
- ecv_decrypt(key, enc, dec);
- cout << "Decrypt: " << dec << endl;
- delete[] enc;
- delete[] dec;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement