zoukankan      html  css  js  c++  java
  • python语言使用rsa密码算法对数据加密时不能对中文加密问题的解决

    在网上找的代码,使用python对RSA密码算法的复现,运行调试只能对数字和字母等,但是不能对中文成功加解密

    针对这个问题,我的解决思路是查看编码问题,可是对于代码不好的自己来说,很多地方都不懂,

    网上查了好多也不知道怎么编码,也不会改动原加解密函数,

    最后索性将中文数据转换问原函数可以正常加解密的数字,比如“0”或“1”字符,而原函数加密后将数据变成了数组,

    第一步:将待加密数据转换成0、1字符

    第二步:对0,1字符进行加密

    第三步:对密文数据进行解密

    第四步:对解密的得到的数据再次转回原数据

    def test():
    p, q, e = make_p_q_e()
    # 1,获取数据
    text = input("待加密数据:")
    # 2,将输入的数据转化成二进制
    plaintext = ' '.join(format(ord(x), 'b') for x in text)
    print(plaintext)
    # 公钥、私钥
    public_key, private_key = make_key(p, q, e)
    # 3.加密
    ciphertext = encryption(public_key, plaintext)
    print("加密后的数据:", ciphertext)
    # 4.解密
    plaintext1 = decrypt(private_key, ciphertext)
    # 5.这里解密之后的数据是二进制,还需要转换为原数据
    plaintext = ''.join([chr(i) for i in [int(b, 2) for b in plaintext1.split(' ')]])
    print("解密后转换的数据:", plaintext)

    test()

    其余函数代码如下:

    # 编写人:Jaoany
    # 开发时间:2021/7/21 10:39
    # : coding:UTF-8
    def ex_gcd(a, b):
    """扩展欧几里德算法"""
    if b == 0:
    return 1, 0
    else:
    q = a // b
    r = a % b
    s, t = ex_gcd(b, r)
    s, t = t, s-q*t
    return [s, t]


    # 快速幂算法
    def fast_expmod(a, e, n):
    """快速幂"""
    d = 1
    while e != 0:
    if(e & 1) == 1:
    d = (d * a) % n
    e >>= 1
    a = a * a % n
    return d


    def make_key(p, q, e):
    """
    生成公私钥
    参数1:大素数p
    参数2:大素数q
    参数3:随机生成e,满足 gcd(e,fin)
    返回值:[公钥,私钥]-------[[n,e],[n,d]]
    """
    n = p * q
    fin = (p-1) * (q-1)
    d = ex_gcd(e, fin)[0] # 辗转相除法求逆(广义欧几里得)
    while d < 0:
    d = (d+fin) % fin
    return [[n, e], [n, d]]


    def encryption(key, data):
    """
    加密
    参数1:列表[n,e]----公钥
    参数2:待价密数据
    返回值:密文
    """
    n, e = key
    plaintext = list(data)
    ciphertext = []
    for item in plaintext:
    ciphertext.append(fast_expmod(ord(item), e, n))
    return ciphertext


    def decrypt(key, ciphertext):
    """
    解密
    参数1:key为私钥
    参数2:密文数据
    返回值:明文
    """
    n, d = key
    plaintext = ''
    for item in ciphertext:
    plaintext += (chr(fast_expmod(item, d, n)))
    return plaintext


    def make_p_q_e():
    """
    返回值:[p,q,e]
    """
    p = 17
    q = 37
    # p = 33478071698956898786044169848212690817704794983713768568912431388982883793878002287614711652531743087737814467999489
    # q = 36746043666799590428244633799627952632279158164343087642676032283815739666511279233373417143396810270092798736308917
    e = 65537
    return [p, q, e]

    运行结果示例:

     

    
    

     




    本文来自博客园,作者:Jaoany,转载请注明原文链接:https://www.cnblogs.com/fanglijiao/p/15045171.html

  • 相关阅读:
    LeetCode24-Swap_Pairs
    LeeCode
    LeetCode3-Longest_Substring_Without_Repeating_Characters
    治愈 JavaScript 疲态的学习计划【转载】
    前端冷知识集锦[转载]
    知道这20个正则表达式,能让你少写1,000行代码[转载]
    关于简历和面试【整理自知乎】
    正念冥想方法
    一些职场经验【转载自知乎】
    犹太复国计划向世界展现了一个不一样的民族——观《犹太复国血泪史》有感
  • 原文地址:https://www.cnblogs.com/fanglijiao/p/15045171.html
Copyright © 2011-2022 走看看