Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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_Encryption_Cryptography_Pycrypto - Fatal编程技术网

Python 解码用RSA编码的文本

Python 解码用RSA编码的文本,python,encryption,cryptography,pycrypto,Python,Encryption,Cryptography,Pycrypto,我有RSA公钥/私钥对和密码短语。我正在尝试解码使用上述密钥加密的文本。编码文本的长度始终为512个字符的alpha-num字符串 我已尝试使用SOF问题中提供的代码 首先,我使用了我的私钥,它是用PEM文件中的AES-256-CBC编码的。 这是privkey.pem的开始,它让我觉得它的AES-256是加密的 -----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: AES-256-CBC <rest of the

我有RSA公钥/私钥对和密码短语。我正在尝试解码使用上述密钥加密的文本。编码文本的长度始终为512个字符的alpha-num字符串

我已尝试使用SOF问题中提供的代码

首先,我使用了我的私钥,它是用PEM文件中的AES-256-CBC编码的。 这是privkey.pem的开始,它让我觉得它的AES-256是加密的

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC
<rest of the data>
-----END RSA PRIVATE KEY-----
所以我向消息来源询问一个没有AES加密的私钥,他们给了我。现在使用这个密钥,解密后的文本如下所示(我只展示了部分文本)

请注意,我必须使用下面的代码将我的私钥从PEM转换为DER,因为使用PEM文件我在解析时得到了
SyntaxError:unexpected EOF

openssl rsa -in private_key.pem -out private_key.der -outform DER

因为这是我找到的解决办法

首先,我使用librray而不是pycrypto

下面是我的编码和解码功能

from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA

def encode_rsa(message, key_path):    
    key = RSA.importKey(open(key_path).read())
    cipher = PKCS1_OAEP.new(key)
    ciphertext = cipher.encrypt(message)
    return ciphertext

def decode_rsa(ciphertext, key_path):
    key = RSA.importKey(open(key_path).read())
    cipher = PKCS1_OAEP.new(key)
    # before decrypt convert the hex string to byte_array 
    message = cipher.decrypt(bytearray.fromhex(ciphertext))
    return message

使用上述两个函数,我能够正确地对数据进行编码/解码。

我将此主题选为非主题,因为除了填充的概念外,根本没有足够的信息继续。尝试使用测试密钥并在此处发布所需信息。我不确定您需要哪些额外信息。我的密钥是使用
openssl
创建的,它们已经在使用中,因此我无法通过重新创建来更改它们。我得到了一个public_key.txt、private_key.pem(AES-256加密)和密码短语。如何使用python使用这些密钥对文本进行解密?我使用.net c#代码对文本进行了加密,并希望使用python对代码进行解密,但在哪里可以获得RSA.importKey??
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
import ast

encrypted_text = "39085fc25e<HIDDEN>2fcce845760391ff"

key = RSA.importKey(open("\\path_to_key\\private.der", encoding="utf8").read())
cipher = PKCS1_OAEP.new(key)

message = cipher.decrypt(ast.literal_eval(str(uid)))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte
openssl rsa -in private_key.pem -out private_key.der -outform DER
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA

def encode_rsa(message, key_path):    
    key = RSA.importKey(open(key_path).read())
    cipher = PKCS1_OAEP.new(key)
    ciphertext = cipher.encrypt(message)
    return ciphertext

def decode_rsa(ciphertext, key_path):
    key = RSA.importKey(open(key_path).read())
    cipher = PKCS1_OAEP.new(key)
    # before decrypt convert the hex string to byte_array 
    message = cipher.decrypt(bytearray.fromhex(ciphertext))
    return message