RSA-2048解密不起作用 - PKCS1_OAEP"解密不正确。"

时间:2015-09-15 04:42:35

标签: python-2.7 rsa pycrypto

代码应该设置一个侦听来自投票网站的连接的服务。它是一个服务器,用于侦听来自外部(客户端)的连接。客户端连接时,客户端等待发送版本号。一旦我的服务器/监听器发送版本号,客户端就会响应一个256字节的块,该块使用我提供的公钥进行RSA 2048位加密。然后必须解码该块,然后(稍后)我将读取内容。我陷入困境,因为我无法解密它:

我明白了:

  

开始连接......

     

来自(' 50.28.6.244',35338)

的连接      

发送版本号...

     

接收加密块

     

追踪(最近一次呼叫最后一次):

     

文件" voteListener.py",第97行,

     

主(PRIVATE_KEY)

     

文件" voteListener.py",第49行,在主

中      

decodingfile = decode_msg(data,privatekey)

     

文件" voteListener.py",第58行,在decode_msg中

     

ciphertext = cipher.decrypt(msg)

     

文件" C:\ Python27 \ lib \ site-packages \ Crypto \ Cipher \ PKCS1_OAEP.py",第227行,解密

     

引发ValueError("解密错误。")

     

ValueError:解密不正确。

     

C:\用户\ STEXAS \桌面\表决>暂停

     

按任意键继续。 。

我的代码:

import socket
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from os import path


def Main(privatekey):
    host = "0.0.0.0"
    port = 8192
    version = "VOTIFIER 1.9"
    print("starting connection...")
    while True:
        s = socket.socket()
        s.bind((host, port))
        s.listen(1)
        c, addr = s.accept()
        print("connection from %s" % str(addr))
        print("sending version number...")
        c.send(version)
        c.send('\n')

        print("receiving encrypted block")
        data = c.recv(256)
        c.close()
        s.close()
        decodedfile = decode_msg(data, privatekey)

        with open("votes.txt", 'wb') as f:
            f.write(decodedfile)
        print("File writen")


def decode_msg(ciphertext, priv_key):
    cipher = PKCS1_OAEP.new(priv_key)
    msg = cipher.decrypt(ciphertext)
    return msg


def read_private_key():
    with open("keys\mykey.pem", 'rb') as f:
        data = f.read()
    key = RSA.importKey(data)
    return key


def generate_key_pair():
    """Generates a 2048 bit RSA key pair and saves the keys to disk"""
    pair = RSA.generate(2048)

    f = open("keys\mykey.pem", "wb")  # private key
    f.write(pair.exportKey('PEM'))
    f.close()

    pub_key = pair.publickey().exportKey(format='PEM')
    keytext = str(pub_key).strip("-----BEGIN PUBLIC KEY-----").strip("-----END PUBLIC KEY-----").replace('\n', "")
    with open("keys\public.txt", 'wb') as f:  # the plain text public key for providing to server list
        f.write(keytext)
    with open("keys\public.pem", 'wb') as f:  # public key
        f.write(pub_key)


if __name__ == "__main__":
    private_key = None

    if not path.exists("keys\mykey.pem"):
        generate_key_pair()

    if path.exists("keys\mykey.pem"):
        private_key = read_private_key()

    if private_key is not None:
        Main(private_key)
    else:
        print("Error with Keys... no key was generated or found!")

1 个答案:

答案 0 :(得分:3)

客户端(我认为Votifier的Java版本)正在使用RSAES PKCS1 v1.5。添加了使用该方案进行解码的方法,并将调用更改为该方法:

在Main()中:

decodedfile = decode_msg_v1_5(data, privatekey)

新方法:

def decode_msg_v1_5(ciphertext, privateKey):
    """  Should consider using the more robust PKCS1 OAEP. """
    sentinel = Random.new().read(256)      # data length is 256
    cipher = PKCS1_v1_5.new(privateKey)
    messagereceived = cipher.decrypt(ciphertext, sentinel)
    return messagereceived
相关问题