明文加密,解密演算法
課堂練習 1、用簡單字母置換產生的密文仍然保持明文的統計特徵。為打亂密文的統計結構,可採取如下的加密方法,它是排斥加加密演算法的擴充套件。將英語的26個字母按算許對映成為0,1,2,3,…,25,並記此對映為I,即I(A)=0, I(B)=1, …, I(Z)=25。令X和Y為兩個英文字母,令 X+Y = I-1([ I(x) + I(Y) ] mod 26) 其中I-1為I的反函式,即I-1(0)=A, I-1(1)=B,…, I-1(25)=Z. 令X = X1X2…Xl和Y = Y1Y2…Yl為長度相等英文字母串,令 X + Y = (X1+Y1)…(Xl+Yl) 令金鑰K為任意英文字母串,並記K的長度為l。(金鑰K可長可短,而且同一字母可出現多次。)令明文M=M1M2…Mk,這裡除Mk外所有Mi均為由l個字母組成的片段,而Mk的長度m滿足0<m<=l。令Km為K的前m個英文字母。定義加密演算法E如下: E(K, M) = C1C2…Ck 其中Ci = K+Mi, i=1,2,…,k-1, Ck=Km+Mk (a) 給出解密演算法D (b) 令K=BLACKHAT。將下列明文翻譯成密文: Methods of making messages unintelligible to adversaries have been necessary. Substitution is the simplest method that replaces a character in the plaintext with a fixed different character in the ciphertext. This method preserves the letter frequency in the plaintext and so one can search for the plaintext from a given ciphertext by comparing the frequency of each letter against the known common frequency in the underlying language.
(a)D(k,c)=M1M2…Mk M(in)=|C(in)-k(i)| (b)
m="Methods of making messages unintelligible to adversaries have been necessary. Substitution is the simplest method that replaces a character in the plaintext with a fixed different character in the ciphertext. This method preserves the letter frequency in the plaintext and so one can search for the plaintext from a given ciphertext by comparing the frequency of each letter against the known common frequency in the underlying language."
k="BLACKHAT"
E=[]
j=int(0)
for i in range(len(m)):
if m[i]==" " :
e=" "
else:
e=chr(((ord(m[i].lower())-97)+(ord(k[j%len(k)].lower())-97))%26+97)
j+=1
E.append(e)
print(E)