1.(全大写的变量名作为常量,通常不对它进行修改,之后默认如此)
之前所学习的简单加密是双表,表对表的,现在所给出的是单表+偏移的简单加密,只用一张表如何实现字母的替换。
def convert_char(single_char:str,operation:str)->str:
'''对单个字符进行加密
输入参数:single_char:要加密的单个字符
operation:加密还是解密,encrypt->加密,decrypt->解密
返回结果:加密/解密后的单个字符
'''
OFFSET=10
ALPHABET_SRC='abcdefghijklmnopqrstuvwxyz'
result=''
if single_char in ALPHABET_SRC:
if operation=='encrypt':
result=ALPHABET_SRC[(ALPHABET_SRC.index(single_char)+OFFSET)%26]
elif operation=='decrypt':
result=ALPHABET_SRC[(ALPHABET_SRC.index(single_char)-OFFSET)%26]
else:
result=single_char
return result
接下来依旧调用前一篇的encrypt_it()和decrypt_it()函数
print(encrypt_it('hello world!'))
print(decrypt_it('rovvy gybvn!'))
验证结果正确。
2.用ASCLL码进行偏移置换(33-126),无表
(查看字符ASCLL码用ord()函数,如
)
def convert_char(single_char:str,operation:str)->str:#参数为字符和所执行的操作
'''对单个字符进行加密
输入参数:single_char:要加密的单个字符
operation:加密还是解密,encrypt->加密,decrypt->解密
返回结果:加密/解密后的单个字符
'''
OFFSET=10
result=''
if ord(single_char) >=33 and ord(single_char)<=126 :
if operation=='encrypt':
result=chr((ord(single_char)-33+OFFSET)%(126-33+1)+33)#chr()意为转化为字符型
elif operation=='decrypt':
result=chr((ord(single_char)-33-OFFSET)%(126-33+1)+33)
else:
result=single_char
return result
def encrypt_it(src_str:str)->str:
'''
用于对字符串进行简单替换加密
输入参数:
src_str:原始文本内容
返回结果:加密文本
'''
encrypted_str=''
for single_char in src_str:
encrypted_str+=convert_char(single_char,'encrypt')
return encrypted_str
def decrypt_it(encrypted_str:str)->str:
'''
用于对字符串进行简单替换解密
输入参数:
encrypted_str:加密文本内容
返回结果:解密文本
'''
decrypted_str=''
for single_char in encrypted_str:
decrypted_str+=convert_char(single_char,'decrypt')
return decrypted_str
#对函数互反性进行验证
assert(decrypt_it(encrypt_it(''))=='')
print(encrypt_it('AbCdefgH!'))
print(decrypt_it('KlMnopqR+'))
运行结果:
3.random.shuffle()函数:用来打乱表,例:
a=[1,2,3,4,5]
random.shuffle(a)
print(a)
需要注意的是,random函数要先初始化,不然会出现错误
name ‘random’ is not defined
初始化代码:
import random
下面介绍几段语句:
alphabet_src=[chr(i) for i in range(33,127)]#ASCLL码表里的元素按行返回为字符
alphabet_tar=alphabet_src.copy()#浅拷贝,避免元数据被篡改
random.shuffle(alphabet_tar)#打乱表
在程序执行过程中,shuffle调用时机不当,有可能造成逻辑问题,在加密过程中,要避免shuffle的多次调用,尽量使其调用次数减少,解决方法是把shuffle语句单独运行,例:
用shuffle的加密写法:
alphabet_src=[chr(i) for i in range(33,127)]
alphabet_tar=alphabet_src.copy()
random.shuffle(alphabet_tar)
def convert_char(single_char,operation):#参数为字符和所执行的操作
global alphabet_src,alphabet_tar#使用全局变量的alphabet_src
result=''
if ord(single_char)>=33 and ord(single_char) <=126:
if operation=='encrypt':
result=alphabet_tar[alphabet_src.index(single_char)]
elif operation=='decrypt':
result=alphabet_src[alphabet_tar.index(single_char)]
else:
result=single_char
return result
def encrypt_it(src_str:str)->str:
'''
用于对字符串进行简单替换加密
输入参数:
src_str:原始文本内容
返回结果:加密文本
'''
encrypted_str=''
for single_char in src_str:
encrypted_str+=convert_char(single_char,'encrypt')
return encrypted_str
def decrypt_it(encrypted_str:str)->str:
'''
用于对字符串进行简单替换解密
输入参数:
encrypted_str:加密文本内容
返回结果:解密文本
'''
decrypted_str=''
for single_char in encrypted_str:
decrypted_str+=convert_char(single_char,'decrypt')
return decrypted_str
#对函数互反性进行验证
assert(decrypt_it(encrypt_it(''))=='')
print(encrypt_it('hello world!'))
print(decrypt_it('khoor zruog!'))
4.序列化:
pickle.dumps()(把内存中的对象表示成一个序列,一个字符串)
pickle.dumps([alphabet_src,alphabet_tar])#'b代表二进制串'
反序列化:序列化的逆过程
pickle.loads(pickle.dumps([alphabet_src,alphabet_tar]))
·········
5.文件的存取
open()函数,用法:
写成一个文件实现交付
f=open('test.txt','wb')#w后的b是用来指定二进制模式
f.write('12312324235345')
f.close()#一定要关上,不然会造成读写混乱
检查写入是否成功:(在windows系统下)
!dir test.txt#windows系统
!more test.txt
f=open('key.dat','wb')#w后的b是用来指定二进制模式
f.write(pickle.dumps([alphabet_src,alphabet_tar]))
f.close()
为防止忘记close,可以采取另外一个写法
with open('key.dat','wb')as f:#另外一种写法
f.write(pickle.dumps([alphabet_src,alphabet_tar]))
with open('key.dat','rb')as f:#b,用二进制读,就要用二进制写
print(pickle.loads(f.read()))
交付的是key.dat文件
pickle.dump(
[alphabet_src,alphabet_tar],
open('key1.dat','wb')
)
pickle.load(open('key.dat','rb'))
6.字典,映射关系
(升级版的表到表)
a={
'a':'b',
'c':'d',
}#字典,a映射到b,c到d,无序的
a['c']
a=dict()#空字典
加密的字典式改造:提高效率
import random
alphabet_src=[chr(i) for i in range(33,127)]
alphabet_tar=alphabet_src.copy()
random.shuffle(alphabet_tar)
alphabet_s2t_dict=dict()
alphabet_t2s_dict=dict()
for i in range(len(alphabet_src)):#写字典
alphabet_s2t_dict[alphabet_src[i]]=alphabet_tar[i]
alphabet_t2s_dict[alphabet_tar[i]]=alphabet_src[i]
def convert_char(single_char,operation):#参数为字符和所执行的操作
global alphabet_src,alphabet_tar#使用全局变量的alphabet_src
result=''
if ord(single_char)>=33 and ord(single_char) <=126:
if operation=='encrypt':
result=alphabet_s2t_dict[single_char]
elif operation=='decrypt':
result=alphabet_t2s_dict[single_char]
else:
result=single_char
return result
def encrypt_it(src_str:str)->str:
'''
用于对字符串进行简单替换加密
输入参数:
src_str:原始文本内容
返回结果:加密文本
'''
encrypted_str=''
for single_char in src_str:
encrypted_str+=convert_char(single_char,'encrypt')
return encrypted_str
def decrypt_it(encrypted_str:str)->str:
'''
用于对字符串进行简单替换解密
输入参数:
encrypted_str:加密文本内容
返回结果:解密文本
'''
decrypted_str=''
for single_char in encrypted_str:
decrypted_str+=convert_char(single_char,'decrypt')
return decrypted_str
#对函数互反性进行验证
assert(decrypt_it(encrypt_it(''))=='')
print(encrypt_it('hello world!'))
print(decrypt_it('>x661 k1.6IW'))