C和巨蟒之间的RSA加密/解密 [英] RSA encrypt/decrypt between C and python

查看:162
本文介绍了C和巨蟒之间的RSA加密/解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有服务器 C 写在蟒蛇和客户端。他们的工作是从服务器发送一个秘密的消息,这是与加密的RSA私钥客户端。我使用的OpenSSL / rsa.h 库,这是我初始化 RSA 对象与私钥和一个encrypte与 RSA_public_encrypt消息(length_of_message,秘密信息,来,RSA,RSA_PKCS1_PADDING)。然后,我发送此加密邮件到蟒蛇服务器并尝试为与Crypto.PublicKey导入相同的私钥解密RSA 库。问题是,它不能正确解密。它总是输出的秘诀在哪里消息是随机摆在那128位长度的消息(如:'\\ X23 \\ xa3x \\ 43 ...秘密信息\\ XEF \\ X4A') ,它应该正常返回刚秘密信息

I have server written in python and client in C . Their job is to send a secret message from server to client which is encrypted with RSA private key. I am using openssl/rsa.h library, that is I initialize a rsa object with a private key and encrypte a message with RSA_public_encrypt(length_of_message, "Secret Message", to, rsa, RSA_PKCS1_PADDING) . Then I send this encrypted message to python server and try to decrypt it with same private key using from Crypto.PublicKey import RSA library. Problem is that it does not decrypt it properly. It always outputs 128-bit length message where the secret message is randomly placed in it (e.g. '\x23\xa3x\43...Secret Message\xef\x4a'), where it should normally return just Secret Message.

推荐答案

问题是关于填充。 Python的RSA模块进行解密 PKCS1 结果填充和不填充去除。下面我从拍摄的功能这里问题解决了:

The problem was about the padding. Python's rsa module decrypts result with PKCS1 padding and does not removes padding. With the function below which I have taken from here problem was solved:

def pkcs1_unpad(text):
if len(text) > 0 and text[0] == '\x02':
    # Find end of padding marked by nul
    pos = text.find('\x00')
    if pos > 0:
        return text[pos+1:]
return None

这篇关于C和巨蟒之间的RSA加密/解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆