Javascript 和 Python 之间的 RSA 通信 [英] RSA communication between Javascript and Python

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

问题描述

我正在开发一个原型,因此它需要在 Chrome 扩展程序和 Python 服务器之间使用 RSA.

I am working on a prototype, so it needs to use RSA between a Chrome Extension and a Python Server.

到目前为止,我计划使用 https://sourceforge.net/projects/pidcrypt/https://www.dlitz.net/.但是,虽然我可以按照文档进行解密和加密工作,但我无法解密彼此的消息.

So far I was planning on using https://sourceforge.net/projects/pidcrypt/ and https://www.dlitz.net/. However, while I can get decrypt and encrypt to work as per the documentation, I cannot get one to decrypt each other's message.

有人可以建议互操作的库,或者让我知道我是否对这些库做错了吗?

Can someone please, either suggest libraries that interoperate or let me know if I am doing something wrong with this libraries?

根据我的研究,pidder 使用 RSA PKCS#1 加密样式填充(类型 2).通过谷歌搜索,我发现它是 PyCrypto 调用 PKCS1_OAEP 的类型.我不太确定,但我已经尝试过标准和其他两个.

From what I worked out, pidder uses RSA PKCS#1 encryption-style padding (type 2). From googling, I sort of worked out that it is the type that PyCrypto calls PKCS1_OAEP. I am not too sure, but I have tried the standard and the other one two.

非常感谢您的帮助!

推荐答案

Javascript 库 (pidCrypt) 使用 PKCS#1 v1.5 进行 RSA 加密,而不是 OAEP.

The Javascript library (pidCrypt) uses PKCS#1 v1.5 for RSA encryption, not OAEP.

这是由 PyCrypto 支持的(参见 此处).这是加密示例:

That is supported by PyCrypto (see here). This is the example for encryption:

from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA

message = 'To be encrypted'
h = SHA.new(message)

key = RSA.importKey(open('pubkey.der').read())
cipher = PKCS1_v1_5.new(key)
ciphertext = cipher.encrypt(message+h.digest())

和解密:

from Crypto.Hash import SHA
from Crypto import Random

key = RSA.importKey(open('privkey.der').read())

dsize = SHA.digest_size
sentinel = Random.new().read(15+dsize)      # Let's assume that average data length is 15

cipher = PKCS1_v1_5.new(key)
message = cipher.decrypt(ciphertext, sentinel)

digest = SHA.new(message[:-dsize]).digest()
if digest==message[-dsize:]:                # Note how we DO NOT look for the sentinel
     print "Encryption was correct."
else:
     print "Encryption was not correct."

请注意,PKCS#1 v1.5 加密方案已知严重被破坏.

Note that PKCS#1 v1.5 encryption scheme is know to be badly broken.

这篇关于Javascript 和 Python 之间的 RSA 通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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