第六讲 可交付的随机加密


在这里插入图片描述

版本一:

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]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值