本文为观看教学视频后的笔记整理,原教程来自哔哩哔哩爱可可爱生活
有时间的小伙伴可以去看一看,老师那里还有很多比较基础的教程
点击此处,进入Github获取下方代码的完整 JupyterNotebook 文件
str.index() #找索引
help() #多用help
%pdb #辅助调试
assert('')#bool表达式验证
ord('!')#获取字符的ASCII码
random.shuffle()
list.copy()#列表的浅拷贝,直接赋值会指向同一空间,原表会被改掉
dir(random)#查看成员函数
辨析局部变量与全局变量
a = 1
def mytest1():
a = 2
print(a)
mytest1()
print(a)
def mytest2():
global a
a = 2
print(a)
mytest2()
print(a)
声明字母表,并用替换的方式建立新的字母表
alphabet_src = "abcdefghijklmnopqrstuvwxyz"#起名要注意可读性,变量名用_,函数名用驼峰式
alphabet_tar = "defghijklmnopqrstuvwxyzabc"
定义待加密文本
src_str = 'hello world!'
加密过程
encrypted_str = ''
for single_char in src_str:
if single_char in alphabet_src:
index = alphabet_src.index(single_char)
encrypted_str = encrypted_str + alphabet_tar[index]
else:
encrypted_str = encrypted_str + single_char
print(encrypted_str)
解密过程
decryped_str = ''
for single_char in encrypted_str:
if single_char in alphabet_tar:
index = alphabet_tar.index(single_char)
decryped_str = decryped_str + alphabet_src[index]
else:
decryped_str = decryped_str + single_char
print(decryped_str)
alphabet_src = "abcdefghijklmnopqrstuvwxyz"#起名要注意可读性,变量名用_,函数名用驼峰式
alphabet_tar = "defghijklmnopqrstuvwxyzabc"
注意体会封装性
def encryptIt(src_str):
encrypted_str = ''
for single_char in src_str:
if single_char in alphabet_src:
index = alphabet_src.index(single_char)
encrypted_str = encrypted_str + alphabet_tar[index]
else:
encrypted_str = encrypted_str + single_char
return encrypted_str
def decryptIt(encrypted_str):
decryped_str = ''
for single_char in encrypted_str:
if single_char in alphabet_tar:
index = alphabet_tar.index(single_char)
decryped_str = decryped_str + alphabet_src[index]
else:
decryped_str = decryped_str + single_char
return decryped_str
print(encryptIt("I love you!"))#不同逻辑之间隔开
print(decryptIt("I oryh brx!"))
alphabet_src = "abcdefghijklmnopqrstuvwxyz"#起名要注意可读性,变量名用_,函数名用驼峰式
alphabet_tar = "defghijklmnopqrstuvwxyzabc"
def encryptIt(src_str):
global alphbet_src,alphabet_tar#全局变量
encrypted_str = ''
for single_char in src_str:
if single_char in alphabet_src:
index = alphabet_src.index(single_char)
encrypted_str = encrypted_str + alphabet_tar[index]
else:
encrypted_str = encrypted_str + single_char
return encrypted_str
def decryptIt(encrypted_str):
global alphbet_src,alphabet_tar#全局变量
decryped_str = ''
for single_char in encrypted_str:
if single_char in alphabet_tar:
index = alphabet_tar.index(single_char)
decryped_str = decryped_str + alphabet_src[index]
else:
decryped_str = decryped_str + single_char
return decryped_str
print(encryptIt("I love you!"))#不同逻辑之间隔开
print(decryptIt("I oryh brx!"))
alphabet_src = "abcdefghijklmnopqrstuvwxyz"#起名要注意可读性,变量名用_,函数名用驼峰式
alphabet_tar = "defopquvghnwxyrstzabcijklm"
自己编写的help适用于多人协作,增强代码的可读性。另外要尽量写英文,少使用中文。
def encryptIt(src_str:str) -> str:
'''Encrypt string -- Simple replacement encryption
Input parameters:
src_str:original text
Return result:encrypted text
'''
global alphbet_src,alphabet_tar#全局变量
encrypted_str = ''
for single_char in src_str:
if single_char in alphabet_src:
index = alphabet_src.index(single_char)
encrypted_str = encrypted_str + alphabet_tar[index]
else:
encrypted_str = encrypted_str + single_char
return encrypted_str
def decryptIt(encrypted_str:str) -> str:
'''Decrypt string -- Simple replacement decryption
Input parameters:
src_str:encrypted text
Return result:original text
'''
global alphbet_src,alphabet_tar
decryped_str = ''
for single_char in encrypted_str:
if single_char in alphabet_tar:
index = alphabet_tar.index(single_char)
decryped_str = decryped_str + alphabet_src[index]
else:
decryped_str = decryped_str + single_char
return decryped_str
print(encryptIt("I love you!"))#不同逻辑之间隔开
print(decryptIt("I wrip lrc!"))
help(encryptIt)
help(decryptIt)
使用索引就可以少定义一个字母表了,同时便于随时更改加密方式
alphabet_src = "abcdefghijklmnopqrstuvwxyz"#起名要注意可读性,变量名用_,函数名用驼峰式
def encryptIt(src_str:str) -> str:
'''Encrypt string -- Simple replacement encryption
Input parameters:
src_str:original text
Return result:encrypted text
'''
global alphbet_src,alphabet_tar#全局变量
result = ''
for single_char in src_str:
if single_char in alphabet_src:
old_index = alphabet_src.index(single_char)
new_index = (old_index + 3) % 26
result = result + alphabet_src[new_index]
else:
result = result + single_char
return result
def decryptIt(encrypted_str:str) -> str:
'''Decrypt string -- Simple replacement decryption
Input parameters:
src_str:encrypted text
Return result:original text
'''
global alphbet_src,alphabet_tar
result = ''
for single_char in encrypted_str:
if single_char in alphabet_src:
old_index = alphabet_src.index(single_char)
new_index = (old_index - 3) % 26
result = result + alphabet_src[new_index]
else:
result = result + single_char
return result
print(encryptIt("I love you!"))#不同逻辑之间隔开
print(decryptIt("I oryh brx!"))
有时候我们可以让代码更精简。
alphabet_src = "abcdefghijklmnopqrstuvwxyz"#起名要注意可读性,变量名用_,函数名用驼峰式
def cryptIt(src_str:str,if_decrypt:bool = False) -> str:
'''Encrypt and Decrypr string -- Simple replacement encryption
Input parameters:
src_str:original text
if_decrypt:True--Encrypt,False--Decrypt
Return result:encrypted or decrypted text
'''
global alphbet_src,alphabet_tar#全局变量
result = ''
for single_char in src_str:
if single_char in alphabet_src:
new_index = (alphabet_src.index(single_char) - 3) % 26 \
if if_decrypt is True \
else (alphabet_src.index(single_char) + 3) % 26 #字符移位替换
result += alphabet_src[new_index]
else:
result += single_char
return result
print(cryptIt("I love you!",True))#不同逻辑之间隔开
print(cryptIt("I ilsb vlr!"))
思考: 代码一定是越精简越好吗?
alphabet_src = "abcdefghijklmnopqrstuvwxyz"#起名要注意可读性,变量名用_,函数名用驼峰式
def cryptIt(src_str:str,if_decrypt:bool = False) -> str:
'''Encrypt and Decrypr string -- Simple replacement encryption
Input parameters:
src_str:original text
if_decrypt:True--Encrypt,False--Decrypt
Return result:encrypted or decrypted text
'''
global alphbet_src,alphabet_tar#全局变量
return ''.join([alphabet_src[(alphabet_src.index(single_char) - 3) % 26 \
if if_decrypt is True \
else (alphabet_src.index(single_char) + 3) % 26] if \
single_char in alphabet_src else single_char \
for single_char in src_str])
print(cryptIt("I love you!",True))#不同逻辑之间隔开
print(cryptIt("I ilsb vlr!"))
代码长度要兼顾简洁性与可读性。
alphabet_src = "abcdefghijklmnopqrstuvwxyz"#起名要注意可读性,变量名用_,函数名用驼峰式
def convert_char(single_char,operation):
global alphabet_src
if single_char in alphabet_src:
old_index = alphabet_src.index(single_char)
if operation == 'encrypt':
new_index = (old_index + 3) % 26
else:
new_index = (old_index - 3) % 26
else:
return single_char
return alphabet_src[new_index]
def encrypt_it(src_str:str) -> str:
'''Encrypt string -- Simple replacement encryption
Input parameters:
src_str:original text
Return result:encrypted text
'''
#列表推导式,局部封装
encrypted_str = ''.join([convert_char(single_char,'encrypt') for single_char in src_str])
return encrypted_str
def decrypt_it(encrypted_str:str) -> str:
'''Decrypt string -- Simple replacement decryption
Input parameters:
src_str:encrypted text
Return result:original text
'''
#列表推导式,局部封装
decrypted_str = ''.join([convert_char(single_char,'decrypt') for single_char in encrypted_str])
return decrypted_str
assert(decrypt_it(encrypt_it('abdkwe')) == 'abdkwe')#对互反性进行验证
print(encrypt_it("I love you!"))#不同逻辑之间隔开
print(decrypt_it("I oryh brx!"))
不同于上方的只能加密字母的代码,使用ASCII码甚至可以加密符号。
def convert_char(single_char,operation):
OFFSET = 10
if ord(single_char) >=33 and ord(single_char) <=126:
if operation == 'encrypt':
result = chr((ord(single_char) -33 + OFFSET) % (126 -33 + 1) + 33)
else:
result = chr((ord(single_char) -33 - OFFSET) % (126 -33 + 1) + 33)
else:
return single_char
return result
def encrypt_it(src_str:str) -> str:
'''Encrypt string -- Simple replacement encryption
Input parameters:
src_str:original text
Return result:encrypted text
'''
#列表推导式,局部封装
encrypted_str = ''.join([convert_char(single_char,'encrypt') for single_char in src_str])
return encrypted_str
def decrypt_it(encrypted_str:str) -> str:
'''Decrypt string -- Simple replacement decryption
Input parameters:
src_str:encrypted text
Return result:original text
'''
#列表推导式,局部封装
decrypted_str = ''.join([convert_char(single_char,'decrypt') for single_char in encrypted_str])
return decrypted_str
assert(decrypt_it(encrypt_it('abdkwe')) == 'abdkwe')#对互反性进行验证
print(encrypt_it("I love you!"))#不同逻辑之间隔开
print(decrypt_it("S vy\"o %y!+"))
import random
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
if single_char in alphabet_src:
if operation == 'encrypt':
result = alphabet_tar[alphabet_src.index(single_char)]
else:
result = alphabet_src[alphabet_tar.index(single_char)]
else:
result = single_char
return result
def encrypt_it(src_str:str) -> str:
'''Encrypt string -- Simple replacement encryption
Input parameters:
src_str:original text
Return result:encrypted text
'''
#列表推导式,局部封装
encrypted_str = ''.join([convert_char(single_char,'encrypt') for single_char in src_str])
return encrypted_str
def decrypt_it(encrypted_str:str) -> str:
'''Decrypt string -- Simple replacement decryption
Input parameters:
src_str:encrypted text
Return result:original text
'''
#列表推导式,局部封装
decrypted_str = ''.join([convert_char(single_char,'decrypt') for single_char in encrypted_str])
return decrypted_str
assert(decrypt_it(encrypt_it('abdkwe')) == 'abdkwe')#对互反性进行验证,不报错就是ok
print(encrypt_it("I love you!"))#不同逻辑之间隔开
print(decrypt_it("9 N{3$ S{~r"))
版本二中的乱序加密,只能在一台电脑中使用,原因在于我们使用了随机数的生成。如果将加密后的文本发送给其他人,收件人根本就无法找出正确的加密规律。所以我们要把加密后的字母表存入文件,将该文件与加密后的文本一起发给收件人。
import pickle
!ls < test.txt
!more < test.txt
f = open('key.dat','wb')
f.write(pickle.dumps([alphabet_src,alphabet_tar]))
f.close()
import random
alphabet_src = [chr(i) for i in range(33,127)]
alphabet_tar = alphabet_src.copy()
random.shuffle(alphabet_tar)
with open('key.dat','wb') as f:#这样忘记close也不要紧
f.write(pickle.dumps([alphabet_src,alphabet_tar]))
with open('key.dat','rb') as f:#这样忘记close也不要紧
print(pickle.loads(f.read()))
pickle.dump(
[alphabet_src,alphabet_tar]
open('key1.dat','wb')
)
!ls < key1.dat
!more < key1.dat
pickle.load(open('key1.dat','rb'))
感谢你的阅读~
如有疑问,可以直接评论或私聊我~
点击此处,进入Github获取上述代码的完整 JupyterNotebook 文件