互操作Python JavaScript RSA加密
在服务器上使用PyCrypto的Python,在客户端使用Tom Wu的JavaScript库。 我可以在服务器上用Python加密和解密,在客户端用JavaScript加密和解密。但是我不能在客户端加密,在服务器上解密。 这个问题已经在前面讨论过了,但还没有解决方案 在Python中:互操作Python JavaScript RSA加密,javascript,python,encryption,rsa,transferable,Javascript,Python,Encryption,Rsa,Transferable,在服务器上使用PyCrypto的Python,在客户端使用Tom Wu的JavaScript库。 我可以在服务器上用Python加密和解密,在客户端用JavaScript加密和解密。但是我不能在客户端加密,在服务器上解密。 这个问题已经在前面讨论过了,但还没有解决方案 在Python中: key = RSA.generate(2048, e=65537) pri_key = key.exportKey() n = key.n session['S_publicKey'] =
key = RSA.generate(2048, e=65537)
pri_key = key.exportKey()
n = key.n
session['S_publicKey'] = n
session['S_privateKey'] = pri_key
publicKey_IntN = session.get('S_publicKey')
publicKey_HexN = hex(publicKey_IntN)[2:].rstrip("L")
render_template('Shop.html', t1=publicKey_HexN )
jsonrsa = request.form['messageJSON']
jsonrsa = codecs.decode( jsonrsa, 'hex' )
#jsonrsa = jsonrsa.encode('utf-8') # in Python 3, must pass bytes
privateKey = session.get('S_privateKey')
jsonStr = decrypt_string(jsonrsa,privateKey) # ERROR invalid decryption
def decrypt_string(encrypted,private_key):
rsakey = RSA.importKey(private_key)
rsakey = PKCS1_OAEP.new(rsakey)
chunk_size = 256
offset = 0
decrypted = ""
while offset < len(encrypted):
decrypted += rsakey.decrypt(encrypted[offset:offset+chunk_size])
offset += chunk_size
return decrypted
在Shop.html(JavaScript)中:
在Python中:
key = RSA.generate(2048, e=65537)
pri_key = key.exportKey()
n = key.n
session['S_publicKey'] = n
session['S_privateKey'] = pri_key
publicKey_IntN = session.get('S_publicKey')
publicKey_HexN = hex(publicKey_IntN)[2:].rstrip("L")
render_template('Shop.html', t1=publicKey_HexN )
jsonrsa = request.form['messageJSON']
jsonrsa = codecs.decode( jsonrsa, 'hex' )
#jsonrsa = jsonrsa.encode('utf-8') # in Python 3, must pass bytes
privateKey = session.get('S_privateKey')
jsonStr = decrypt_string(jsonrsa,privateKey) # ERROR invalid decryption
def decrypt_string(encrypted,private_key):
rsakey = RSA.importKey(private_key)
rsakey = PKCS1_OAEP.new(rsakey)
chunk_size = 256
offset = 0
decrypted = ""
while offset < len(encrypted):
decrypted += rsakey.decrypt(encrypted[offset:offset+chunk_size])
offset += chunk_size
return decrypted
jsonrsa=request.form['messageJSON']
jsonrsa=codecs.decode(jsonrsa,'hex')
#jsonrsa=jsonrsa.encode('utf-8')#在Python 3中,必须传递字节
privateKey=session.get('S_privateKey')
jsonStr=decrypt_字符串(jsonrsa,privateKey)#错误解密无效
def解密_字符串(加密、私钥):
rsakey=RSA.importKey(私钥)
rsakey=PKCS1_OAEP.new(rsakey)
区块大小=256
偏移量=0
解密=“”
当偏移量
调用编解码器。解码
然后忽略结果没有任何作用。因此,在Python中,您没有将输入视为十六进制,只需将每个十六进制数字转换为其ASCII字节值。更改为,jsonrsa=codecs.decode(jsonrsa,'hex'),但仍然是相同的错误。因为您没有告诉我们这是什么错误,所以这不是很有帮助。但即使是相同的错误类型,我怀疑这是同一个实际问题。是的,这是相同的错误,无效的解密。