非常推荐《密码编码学与网络安全--原理与实践(第八版)》这本书。
密码体质五元组:P,C,K,E,D
P,plaintext,明文空间
C,ciphertext,密文空间
K,key,密钥空间
E,encrypt,加密算法
D,decrypt,解密算法
单表代换
单表:英文26字母的顺序
代换:替换为别的字母并保证解密的唯一性
假如我们让加密方式为所有字母顺序移动3位
- import string
-
- string.ascii_lowercase
- 'abcdefghijklmnopqrstuvwxyz'
密钥 ,b=3
加密算法y=(x+b)mod26
- plaintext='flag{Caesar Cipher}'
- def encrypt(plaintext,b):
- ciphertext=''
- for each in plaintext:
- if each in string.ascii_lowercase:
- ciphertext+=string.ascii_lowercase[(string.ascii_lowercase.index(each)+3)%26]
- else:
- ciphertext+=each
- return ciphertext
-
- ret=encrypt(plaintext,3)
- # iodj{Cdhvdu Clskhu}
解密算法为x=(y-b)mod26
- ret='iodj{Cdhvdu Clskhu}'
- def decrypt(ciphertext,b):
- plaintext=''
- for each in ciphertext:
- if each in string.ascii_lowercase:
- plaintext+=string.ascii_lowercase[(string.ascii_lowercase.index(each)-3)%26]
- else:
- plaintext+=each
- return plaintext
-
- ret=decrypt(ret,3)
- # flag{Caesar Cipher}
密钥空间26
明文空间,密文空间均为26个英文字母(无法加密其他字符和数字)
当b=13时,加密算法也能解密,此时这种加密也被成为ROT13。
如果不使用英文标准表,而是从26字母的全排列(26!)中随机选择一个,敌手单纯爆破这个单表和密钥还是很难的。
不过单表代换密码有一个问题:无法掩盖英文字母的统计学特征(如字母e的出现次数更多)。
假如:e在明文中出现700次,e被加密为s,s在密文中也会出现700次。敌手可以通过分析单个字母,词组,词缀等方法直接跳过秘钥,直接尝试恢复明文。
对于加密算法y=(x+b)mod26而言,无法通过增大b的取值从而增加密码强度,也无法通过嵌套多层的加密方式增加密码强度。