RSA-2048 解密不工作 - PKCS1_OAEP "Incorrect decryption."
RSA-2048 decrypt not working - PKCS1_OAEP "Incorrect decryption."
该代码应该设置一个服务来侦听来自投票网站的连接。它是一个服务器,用于侦听来自外部(客户端)的连接。当客户端连接时,客户端等待发送版本号。一旦我的 server/listener 发送了版本号,客户端就会响应一个 256 字节的块,该块使用我提供的 public 密钥进行 RSA 2048 位加密。然后必须对该块进行解码,然后(稍后)我将读取内容。我陷入无法解密的困境:
我明白了:
starting connection...
connection from ('50.28.6.244', 35338)
sending version number...
receiving encrypted block
Traceback (most recent call last):
File "voteListener.py", line 97, in
Main(private_key)
File "voteListener.py", line 49, in Main
decodedfile = decode_msg(data, privatekey)
File "voteListener.py", line 58, in decode_msg
ciphertext = cipher.decrypt(msg)
File "C:\Python27\lib\site-packages\Crypto\Cipher\PKCS1_OAEP.py", line 227, in decrypt
raise ValueError("Incorrect decryption.")
ValueError: Incorrect decryption.
C:\Users\STEXAS\Desktop\vote>pause
Press any key to continue . . .
我的代码:
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!")
客户(我想 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
该代码应该设置一个服务来侦听来自投票网站的连接。它是一个服务器,用于侦听来自外部(客户端)的连接。当客户端连接时,客户端等待发送版本号。一旦我的 server/listener 发送了版本号,客户端就会响应一个 256 字节的块,该块使用我提供的 public 密钥进行 RSA 2048 位加密。然后必须对该块进行解码,然后(稍后)我将读取内容。我陷入无法解密的困境:
我明白了:
starting connection...
connection from ('50.28.6.244', 35338)
sending version number...
receiving encrypted block
Traceback (most recent call last):
File "voteListener.py", line 97, in
Main(private_key)
File "voteListener.py", line 49, in Main
decodedfile = decode_msg(data, privatekey)
File "voteListener.py", line 58, in decode_msg
ciphertext = cipher.decrypt(msg)
File "C:\Python27\lib\site-packages\Crypto\Cipher\PKCS1_OAEP.py", line 227, in decrypt
raise ValueError("Incorrect decryption.")
ValueError: Incorrect decryption.
C:\Users\STEXAS\Desktop\vote>pause
Press any key to continue . . .
我的代码:
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!")
客户(我想 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