Title Description:
In the 6th century, French diplomat Blaise de Vigen è re designed a multi table cipher encryption algorithm - Vigen è re cipher. The encryption and decryption algorithm of Vigen è re password is easy to use and difficult to decipher. It was widely used by the Confederate army in the American Civil War.
In cryptography, we call the information to be encrypted plaintext, represented by M; The encrypted information is called ciphertext, represented by C; The key is a parameter, which is the data input in the algorithm of converting plaintext into ciphertext or ciphertext into plaintext, which is recorded as K. In the Vigen è re password, the key K is a letter string, k=k1k2... kn. When plaintext M=m1m2... mn, the obtained ciphertext C=c1c2... cn, where ci=mi ® ki, operation ® The rules are shown in the following table:
During the operation of Vigen è re encryption, you should pay attention to:
one ® The operation ignores the case of the letters involved in the operation and maintains the case of the letters in plaintext M;
2. When the length of plaintext M is greater than the length of key k, the key k is reused.
For example, when plaintext M=Helloworld and key k=abc, ciphertext C=Hfnlpyosnd.
input
The first line is a string representing the key k, with a length of no more than 100, which only contains upper and lower case letters.
The second line is a string, which represents the encrypted ciphertext, with a length of no more than 1000, including only uppercase and lowercase letters.
For 100% data, the length of the input key shall not exceed 100, and the length of the input ciphertext shall not exceed 1000, and all of them contain only English letters.
output
The output consists of 1 line and a string representing the plaintext corresponding to the input key and ciphertext.
sample input
CompleteVictory
Yvqgpxaimmklongnzfwpvxmniytm
sample output
Wherethereisawillthereisaway
Code
#include <bits/stdc++.h> using namespace std; string str,key; int main() { getline(cin,key); getline(cin,str); for (int i=0;i<key.length();i++) if (key[i] >= 'A' && key[i] <= 'Z') key[i] += 32;//Capital to lowercase for (int i=0;i<str.length();i++) { int temp = str[i] - key[keyIndex] + 'a';//int temp = str[i] - (key[keyIndex] - 'a'); Represents the difference if (str[i] >= 'a')//Judge case first { if (temp < 'a') temp += 26;//If it is lowercase and the value is less than a, add 26 } else { if (temp < 'A') temp += 26; } str[i] = char(temp); keyIndex++; if (keyIndex >= key.length()) keyIndex = 0;//If the Key index reaches the tail, start over } for (int i=0;i<str.length();i++) cout << str[i];//output return 0; }int keyIndex = 0;//(int keyIndex) the position of the currently used Key in the Key
The author tried this code and instantly "RE", because there is still a piece of code missing in this article, and we don't have it
For the position of the Key in the Key, int keyindex = 0 should be added between the 12th and 13th lines// (int keyIndex)
Reprogram the code
#include <bits/stdc++.h> using namespace std; string str,key; int main() { getline(cin,key); getline(cin,str); for (int i=0;i<key.length();i++) if (key[i] >= 'A' && key[i] <= 'Z') key[i] += 32;//Capital to lowercase int keyIndex = 0;//(int keyIndex) the position of the currently used Key in the Key for (int i=0;i<str.length();i++) { int temp = str[i] - key[keyIndex] + 'a';//int temp = str[i] - (key[keyIndex] - 'a'); Represents the difference if (str[i] >= 'a')//Judge case first { if (temp < 'a') temp += 26;//If it is lowercase and the value is less than a, add 26 } else { if (temp < 'A') temp += 26; } str[i] = char(temp); keyIndex++; if (keyIndex >= key.length()) keyIndex = 0;//If the Key index reaches the tail, start over } for (int i=0;i<str.length();i++) cout << str[i];//output return 0; }