python练习-字符串加密解密

题目描述

输入两行字符串,第一行为需要加密的字符串,第二行为需要解密的字符串,按照加密解密规则输出两行,第一行为加密后的字符串,第二行为解密后的字符串。加密规则:将大写字母变换为该字母后一位字母、并小写输出,将小写字母变换为该字母后一位字母、并大写输出,将数字加1输出(9变换为0),其余字符不变。解密规则对应加密规则的逆过程。

解题思路

python练习-简单密码加密一样

python代码实现

import re

alphaList = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

def Encrypt(aucPassword, aucResult=[]):
    for x in aucPassword:
        if re.match('[A-Z]', x):
            xLower = x.lower()
            idx = alphaList.index(xLower) + 1
            if idx == len(alphaList):
                idx = 0
            x = alphaList[idx]
        elif re.match('[a-z]', x):
            idx = alphaList.index(x) + 1
            if idx == len(alphaList):
                idx = 0
            x = alphaList[idx].upper()
        elif re.match('[0-9]', x):
            if x == '9':
                x = '0'
            else:
                x = str(int(x) + 1)
        aucResult.append(x)
    print("".join(aucResult))

def unEncrypt(password, result=[]):
    for x in password:
        if re.match('[A-Z]', x):
            xLower = x.lower()
            idx = alphaList.index(xLower) - 1
            if idx == -1:
                idx = len(alphaList)
            x = alphaList[idx]
        elif re.match('[a-z]', x):
            idx = alphaList.index(x) - 1
            if idx == -1:
                idx = len(alphaList)
            x = alphaList[idx].upper()
        elif re.match('[0-9]', x):
            if x == '0':
                x = '9'
            else:
                x = str(int(x) - 1)
        result.append(x)
    print(''.join(result))

pwdList = []
for i in range(2):
    pwdList.append(input())
Encrypt(pwdList[0])
unEncrypt(pwdList[1])
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值