Cryptography With Python 简明教程
Transposition Cipher
移位密码是一种密码算法,其中将明文中的字母顺序重新排列以形成密文。在此过程中,不包括实际的明文字母。
Transposition Cipher is a cryptographic algorithm where the order of alphabets in the plaintext is rearranged to form a cipher text. In this process, the actual plain text alphabets are not included.
Example
移动密码的一个简单示例是 columnar transposition cipher ,其中明文中的每个字符都以指定的字母宽度水平书写。密文是垂直书写的,这会创建一个完全不同的密文。
A simple example for a transposition cipher is columnar transposition cipher where each character in the plain text is written horizontally with specified alphabet width. The cipher is written vertically, which creates an entirely different cipher text.
考虑明文 hello world ,让我们应用如下所示的简单列移位技术
Consider the plain text hello world, and let us apply the simple columnar transposition technique as shown below

明文字符水平放置,并且密文以垂直格式创建为 : holewdlo lr. 。现在,接收者必须使用相同的表格将密文解密为明文。
The plain text characters are placed horizontally and the cipher text is created with vertical format as : holewdlo lr. Now, the receiver has to use the same table to decrypt the cipher text to plain text.
Code
以下程序代码演示了列移位技术的简单实现−
The following program code demonstrates the basic implementation of columnar transposition technique −
def split_len(seq, length):
return [seq[i:i + length] for i in range(0, len(seq), length)]
def encode(key, plaintext):
order = {
int(val): num for num, val in enumerate(key)
}
ciphertext = ''
for index in sorted(order.keys()):
for part in split_len(plaintext, len(key)):
try:ciphertext += part[order[index]]
except IndexError:
continue
return ciphertext
print(encode('3214', 'HELLO'))
Output
列移位技术的基本实现的程序代码提供了以下输出−
The program code for the basic implementation of columnar transposition technique gives the following output −

Note − 当执行移位技术时,密码分析人员观察到密码安全显著提高。他们还注意到,使用相同的移位密码重新加密密文会产生更好的安全性。
Note − Cryptanalysts observed a significant improvement in crypto security when transposition technique is performed. They also noted that re-encrypting the cipher text using same transposition cipher creates better security.