- 简单替换
- 偏移量
- ascall
- hafuman 编码
1.简单替换
为26个字母建立一个对应的密文字母表,将发送的消息用密文替换,接受方参照表进行解密
string1="qwertyuiopasdfhjklzxcbnm"
string2="rtyuiopasdfghjklzxcbnmqwe"
string="l love you"
def encrypted_str(string):
alternative_string=""
for i in string:
if i in string1:
index=string1.index(i)
alternative_string+=string2[index]
else:
alternative_string+=i
return alternative_string
#print(encrypted_str(string))
#解密
def decrypted_str(string):
alternative_string=""
for i in string:
if i in string2:
index=string2.index(i)
alternative_string+=string1[index]
else:
alternative_string+=i
return alternative_string
print(decrypted_str(encrypted_str(string)))
2.偏移转换
old_world是一份参照表,找到消息中的字符在表中的位置,按照一定的偏移量进行偏移,实现加密
import math
old_word="qwertyuiopasdfghjklzxcvbnm"
#new_world=""
def encrypted(old):
global old_word
new_world=""
for i in old:
if i not in old_word:
new_world+=i
else:
index=old_word.index(i)
new_world+=old_word[(index+5)%26]
# print(new_world)
return new_world
new_word=encrypted("l love you")
print(new_word)
3.使用Ascall进行加密
python使用python提供了ord()函数,将字符转化内Ascall码中对应的建值转换公式为(ord(i)-33+10)%(127-33)+33
old_word="qwertyuiopasdfghjklzxcvbnm"
def encrypted(old):
new_word=""
for i in old:
if ord(i)>=33&ord(i)<=127:
new_word+=chr((ord(i)-33+10)%(127-33)+33)
else:
new_word+=i
return new_word
a=encrypted("l am shihao")
print(a)
4.建立哈夫曼树
哈夫曼树是最小生成树的一种,可借助于其对字符进行加密,解密待日后写出来补充。
1. 建立一个treemode类用于存储节点,有4个属性,分别是key,value,left,right.
2.构造哈夫曼树时,可使用列表做为数据结构,每次对其进行排序并使用pop()弹出值最小的2个节点。
3.当列表中只有一个元素时将其作为树根返回。
message="I am"
dir_={}
listnode=[]
for i in message:
dir_[i]=ord(i)
print(dir_)
class treenode(object):
def __init__(self,key=None,value=None):
self.message=key
self.value=value
self.left=None#左
self.right=None#右
class buildtree:
def __init__(self,listnode):
self.listnode=listnode
self.bootnode=None
self.mode=[]
def mysort(self):
self.listnode.sort(key=lambda listnode:listnode.value,reverse=True)
#for i in listnode:
#print(i.value)
while(len(listnode)>1):
left=listnode.pop()
right=listnode.pop()
unleaf_node=treenode(key=chr((left.value+right.value)),value=(left.value+right.value))
unleaf_node.left=left
unleaf_node.right=right
listnode.append(unleaf_node)
self.listnode.sort(key=lambda listnode:listnode.value,reverse=True)
self.bootnode=listnode.pop()
print(self.bootnode.value)
return self.bootnode
def hafuman_mode(self,bootnode):
Node=bootnode
if bootnode.left!=None:
self.mode.append(0)
self.hafuman_mode(bootnode.left)
#hafuman_tree()
if bootnode.right!=None:
self.mode.append(1)
self.hafuman_mode(bootnode.right)
if (33<ord(Node.message))&(130>ord(Node.message)):
print(Node.message,":",self.mode)
try:
self.mode.pop()
except:
pass
#hafuman_tree()
for i in dir_.keys():
p=treenode(key=i,value=dir_[i])
listnode.append(p)
hafuman_tree=buildtree(listnode)
bootnode=hafuman_tree.mysort()
hafuman_tree.hafuman_mode(bootnode)
bootnode.left
#print(listnode[1].value)
5.使用模块pickle
pickle提供了对数据进行序列化的操作,可借助它将消息进行序列化。
import pickle
#help(pickle)
s="12345asdfgzxcvb"
with open("pickle.dat","wb") as f:
f.write(pickle.dumps(s))
with open("pickle.dat","rb")as f:
s1=f.read()
s2=pickle.loads(s1)
print(s1,s2)
11