SHA1WithRSA签名使用openssl 实现

来源:互联网 发布:渔民之家钓鱼源码 编辑:程序博客网 时间:2024/06/01 14:32

引言:
2017年就要到了,想想自己使用阿里云搭的博客 眼看就要到期了。 虽说没写几遍有质量的文章吧,但是放其不管也于心不忍。这几天就琢磨着把几遍有内容的转到博客中。在寻求着落点的时候发现Markdown。看了CSDN中Markdown的范文,再看看自己曾经写的文章。只能呵呵了
想着就借此机会学习一下Markdown的话语。


最近一个项目对接。要使用SHA1WithRSA签名验签。
之前接触过DES、3DES、 AES、 SHA1、 MD5、RSA 看这加密想当然的觉得是就是先对数据做个SHA1摘要再做个RSA加密嘛,简单不是。
man 了一下 openssl 关于RSA加解密。霹雳啪啦几个小时就把 理解的“SHA1WithRSA”实现了。
但是测试时对方就是验签不过。
后面还是问广大的网络。才让我得了解自己的无知。
这不也有人和犯一样的错 哈哈

简单阅读一下就能大概知道实际上可以通过openssl的RSA_sign实现
来看一下RSA_sign

#include <openssl/rsa.h> int RSA_sign(int type, const unsigned char *m, unsigned int m_len,    unsigned char *sigret, unsigned int *siglen, RSA *rsa); int RSA_verify(int type, const unsigned char *m, unsigned int m_len,    unsigned char *sigbuf, unsigned int siglen, RSA *rsa);

DESCRIPTION
RSA_sign() signs the message digest m of size m_len using the private key rsa as specified in PKCS #1 v2.0. It stores the signature in sigret and the signature size in siglen. sigret must point to RSA_size(rsa) bytes of memory. Note that PKCS #1 adds meta-data, placing limits on the size of the key that can be used. See RSA_private_encrypt for lower-level operations.
type denotes the message digest algorithm that was used to generate m. If type is NID_md5_sha1, an SSL signature (MD5 and SHA1 message digests with PKCS #1 padding and no algorithm identifier) is created.
RSA_verify() verifies that the signature sigbuf of size siglen matches a given message digest m of size m_len. type denotes the message digest algorithm that was used to generate the signature. rsais the signer’s public key.
RETURN VALUES
RSA_sign() returns 1 on success. RSA_verify() returns 1 on successful verification.

简单看看,其实RSA_sing()函数第一为送NID_sha1

通过这篇文章知道需要签名的是SHA1摘要而非所有报文。所以要先对明文数据做SHA1。再调用 RSA_sign()签名。

结论:

对openSSL源码一知半解的我,竟把RSA加密|解密和RSA签名|验证混淆。
最后 私钥加密 ≠ 签名

1 0