python:异或加密算法

def encode(plaintext,key):
    key = key * (len(plaintext) // len(key)) + key[:len(plaintext) % len(key)]#取整数/余数
    ciphertext=[]
    for i in range(len(plaintext)):
        ciphertext.append(str(ord(plaintext[i])^ord(key[i])))
    return ','.join(ciphertext)
#解密
def decode(ciphertext,key):
    ciphertext=ciphertext.split(',')
    key=key*(len(ciphertext)//len(key))+key[:len(ciphertext)%len(key)]#取整数/余数
    plaintext=[]
    for i in range(len(ciphertext)):
        plaintext.append(chr(int(ciphertext[i])^ord(key[i])))
    return ''.join(plaintext)

if __name__ == '__main__':
    functions=input('输入A加密,输入B解密,其它关闭>>>>')
    if functions=='A':
        plaintext=input('请输入加密文字明文>>>')
        key=input('请输入加密密钥>>>')
        print('密文',encode(plaintext,key))
    if functions=='B':
        ciphertext = input('请输入解密文字明文>>>')
        key = input('请输入解密密钥>>>')
        print('明文',decode(ciphertext,key))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值