RSA sign and verify

RSA的签名和验证是由标准文件 PKCS #1 v2.1: RSA Cryptography Standard 定义的

传统认为一段明文用RSA私钥加密后得到签名,签名用响应的公钥来验证。

这其中漏掉了一个最重要的环节,signature padding scheme.下面的图片描述了真实的应用中用到的RSA sign and verify.

 

PKCS 2.1在原来的1.5版本基础上添加了新的encrypt padding和 si'gnature padding

便于以后方便查找,下面附上一些原文:

The signature schemes RSASSA-PKCS-v1_5 ("PKCSV1_5") and RSASSA-PSS ("PSS") have differences. 

  • PKCSV1_5 is deterministic. The same message and key will produce an identical signature value each time. PSS is randomized and will produce a different signature value each time (unless you use a zero-length salt).
  • A PKCSV1_5 signature is complete in itself. Once decrypted using the private key, you can detect the hash function used to create it and extract the message digest value. A PSS signature has separate parameters (see below) which need to be known prior to verifying a signature. These are included in X.509 certificates and CMS signed-data objects, but need to be communicated separately for an isolated signature value.
  • You can extract the message digest value from a PKCSV1_5 signature. You cannot extract it from a PSS signature; you can only verify against a known digest value.
  • PSS has a security proof and is more robust in theory than PKCSV1_5. Nevertheless PKCSV1_5 has no known security weaknesses at this time.
  • PSS had patent issues until recently (the last one expired in 2010) and is less widely adopted. PKCSV1_5 has been widely used since the 1990s.

PKCS#1v1.5 padding scheme for SHA1:

 

 

The Hash Maigc Code:

### 实现RSA4096签名算法 #### 生成密钥对 为了创建安全的RSA4096签名,首先需要生成一对公私钥。这可以通过`Cryptography`库来完成。 ```python from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization private_key = rsa.generate_private_key( public_exponent=65537, key_size=4096 ) pem_private = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption() ) with open('private_key.pem', 'wb') as pem_out: pem_out.write(pem_private) public_key = private_key.public_key() pem_public = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) with open('public_key.pem', 'wb') as pem_out: pem_out.write(pem_public) ``` 这段代码展示了如何利用指定参数生成一个4096位长度的RSA私钥以及对应的公钥,并将其保存为PEM文件格式[^1]。 #### 创建消息摘要并签署数据 接着,在拥有有效的私钥之后,可以准备要发送的信息,并计算其哈希值作为消息摘要;随后使用私钥对该摘要进行加密操作即完成了电子签名过程。 ```python from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding message = b"A message I want to sign" signature = private_key.sign( message, padding.PSS( mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH ), hashes.SHA256() ) print(signature.hex()) ``` 这里采用了PSS填充模式配合SHA-256散列函数来进行签名处理[^2]。 #### 验证签名的有效性 接收方收到带有签名的消息后,则需通过公开可用的对应公钥验证该签名为真伪。 ```python try: public_key.verify( signature, message, padding.PSS( mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH ), hashes.SHA256() ) print("The signature is valid.") except Exception as e: print(f"The signature is not valid: {e}") ``` 上述脚本尝试用给定的公钥检验传入的数据及其关联的数字签名是否匹配,如果成功则表明信息未被篡改且确实来源于声称者[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值