#P2636. 密码破解者
密码破解者
Description
According to intelligence, the Japanese use kinds of encryption methods:
- Rail Fence Cipher:
The so‑called rail fence cipher divides the plaintext into groups of characters, then concatenates the first character of each group, followed by the second character of each group, and so on, forming a seemingly irregular string. The most common case is the ‑rail rail fence cipher.
For example, plaintext: THERE IS A CIPHER
After removing spaces: THEREISACIPHER
Group by two: TH ER EI SA CI PH ER
Take the first character of each group: TEESCPE
Then the second character of each group: HRIAIHR
Concatenate them: TEESCPEHRIAIHR.
This yields the ciphertext we need.
There may also be more rails.
Note: If the plaintext length is not divisible by the number of rails, the leftover characters form a shorter group on their own. For example:
For THERE IS A CIPHER, the ‑rail grouping is: THE REI SAC IPH ER.
Take the first, second, and third characters in order (the last group only has the first two). The encrypted text is: TRSIE HEAPR EICH (remove spaces).
- Vigenère Cipher:
The Vigenère cipher first introduced the idea of a “key,” which is of great significance in cryptography.
In cryptography, the information to be encrypted is called the plaintext, denoted by ; the encrypted information is called the ciphertext, denoted by ; and the key is a parameter, i.e., the data input to the algorithm that transforms plaintext into ciphertext or ciphertext into plaintext, denoted by . In the Vigenère cipher, the key is a string of letters, . When the plaintext is , the resulting ciphertext is , where . The operation follows the table below:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A -A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
B -B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
C -C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
D -D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
E -E F G H I J K L M N O P Q R S T U V W X Y Z A B C D
F -F G H I J K L M N O P Q R S T U V W X Y Z A B C D E
G -G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
H -H I J K L M N O P Q R S T U V W X Y Z A B C D E F G
I -I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
J -J K L M N O P Q R S T U V W X Y Z A B C D E F G H I
K -K L M N O P Q R S T U V W X Y Z A B C D E F G H I J
L -L M N O P Q R S T U V W X Y Z A B C D E F G H I J K
M -M N O P Q R S T U V W X Y Z A B C D E F G H I J K L
N -N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
O -O P Q R S T U V W X Y Z A B C D E F G H I J K L M N
P -P Q R S T U V W X Y Z A B C D E F G H I J K L M N O
Q -Q R S T U V W X Y Z A B C D E F G H I J K L M N O P
R -R S T U V W X Y Z A B C D E F G H I J K L M N O P Q
S -S T U V W X Y Z A B C D E F G H I J K L M N O P Q R
T -T U V W X Y Z A B C D E F G H I J K L M N O P Q R S
U -U V W X Y Z A B C D E F G H I J K L M N O P Q R S T
V -V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
W -W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
X -X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
Y -Y Z A B C D E F G H I J K L M N O P Q R S T U V W X
Z -Z A B C D E F G H I J K L M N O P Q R S T U V W X Y
Notes for operating Vigenère encryption:
When the length of plaintext is greater than the length of key , repeat the key cyclically.
For example, plaintext , key , ciphertext .
- QWE Keyboard Code:
With the popularization of keyboards, corresponding keyboard codes also appeared.
This is a common keyboard. On the left letter area, there are three rows of letters:
QWERTYUIOP
ASDFGHJKL
ZXCVBNM
Starting from the first row and the first column, replace A with Q, B with W, …, Z with M, and so on.
For example, CODING is encrypted as EGROFU.
This is a simple encryption method today, but during World War II, when keyboards were not widespread, it was a big challenge.
Input Format
- The first line contains a positive integer , the number of encryption layers applied to the intercepted ciphertext.
- The second line contains a string , the ciphertext after encryption.
- The next lines (i.e., lines to ) each begin with a positive integer with , indicating the encryption method used for that layer.
- The methods are listed in order: the ‑th given method is the actual ‑th encryption layer in the process, where .
- If , the rail fence cipher was used; an integer follows, the number of rails used for encryption.
- If , the Vigenère cipher was used; a string follows, which is the key.
- If , the QWE keyboard code was used.
Output Format
Output one line: a string representing the plaintext after decrypting all layers of encryption.
2
YSLTRIQXSHTQTR
1 2
3
FULLSPEEDAHEAD
Hint
.
Translated by ChatGPT 5
京公网安备 11011102002149号