Python-RSA(公私钥制作,加解密,签名)

  • Signing data with the RSA algorithm

  • Step1. Create private/public keypair (optional)

openssl genrsa -out private.pem 1024  >private.pem

This creates a key file called private.pem. This file actually have both the private and public keys, so you should extract the public one from this file:

openssl rsa -in private.pem -out public.pem -outform PEM -pubout   >public.pem

You‘ll now have public.pem containing just your public key, you can freely share this with 3rd parties.

  • Step2. Create a hash of the data

echo ‘data to sign‘     > data.txt
openssl dgst -md5 data.txt    >data‘s md5 code
  • Step3. Sign the hash using the private key

openssl rsautl -sign -inkey private.pem -keyform PEM  -md5 -out data.sign  data.txt  > signature

The file ‘signature‘ and the actual data ‘data.txt‘ can now be communicated to the receiving end. The hash algorithm (in our case md5) as well as the public key must also be known to the receiving end.

  • Authenticate data using the public key

  • Step4. Create a hash of the data (same as Step 2)

  • Step5. Verify the signature

openssl rsautl -verify -inkey public.pem -keyform PEM -pubin -md5 -signature -signature data.sign data.txt  > verified
diff -s verified hash

If the result of the above command ‘verified‘ matches the hash generated in Step 3.1 (in which case you the result of the diff command would be ‘Files verified and hash are identical‘) then the signature is considered authentic and the integrity/authenticity of the data is proven.

时间: 2024-08-06 22:13:47

Python-RSA(公私钥制作,加解密,签名)的相关文章

Python rsa公私钥生成 rsa公钥加密(分段加密)私钥加签实战

一般现在的SAAS服务提供现在的sdk或api对接服务都涉及到一个身份验证和数据加密的问题.一般现在普遍的做法就是配置使用非对称加密的方式来解决这个问题,你持有SAAS公司的公钥,SAAS公司持有你的公钥,你们就可以进行加密和签名的验证了. 先来看下两种在linux或者mac下面生成key pair的方法: 使用openssl 生成一把2048bit长度的钥匙对,首先我们生成一把.pem格式的私钥: openssl genrsa -out private_key.pem 2048 然后通过这把私

.net core中使用openssl的公钥私钥进行加解密

这篇博文分享的是 C#中使用OpenSSL的公钥加密/私钥解密 一文中的解决方法在 .net core 中的改进.之前的博文针对的是 .NET Framework ,加解密用的是 RSACryptoServiceProvider .虽然在 corefx(.NET Core Framework) 中也有 RSACryptoServiceProvider ,但它目前只支持 Windows ,不能跨平台. 之前的 new RSACryptoServiceProvider(); 代码在 mac 上运行,

java生成RSA公私钥字符串,简单易懂

java生成RSA公私钥字符串,简单易懂 解决方法: 1.下载bcprov-jdk16-140.jar包,参考:http://www.yayihouse.com/yayishuwu/chapter/1537 2.java代码 KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance("RSA"); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPa

Java中使用OpenSSL生成的RSA公私钥进行数据加解密

RSA是什么:RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的公钥加密算法,它能够 抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准.目前该加密方式广泛用于网上银行.数字签名等场合.RSA算法基于一个十分简单的 数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥. OpenSSL是什

Java实现RSA密钥对并在加解密、加签验签中应用的实例

一.项目结构 二.代码具体实现 1.密钥对生成的两种方式:一种生成公钥私文件,一种生成公钥私串 KeyPairGenUtil.java package com.wangjinxiang.genkey.util; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.security.Key; import java.security.KeyPair; import java.security

Java中使用OpenSSL生成的RSA公私钥

RSA是什么:RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准.目前该加密方式广泛用于网上银行.数字签名等场合.RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥. OpenSSL是什么:众

RSA 公私钥 互换问题

关于 RSA,我的理解是: 按定义的话,公私钥是可以互换的 问题是常见的实现里面,保存“私钥”的那个数据结构或者文件,里面除了私钥所必需的那一对数之外,还有额外的信息(足以算出公钥来),所以绝对不能把这个“私钥”数据给其他人…… n=pq φ(n)=(p-1)(q-1) m^φ(n) ≡ 1 mod n 令 ed ≡ 1 mod φ(n) cipher=m^e mod n plain = cipher ^ d mod n ≡ m^ed mod n = m 理论上e与d是可以互换的,但是一般公钥指

iOS RSA加解密签名和验证

转自:http://www.jianshu.com/p/81b0b54436b8 Pre:在公司负责了一个项目,需要用到iOS RSA验证签名的功能.后台给我的仅仅是一个公钥的字符串.经过起初的一段时间的挣扎,发现远远没有那么简单.iOS RSA是需要证书的,而java的后台只能给我一个公钥字符串.搜索了无数网页还是没找到能用的成型的代码.最后还是参考了下支付宝的SDK的签名机制,明白可以先把公钥字符串写入文件然后读取文件得到openssl 中的RSA结构体指针.现在精心整理了下iOS RSA使

加解密/签名验签

一.我加密.签名的过程 1,生成18位随机密钥:rand 067870-544583-448433: 2,使用对方的公钥证书(cer文件),并使用“RSA”算法,对随机密钥的字节数组加密,得到一个字节数组,将其转化为小写的十六进制字符串:secretKey 07ed7542951980385e32c149039255aabf8009e1e98a9432db19755e45e07361d318b98393d431cad4f656df7bc254548bacc92c53aa9566fd9ca1105