Python-可交付的随机加密

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'))

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值