第四讲 简单加密

简易替换密码

本讲大纲

在这里插入图片描述

版本一:原始版

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)
decrypted_str=''
for single_char in encrypted_str:
    if single_char in alphabet_tar:
        index = alphabet_tar.index(single_char)
        decrypted_str=decrypted_str+alphabet_src[index]
    else:
        decrypted_str=decrypted_str+single_char
print (decrypted_str)

TIPs:
%pdb
q 结束进程

help():

Help on _Helper in module _sitebuiltins object:
class _Helper(builtins.object)
| Define the builtin ‘help’.
|
| This is a wrapper around pydoc.help that provides a helpful message
| when ‘help’ is typed at the Python interactive prompt.
|
| Calling help() at the Python prompt starts an interactive help session.
| Calling help(thing) prints help for the python object ‘thing’.
|
| Methods defined here:
|
| call(self, *args, **kwds)
| Call self as a function.
|
| repr(self)
| Return repr(self).
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| dict
| dictionary for instance variables (if defined)
|
| weakref
| list of weak references to the object (if defined)

版本二:函数

alphabet_src = 'abcdefghijklmnopqrstuvwxyz'
alphabet_tar = 'defghijklmnopqrstuvwxyzabc'

def encryptIt(src_str:str)->str:
    global alphabet_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:
    '''用于对字符串进行简单加密替换
    输入参数:
    encryptIt:加密文本
    返回结果:原始文本   
    '''  
    global alphabet_src,alphabet_tar
    decrypted_str=''
    for single_char in encrypted_str:
        if single_char in alphabet_tar:
            index = alphabet_tar.index(single_char)
            decrypted_str=decrypted_str+alphabet_src[index]
        else:
            decrypted_str=decrypted_str+single_char
    return decrypted_str
    


print(encryptIt('hello world!'))    
print(decryptIt('khoor zruog!'))    

TIPs:
全局变量: global
命名空间的隔离

global alphabet_src,alphabet_tar

验证: assert
测试代码

assert( 'abcdefghijklmnopqrstuvwxyz'==decryptIt(encryptIt('abcdefghijklmnopqrstuvwxyz')))

函数地说明文档

def encryptIt(src_str:str)->str:
    '''用于对字符串进行简单加密替换
    输入参数:
    src_str:原始文本内容
    返回结果:加密文本    
    '''

仅为说明,输入非所需参数时,也不会报错

help(encryptIt)

Help on function encryptIt in module main:
encryptIt(src_str: str) -> str
用于对字符串进行简单加密替换
输入参数:
src_str:原始文本内容
返回结果:加密文本

作用:更好地封装

版本三:自动移位

alphabet_src = 'abcdefghijklmnopqrstuvwxyz'

def encryptIt(src_str:str)->str:
    '''用于对字符串进行简单加密替换
    输入参数:
    src_str:原始文本内容
    返回结果:加密文本    
    '''
    global alphabet_src
    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 if old_index + 3 < 26 else old_index + 3 - 26 
            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:
    '''用于对字符串进行简单加密替换
    输入参数:
    encryptIt:加密文本
    返回结果:原始文本   
    '''
    global alphabet_src
    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
    
assert( 'abcdefghijklmnopqrstuvwxyz'==decryptIt(encryptIt('abcdefghijklmnopqrstuvwxyz')))

print(encryptIt('hello world!'))
print(decryptIt('khoor zruog!'))

两个函数,代码重复率过高

版本四:完善版

alphabet_src = 'abcdefghijklmnopqrstuvwxyz'

def cryptIt(src_str:str,if_decrypted:bool=False)->str:
    '''用于对字符串进行简单加密/解密
    输入参数:
    src_str:原始文本内容
    if_decrypted:True表示解密过程,False表示加密过程
    返回结果:加密/解密文本    
    '''
    global alphabet_src
    result=''
    for single_char in src_str:
        if single_char in alphabet_src:
            old_index = alphabet_src.index(single_char)
            if if_decrypted==True:
                new_index=(old_index -3)%26
            else:
                new_index=(old_index +3)%26
            result=result+alphabet_src[new_index]
        else:
            result=result+single_char
    return result
    
assert( 'abcdefghijklmnopqrstuvwxyz'==cryptIt(cryptIt('abcdefghijklmnopqrstuvwxyz'),True))

print(cryptIt('hello world!',False))
print(cryptIt('khoor zruog!',True))

20200405
爱可可-爱生活 陈光老师课堂记录 第四讲
b站原课程链接:https://www.bilibili.com/video/BV1b7411N7P2?p=10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值