Cryptography With Python 简明教程

Cryptography with Python - ROT13 Algorithm

到目前为止,您已经了解了反向密码和凯撒密码算法。现在,我们来讨论 ROT13 算法及其实现。

Till now, you have learnt about reverse cipher and Caesar cipher algorithms. Now, let us discuss the ROT13 algorithm and its implementation.

Explanation of ROT13 Algorithm

ROT13 密码指的是缩写 Rotate by 13 places 。它凯撒密码的一个特例,其中移位始终为 13。每封信都移位 13 位,以加密或解密消息。

ROT13 cipher refers to the abbreviated form Rotate by 13 places. It is a special case of Caesar Cipher in which shift is always 13. Every letter is shifted by 13 places to encrypt or decrypt the message.

Example

下图形象地说明了 ROT13 算法流程 −

The following diagram explains the ROT13 algorithm process pictorially −

rot

Program Code

ROT13算法的程序实现如下 -

The program implementation of ROT13 algorithm is as follows −

from string import maketrans

rot13trans = maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
   'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm')

# Function to translate plain text
def rot13(text):
   return text.translate(rot13trans)
def main():
   txt = "ROT13 Algorithm"
   print rot13(txt)

if __name__ == "__main__":
   main()

您可以在下图中看到 ROT13 输出 -

You can see the ROT13 output as shown in the following image −

rot13

Drawback

ROT13算法使用13位移。因此,以相反方式位移字符非常容易解密密文。

The ROT13 algorithm uses 13 shifts. Therefore, it is very easy to shift the characters in the reverse manner to decrypt the cipher text.

Analysis of ROT13 Algorithm

ROT13加密算法被认为是凯撒加密的特殊情况。这并不是一种非常安全的算法,可以通过频率分析或仅仅尝试可能的 25 个密钥轻松破解,而 ROT13 可以通过位移 13 位进行破解。因此,它没有任何实际用途。

ROT13 cipher algorithm is considered as special case of Caesar Cipher. It is not a very secure algorithm and can be broken easily with frequency analysis or by just trying possible 25 keys whereas ROT13 can be broken by shifting 13 places. Therefore, it does not include any practical use.