第六讲 可交付的随机加密
版本一:
alphabet_src = 'abcdefghijklmnopqrstuvwxyz'
alphabet_tar = 'defghijklmnopqrstuvwxyzabc'
def convert_char(single_char:str,operation:str)->str:
'''对单字符进行加密
输入参数:
single_char:要加密的单字
operation:加密还是解密,encrypt->加密,decrypt->解密
返回结果:加密/解密后的单字
'''
global alphabet_src,alphabet_tar
result = ''
if single_char in alphabet_tar:
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('AbCdefgH!'))=='AbCdefgH!')
这是第五讲的遗留版,第五讲我没听,我也不知道发生了啥
版本二:字母表位置偏移法
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
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('AbCdefgH!'))=='AbCdefgH!')
版本三:ASCII码的偏移置换
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)
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('AbCdefgH!'))=='AbCdefgH!')
版本四:升级版的表到表加密
def convert_char(single_char:str,operation:str)->str:
'''对单字符进行加密
输入参数:
single_char:要加密的单字
operation:加密还是解密,encrypt->加密,decrypt->解密
返回结果:加密/解密后的单字
'''
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('AbCdefgH!'))=='AbCdefgH!')
shuffle:
shuffle(x, random=None) method of random.Random instance
Shuffle list x in place, and return None.
Optional argument random is a 0-argument function returning a
random float in [0.0, 1.0); if it is the d efault None, the
standard random.random will be used.
pickle:
Create portable serialized representations of Python objects.
Classes:
Pickler
Unpickler
Functions:
dump(object, file)
dumps(object) -> string
load(file) -> object
loads(string) -> object
open:
========= ===============================================================
Character Meaning
--------- ---------------------------------------------------------------
‘r’ open for reading (default)
‘w’ open for writing, truncating the file first
‘x’ create a new file and open it for writing
‘a’ open for writing, appending to the end of the file if it exists
‘b’ binary mode
‘t’ text mode (default)
‘+’ open a disk file for updating (reading and writing)
‘U’ universal newline mode (deprecated)
========= ===============================================================
!ls/dir
!cat /!more
with open('key.dat','wb')as f:
f.write(pickle.dumps([alphabet_src,alphabet_tar]))