Answer the question
In order to leave comments, you need to log in
How to implement encryption with modulo 2 gamma cipher?
It is necessary to implement two ciphers (encryption and decryption with capital Russian letters): 1) encryption using the scaling cipher modulo n, and 2) encryption using the scaling cipher modulo 2.
It was possible to implement modulo n, the code is given below.
Tell me, how can I implement modulo 2 encryption based on my code?
The essence of these ciphers is as follows.
1) Modulo n. There is an alphabet from "A" to "Z" without the letter "E". Each character is assigned a number: A-0, B-1, ..., Z-31. Total 32 characters. Encryption occurs as follows: the i-th numbers of the characters of the original message and the gamma (key) are added and divided modulo n (n = 32). The result is numbers that are equal to some characters of the alphabet. This is the cipher. Decryption proceeds as follows: module n is added to the i-th number of the ciphertext and the i-th number of the gamma symbol is subtracted, and all this is still divided modulo n.
Example:
Text: BREAD
Key (gamma): LUNCH
Cipher: GMKE
2) Modulo 2. Here you already need to work with binary ASCII codes. The letter "A" has the code 192 (1100 0000), the letter "I" - 223 (1101 1111). Modulo 2, add the binary values of text and gamma. The ciphertext will be the message in the form of a decimal value. You can also output additionally as binary. Decryption goes like this: the binary value of the cipher and gamma are added modulo 2.
If the gamma is less than the original text, then it is complemented cyclically.
Example:
Text: B(194 = 1100 0010) O(206 = 1100 1110) B(194 = 1100 0010) A(192 = 1100 0000)
Key (Gamma): S(222 = 1101 1110) L(203 = 1100 1011 ) I(223 = 1101 1111) Yu(222 = 1101 1110)
Code: 28(0001 1100) 5(0000 0101) 29(0001 1101) 30(0001 1110)
Please help me implement the second code.
#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
using namespace std;
int main() {
//setlocale(LC_ALL, "Russian");
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int t, n, i, j, k, sum = 0;
string m;
cout << "Введите текст: ";
cin >> m;
string key;
cout << "Введите ключ (гамму): ";
cin >> key;
int mod = key.size();
j = 0;
for (i = key.size(); i < m.size(); i++) {
key += key[j%mod];
j++;
}
string ans = "";
for (i = 0; i < m.size(); i++) {
ans += (key[i] - 'А' + m[i] - 'А') % 32 + 'А';
}
cout << "Зашифрованный текст: " << ans << '\n';
string ans1 = "";
for (i = 0; i < ans.size(); i++) {
ans1 += (ans[i] - key[i] + 32) % 32 + 'А';
}
cout << "Расшифрованный текст: " << ans1 << '\n';
_getch();
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question