Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用RSA方法解密数据的问题_Python_Python 3.x_Python 2.7_Rsa_Ase - Fatal编程技术网

Python 用RSA方法解密数据的问题

Python 用RSA方法解密数据的问题,python,python-3.x,python-2.7,rsa,ase,Python,Python 3.x,Python 2.7,Rsa,Ase,有没有办法解决这个问题?即使代码在同一个文件中对数据进行了加密和解密,但问题发生在我将代码分为两部分时,一部分加密,另一部分解密,但我仍然得到了错误的解密数据,即使我使用相同的公钥,并且在加密部分生成了n值。 假设: data='hello' p=23 q=19 public key=(185,437) private key=(533,437) 当我使用公钥解密时,数据是错误的!!我也试过用私人的同样也错!!任何建议 加密代码: import random

有没有办法解决这个问题?即使代码在同一个文件中对数据进行了加密和解密,但问题发生在我将代码分为两部分时,一部分加密,另一部分解密,但我仍然得到了错误的解密数据,即使我使用相同的公钥,并且在加密部分生成了n值。 假设:

  data='hello'
    p=23
    q=19
    public key=(185,437)
    private key=(533,437)
当我使用公钥解密时,数据是错误的!!我也试过用私人的同样也错!!任何建议

加密代码:

import random

def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a


def multiplicative_inverse(e, phi):
    d = 0
    x1 = 0
    x2 = 1
    y1 = 1
    temp_phi = phi

    while e > 0:
        temp1 = temp_phi//e
        temp2 = temp_phi - temp1 * e
        temp_phi = e
        e = temp2

        x = x2- temp1* x1
        y = d - temp1 * y1

        x2 = x1
        x1 = x
        d = y1
        y1 = y

    if temp_phi == 1:
        return d + phi

'''
Tests to see if a number is prime.
'''
def is_prime(num):
    if num == 2:
        return True
    if num < 2 or num % 2 == 0:
        return False
    for n in range(3, int(num**0.5)+2, 2):
        if num % n == 0:
            return False
    return True

def generate_keypair(p, q):
    if not (is_prime(p) and is_prime(q)):
        raise ValueError('Both numbers must be prime.')
    elif p == q:
        raise ValueError('p and q cannot be equal')
    #n = pq
    n = p * q

    #Phi is the totient of n
    phi = (p-1) * (q-1)

    #Choose an integer e such that e and phi(n) are coprime
    e = random.randrange(1, phi)

    #Use Euclid's Algorithm to verify that e and phi(n) are comprime
    g = gcd(e, phi)
    while g != 1:
        e = random.randrange(1, phi)
        g = gcd(e, phi)

    #Use Extended Euclid's Algorithm to generate the private key
    d = multiplicative_inverse(e, phi)

    #Return public and private keypair
    #Public key is (e, n) and private key is (d, n)
    return ((e, n), (d, n))

def encrypt(pk, plaintext):
    #Unpack the key into it's components
    key, n = pk
    #Convert each letter in the plaintext to numbers based on the character using a^b mod m
    cipher = [(ord(char) ** key) % n for char in plaintext]
    #Return the array of bytes
    return cipher


if __name__ == '__main__':

    print ("RSA Encrypter/ Decrypter")
    p =  int(23)
    q = int(19)
    print ("Generating your public/private keypairs now . . .")
    public, private = generate_keypair(p, q)
    print ("Your public key is ", public ," and your private key is ", private)
    message = str('hello')
    encrypted_msg = encrypt(private, message)
    print ("Your encrypted message is: ")
    print (''.join(map(lambda x: str(x), encrypted_msg)))
    def decrypt(k,pk, ciphertext):
    #Unpack the key into its components
    key=k
    n = pk
    #Generate the plaintext based on the ciphertext and key using a^b mod m
    plain = [chr((ord(char) ** key)  % n) for char in ciphertext]
    return ''.join(plain)


if __name__ == '__main__':
    '''
    Detect if the script is being run directly by the user
    '''
    print ("RSA Encrypter/ Decrypter")
    key =  int(533)
    n = int(437)
    public=(key,n)
    message = '271169420420218'

    print ("Decrypting message with public key ", public ," . . .")
    print ("Your message is:")
    print (decrypt(key,n, message))

我正在使用python 3.6 spyder

@James Restore Monica Polk,它是python 3.6,我在python 2.7中也尝试过同样的解密问题,所以我转换为在python 3中工作。6@James恢复Monica Polk的身份,你能帮我让它在python 3中工作吗,因为当我创建2个文件时,我得到了错误的解密数据,一个用于加密,另一个用于解密,我已经将xrange更改为range以与python 3兼容,但解密数据仍然是错误的wrong@James恢复莫妮卡·波尔克的身份,我已经这么做了,但仍然得到了错误的解密数据你不能像现在这样对密文进行编码,当您准备解密时,您无法确定数字边界在哪里。你的解密必须撤销加密的每一个步骤,而不是。@James restore Monica Polk,这是python 3.6,我在python 2.7中也尝试过同样的解密问题,所以我转换为在python 3中工作。6@James恢复Monica Polk的身份,你能帮我让它在python 3中工作吗,因为当我创建2个文件时,我得到了错误的解密数据,一个用于加密,另一个用于解密,我已经将xrange更改为range以与python 3兼容,但解密数据仍然是错误的wrong@James恢复莫妮卡·波尔克的身份,我已经这么做了,但仍然得到了错误的解密数据你不能像现在这样对密文进行编码,当您准备解密时,您无法确定数字边界在哪里。而你的解密必须撤销加密的每一步。