明文加密,解密算法

时间:2024-03-19 08:21:04

课堂练习
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)

明文加密,解密算法