python 维吉尼亚密码加密和解密

最近在学密码学,作业里面的加密解密看得脑壳疼,刚好寒假自学了一些python,第一反应就是用python帮我加解密。

菜菜的我写了好久,刚写完,然后同学告诉我维吉尼亚密码有专门的加密解密的库,我一下就震惊加无语了。但是还是要发在这里让大家看看吧。注意,加密和解密的密钥是同一个,所以一次运行最好只去加密或者解密。

如果有bug或者可以改进的地方,欢迎评论发表哈。

import string


def encrypt(Plain, Key):
    print('加密的密文为:', end='')
    count = 0  # 计数器,如果遇到标点或者空格加1,在Key中减计数器
    for i in range(len(Plain)):
        if Plain[i] not in (string.punctuation + ' '):
            Plain_i = ord(Plain[i])  # 明文中第i个字符的ascii
            Key_i = ord(Key[(i - count) % len(Key)])  # 密钥中对应个字符的ascii
            Char = enascii_turn(Plain_i, Key_i)
            print(Char, end='')  # ord(字母)转ascii
        else:
            print(Plain[i], end='')
            count += 1


def decrypt(cipher, Key):  # 大写转大写,小写转小写
    print('')
    print('解密的明文为:', end='')
    count = 0
    for i in range(len(cipher)):
        if cipher[i] not in (string.punctuation + ' '):
            cipher_i = ord(cipher[i])
            Key_i = ord(Key[(i - count) % len(Key)])
            Char = deascii_turn(cipher_i, Key_i)
            print(Char, end='')
        else:
            print(cipher[i], end='')
            count += 1


def keep(num):  # 单个字母ascii转换
    if num - 96 > 0:
        return num - 97
    else:
        return num - 65


def enascii_turn(num1, num2):  # 加密ascii码转换
    if num1 > 96:
        num1 = ((keep(num1) + keep(num2)) % 26) + 97
        return chr(num1)  # chr(数字)转字母
    else:
        num1 = ((keep(num1) + keep(num2)) % 26) + 65
        return chr(num1)


def deascii_turn(num1, num2):  # 解密ascii码转换
    if num1 > 96:
        num1 = ((keep(num1) - keep(num2)) % 26) + 97
        return chr(num1)
    else:
        num1 = ((keep(num1) - keep(num2)) % 26) + 65
        return chr(num1)


def main():
    Plaintext = input("请输入明文")
    Key = input("请输入密钥")
    ciphertext = input("请输入密文")

    ciphertext = ' '.join(ciphertext.split())  # 多个空格转换:先split分成列表,再。join转换成字符串.‘ ’间隔一个空格
    encrypt(Plaintext, Key)
    decrypt(ciphertext, Key)


main()

例如题目:下列密文是通过维吉尼亚密码加密得来的, 试求其明文。

Per zlrracm, vxmcs r qipqlczhs. Qs fcv rihw sxx hblrxh sm nkidhvzphw. Ixxvn qsn, lysh sifecs uui jrrfyg, mk xj suvc kd ss wbrzrrz uqh jpp zyw qv ylgn osfz fin isi bpgyoj, fg dm zdqzap, cl sifecs qks cdfy iu xyxey iu tipp zcni dt. Sin lj nt rfy jszcx hi jik iyfixky iysmh hzuwwwxpk izayv; mw lv olh kfxeu nr gitrhy d afgcr qkiit vjyucsdum bdw kwv cjssiilbcwc kd wwhg e ads, ohg ewuffx fscavuy; lj nt rfy jszcx hi vemt kvy hrmxichpiei rbx giwtrh zxxlgv duqhvbzqm, wlvc ns uui xdzba ws ypms nr hf xk hijikwvf.

由不知名手段获得密钥(求助万能的同学)为:prouder

解密后:And finally, build a community. No one does big things by themselves. Right now, when people are scared, it is easy to be cynical and say let me just look out for myself, or my family, or people who look or think or pray like me. But if we are going to get through these difficult times; if we are going to create a world where everybody has the opportunity to find a job, and afford college; if we are going to save the environment and defeat future pandemics, then we are going to have to do it together.

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值