我有两个简短的脚本和一个(示例)私钥,它们产生的结果不同。
secret.key
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAxgVbpGebnqyq09U282gWcKqM/XXUqfszvaEoejmJQ0rS0Xx+
OvxU24aNbqB3G+TLIqzJp5O0Ei430kjZyWVvaIKbVr/PGpBBEiMvOANAQnFy3GOC
SQdpxFzE/2j1X2mvS/orhZ/lk7zabU3f4uI25XE90Y/hBZG/jKh9No0W/CZk6iE2
L0Fvy1JXhOSxoOORbIcMhCr9kaUJFXLx2kv3LbNIwP8a/xTszN9TvFKb61uYIpUk
JRuuXPcFREIZuu7tTMWdJI7QPf2LZOLNHkEydGIHM0jygREx9diG77BGiSYHWyxu
lodxN8dld3Ol/NEW5HCJbcGVqbf10HMXnSvJVQIDAQABAoIBAF5s4aVE7pU8ev0m
V9y7zq+M9ExMqssfiS2sspON6iFXha31MnBIMOK5h6mwBPwdHqx7PnGVYjGlLmMs
8AU0RGrOlOpJ7qUZEp5mfraz/ilw1be4D8FpMos7PYkZcKA90iRlt+kxNRU84d9r
Wg5jlo/UbwkGFeRE+tVE5uhjYQl9IlolWl/8HFAMRJj9Qkr3XLy2NXQqD1TfMQkA
KveHaJLhGDOCQ4nkrkDWBuI5qK/As26QBeZY5kKp/O48MHKdC52EkRMjDN1WYdL7
hmAKboN16zfh7j96HvZNYLIMA9IlMq5ngh2Uq15TiNR5ekWdE1zMN23wldLFIRJd
Z23o0qECgYEA7s6HJGiAgw46NnDTJL1KQLDubAtJ8DBhDNZIlysJKPz7d2jNTqkG
13MiX3IrjAdH52FvVtXCgryTjnGvIOxyFFqTPU3kN6gujqMGAR3J3vsoucJyzRQJ
AwR3/Be9yfQ4VF2V8KYfTkkuF+TkyVyZ2/+24v/kTruJMWYmd3sQNOkCgYEA1Eca
NWISu7OclZZeFVyPmB1wUnengmp16iyX1krPkC9HbSvGbb6vsFWALIHq4MsSkyns
qSxUjbGB2cKyke2cvGr3ULX43jppSzThO98rFL35OrdRJvqfhapTz/p0A/cdUbVx
EV10+0SF1HlVSthNPc+NkYn4WDIXs1sh7HYWXY0CgYEApe731IKQmX/vTxjCfgrR
Z3YJlSWa8LyNEwBqXC/ZI5P1n5lA32FvkZE81xlbzObaPZpkZPAPQgyKczXnPHdP
JXlySeyvoUTJZO8+ItRyCvB18e19G6bKREB8prxQcd2yrlyA7UBhDGKpFo1Ds+8W
MdnTWJP6LgL9Z0fVfrwSHNECgYEAhBn7ZV0y6zxLJYv94K1JbxBi5e8wfyhhWOmH
VAQaR1Ak0vt68LDKBKIwsYHn+Rxm4s4kSOMde1ALzsgq+EU2VMr8PW4BAcq32+V8
hRXMaYwPnUqXZfpxa20j2zmxGQafaEnRo0zVj8iEwEUBnyIcb2rKXtfNhJUnyqXb
5ptWL8ECgYEApsy8xdrXeKrd81CfWsBqoefC8/zx4Rj5CMLs2juE21TUG4B6mvOT
atxGV8ClpILPY6NVhCdhly8MAwkDxVSMfQ+xtDMAoW1QDkudx8ICsJRtckCwTyJI
D8WarpJvtYcPLvuBvfQgbAFhThS84yQmTiDMc185v/+P5X7Y7ooYpr4=
-----END RSA PRIVATE KEY-----
sign.py (https://repl.it/@DevinRodriguez1/JumboOlivedrabOs)
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from base64 import b64encode
digest = SHA256.new()
digest.update("This is a string!".encode())
key = open("secret.key", "r").read()
rsakey = RSA.importKey(key)
signer = PKCS1_v1_5.new(rsakey)
sign = signer.sign(digest)
signed = b64encode(sign)
print(signed.decode())
产生g8gdQsV1XNqTLSZ/cqo961DN//wfEEYMOjQDiFJ4tBfznBHPmun3y3opINVNJHBl48jxWmP42d4fxzLuF4OScwF6KV3aDjKZ8SUmjs8V/1Wp+2Oxh3ukeifMHgt0C18j7gc2E5JUblpRJc/pVt1PrD66iFwdYPWzrsZYbeV3xZtLk9smJIrS+AGHTYyoJRtRp4beK+Ir82HyB+sDNt5lQcrXYsj4lqU7yBfwmWlEHt/ptI1xisv1SHCnHFms6YpbVfxOhicHf5P+FOYrwSyQPQUTtgeswVYScnB8mmBUbH+e8j7Eh3k++lAfZY89l9xB7fQLpTrgylKFHGEGaS+l3Q==
sign.js (https://repl.it/@DevinRodriguez1/LooseWrithingStructures)
const crypto = require('crypto');
const readFileSync = require('fs').readFileSync;
let private_key = readFileSync('secret.key').toString();
let digest = crypto.createHash('SHA256').update("This is a string!");
let signed = crypto.createSign('SHA256').update(digest.digest()).sign(private_key, 'base64');
console.log(signed);
产生
pKJQTqOXJPK60xWzzMhR2h1FtLust7Hd5iy9FvlPUIPYrH0N6U+9xv+AnKSRDdaOcLeAfmpZN7Y2Zkvv15Ai0yB/D5K8LZFz9Jgk3w+Tim5+rIJX1590tAu9mWfeN0JCsSFCVYnrNLEKKKwBDpStnx9NKj+oKPs2lSbtUh8fOFHboleN8U0wAvF7rmlZn0W8v+rZ5HnwJ9pe4FxPLjznw8r7iTEh2z/r/6mUGDJNPS7U2i32fbsX9tOYaU7Ce09T29Mi7Wq3vnAnnr9LffpIBXMMgEIda3svURJODWAXIg5eAXJ0393C36qM3RMY68rWYdT1s9jYfzuELLNMJr1ENQ==
我已经尝试了NPM的许多模块,这些模块产生了相同的签名,但是没有运气匹配python的原始签名。我在这里想念什么?
谢谢。
答案 0 :(得分:0)
使用适当的填充方案(包括一些随机性)来实现安全的RSA加密。有关更多详细信息,请参见PKCS#1或OAEP。
RSA加密对填充有0和一串随机位的消息进行加密。在此过程中,通过加密哈希和XOR运算将随机字符串“隐藏”在密文中。解密时,RSA解密从密文中恢复随机字符串,并使用它来恢复消息。这就是为什么您对同一条短信收到不同结果的原因。