# 字符串转单字符列表,再转为数字序列便于计算 def stoc(str): ch_list = [] for i in str: ch_list.append(i) return [ord(i)-65 for i in ch_list]
# Int to Chr # 将数字序列还原成大写字母序列 def itoc(list_int): A = [chr(i+65) for i in list_int] ch = "" for i in A: ch += i return ch
# 计算密文序列 def Encrypt(Message,k,n): print('>>>使用加法密码算法进行加密(k={}, n={})'.format(k,n)) return itoc([i+k%n for i in stoc(Message)])
# 计算明文序列 def Decrypt(Ciphertext,k,n): # 解密方式1:通过构建密码表进行查表解密 # 解密方式2:通过加密逆运算计算明文 DecryptionType = 2 if(DecryptionType == 1): print('>>>构建密码表:') A = [i for i in range(0,n)] print('>>>明文字母表:{}'.format(itoc(A))) B = Encrypt(itoc(A),k,n) print('>>>密文字母表:{}'.format(B)) CiphertextTables = dict(zip(B,A)) print('>>>构建密码表进行查表解密') return itoc([CiphertextTables[i] for i in Ciphertext]) else: print('>>>通过加密逆运算进行解密(k的逆元为:{})'.format(-k)) return itoc([c-k+n %n for c in stoc(Ciphertext)])
if __name__=='__main__': # 当前仅支持大写字母串 A = ('ABCDEF') print('输入的明文字符串为:{}'.format(A)) B = Encrypt(A,7,26) print('加密后的密文:{}'.format(B)) C = Decrypt(B,7,26) print('解密后的明文:{}'.format(C))
# 字符串转单字符列表,再转为数字序列便于计算 def stoc(str): ch_list = [] for i in str: ch_list.append(i) return [ord(i)-65 for i in ch_list]
# Int to Chr # 将数字序列还原成大写字母序列 def itoc(list_int): A = [chr(i+65) for i in list_int] ch = "" for i in A: ch += i return ch
# 计算密文序列 def Encrypt(Message,k,n): print('>>>使用乘法密码算法进行加密(k={}, n={})'.format(k,n)) return itoc([i*k%n for i in stoc(Message)])
# 计算明文序列 def Decrypt(Ciphertext,k,n): # 解密方式1:通过构建密码表进行查表解密 # 解密方式2:通过加密逆运算计算明文 DecryptionType = 2 if(DecryptionType == 1): print('>>>构建密码表:') A = [i for i in range(0,n)] print('>>>明文字母表:{}'.format(itoc(A))) B = Encrypt(itoc(A),k,n) print('>>>密文字母表:{}'.format(B)) CiphertextTables = dict(zip(B,A)) print('>>>构建密码表进行查表解密') return itoc([CiphertextTables[i] for i in Ciphertext]) else: for k1 in range(0, n): if(k1 * k % n == 1): break print('>>>通过加密逆运算进行解密(k的逆元为:{})'.format(k1)) return itoc([c*k1%n for c in stoc(Ciphertext)])
if __name__=='__main__': # 当前仅支持大写字母串 A = ('ABCDEF') print('输入的明文字符串为:{}'.format(A)) B = Encrypt(A,7,26) print('加密后的密文:{}'.format(B)) C = Decrypt(B,7,26) print('解密后的明文:{}'.format(C))
# 字符串转单字符列表 def stoc(str): ch_list = [] for i in str: i = i.strip() if(i != ''): ch_list.append(i) return [i for i in ch_list]
# 字符列表转换为字符串 def ctoc(list_ch): ch = "" for i in list_ch: ch += i return ch
# 构建密码字典 def GetM_C(): A = [] B = [] C = [] for i in range(0,26): A.append(chr(i+65)) B.append([chr(i+65)]) for j in range(1,26): B[i].append((chr((i+j)%26+65))) for i in A: C.append(dict(zip(A,B[ord(i)-65]))) return C
# 计算密文序列 def Encrypt(Message,K): M_C = GetM_C() C = '' for i,k in zip(stoc(Message),stoc(K)): #print(i,k) C += M_C[ord(i)-65][k] print('>>>使用Vigenre密码算法进行加密') return C
# 通过查密码表计算明文序列 def Decrypt(Ciphertext,K): M_C = GetM_C() M = '' for c,k in zip(stoc(Ciphertext),stoc(K)): M_Dict = M_C[ord(k)-65] #print(M_Dict) for m in M_Dict.keys(): if(M_Dict[m] == c): M += m #print(m) print('>>>使用Vigenre密码算法进行解密') return M
if __name__=='__main__': # 当前仅支持大写字母串 M = ('MING CHEN WU DIAN FA DONG FAN GONG') K = ('XING CHUI PING YE KUO YUE YONG DA JIANG LIU') print('输入的明文字符串为:{}'.format(M)) print('输入的密钥字符串为:{}'.format(K)) C = Encrypt(M, K) print('加密后的密文:{}'.format(C)) M1 = Decrypt(C, K) print('解密后的明文:{}'.format(M1))
以上代码的执行效果:
1 2 3 4 5 6
输入的明文字符串为:MING CHEN WU DIAN FA DONG FAN GONG 输入的密钥字符串为:XING CHUI PING YE KUO YUE YONG DA JIANG LIU >>>使用Vigenre密码算法进行加密 加密后的密文:JQAMEOYVLCQOYRPURMHKDOAMRNP >>>使用Vigenre密码算法进行解密 解密后的明文:MINGCHENWUDIANFADONGFANGONG
# 调用Vernam算法进行加密 def Encrypt(Message,K): print('>>>调用Vernam算法进行加密') result = '' for m,k in zip(Message, K): result += Execute_Unit(CtoB(m),CtoB(k)) return result
# 调用Vernam算法进行解密 def Decrypt(Ciphertext,K): print('>>>调用Vernam算法进行解密') # Bk存放二进制K值序列 BK = '' for k in K: BK += CtoB(k) # BM存放二进制明文序列 BM = Execute_Unit(Ciphertext, BK[:len(Ciphertext)]) print('>>>二进制密钥序列:{}'.format(BK)) print('>>>二进制明文序列:{}'.format(BM)) return BtoC(BM)
if __name__=='__main__': # 当前仅支持大写字母串 M = ('MING CHEN WU DIAN FA DONG FAN GONG') K = ('XING CHUI PING YE KUO YUE YONG DA JIANG LIU') print('输入的明文字符串为:{}'.format(M)) print('输入的密钥字符串为:{}'.format(K)) C = Encrypt(M, K) print('加密后的密文:{}'.format(C)) M1 = Decrypt(C, K) print('解密后的明文:{}'.format(M1))
代码的运行效果如下:
1 2 3 4 5 6 7 8
输入的明文字符串为:MING CHEN WU DIAN FA DONG FAN GONG 输入的密钥字符串为:XING CHUI PING YE KUO YUE YONG DA JIANG LIU >>>调用Vernam算法进行加密 加密后的密文:00010101000000000000000000000000000000000000000000000000000100000000011100000000000001110001110001101110000000110110100100011000000010110000000000001101000101000110111101100100000101100001101100000010000000000001111100001110000000000110011101100111000010110000111101100111 >>>调用Vernam算法进行解密 >>>二进制密钥序列: 01011000010010010100111001000111001000000100001101001000010101010100100100100000010100000100100101001110010001110010000001011001010001010010000001001011010101010100111100100000010110010101010101000101001000000101100101001111010011100100011100100000010001000100000100100000010010100100100101000001010011100100011100100000010011000100100101010101 >>>二进制明文序列: 01001101010010010100111001000111001000000100001101001000010001010100111000100000010101110101010100100000010001000100100101000001010011100010000001000110010000010010000001000100010011110100111001000111001000000100011001000001010011100010000001000111010011110100111001000111 解密后的明文:MING CHEN WU DIAN FA DONG FAN GONG