1)若是大写字母,则进行字母替换:英文26个字母按键盘QWERT的顺序排列,从第一行到第三行连接起来再与字母表顺序进行替换,如Q用A来替换,Y用F替换,如下所示:
2)将经过字母替换后的明文每个字符的8位二进制ASCII码(不足8位的左端补0,补足8位)分成两段(左4位一段,右4位为另一段),如字符“A”的二进制ASCII值为01000001,分段后为0100,0001;
3)将高位段(左边4位)转化为十六进制数(如0100转化为4);
4)对低位段(右边4位)执行0→1,1→0后转化为十六进制数(如0001→1110→14→E);
5)依次连接两位十六进制数,得到该字符的密文,如“Q”的密文为“4E”;
6)将每个字符的密文按照明文的顺序依次连接。
程序运行结果如下图所示:
实现上述功能的Python程序如下,请回答下列问题:
def dtb(num): #十进制转8位二进制,高低4位分别处理
i=0;result=""
while i<8:
if i<4:
y=1-num % 2
else:
y=num % 2
num=num//2
result=str(y)+result
i+=1
return result
mw=input("请输入待加密的字符:")
jm=""
mm={"Q":"A","W":"B","E":"C","R":"D","T":"E","Y":"F","U":"G","I":"H","O":"I","P":"J","A":"K","S":"L","D":"M","F":"N","G":"O","H":"P","J":"Q","K":"R","L":"S","Z":"T","X":"U","C":"V","V":"W","B":"X","N":"Y","M":"Z"}
for i in range(len(mw)):
if "A"<=mw[i]<="Z":
else:
jm+=mw[i]
jm1=""
for i in range(len(jm)):
zf="0123456789ABCDEF";result1="";sum=0
for i in :
sum=int(jm1[i])*8+int(jm1[i+1])*4+int(jm1[i+2])*2+int(jm1[i+3])
result1+=zf[sum]
sum=0
print("密文为:",result1)