rsa

用openssl库RSA加密解密

倖福魔咒の 提交于 2020-01-11 03:41:12
1 #include <stdio.h> 2 #include <openssl/rsa.h> 3 #include <openssl/pem.h> 4 #include <openssl/err.h> 5 6 //加密 7 int my_encrypt(const char *input, int input_len, char *output, int *output_len, const char *pri_key_fn) 8 { 9 RSA *p_rsa = NULL; 10 FILE *file = NULL; 11 int ret = 0; 12 13 if((file = fopen(pri_key_fn, "rb")) == NULL) 14 { 15 ret = -1; 16 goto End; 17 } 18 19 if((p_rsa = PEM_read_RSAPrivateKey(file, NULL,NULL,NULL )) == NULL) 20 { 21 ret = -2; 22 goto End; 23 } 24 25 if((*output_len = RSA_private_encrypt(input_len, (unsigned char*)input, (unsigned char*)output, p_rsa, RSA_PKCS1

支付宝RSA签名

天大地大妈咪最大 提交于 2020-01-10 15:40:50
1、参考网上相关文章,开放php中的openssl,但使用网上例子调用openssl_pkey_new,一直报100013错误。后改用用支付宝提供的SDKdemo程序 发现使用提供的privkye,可以生成签名串,但使用苹果电脑生成的key就不成功。 参考此文http://orangeholic.iteye.com/blog/2161771 PHP RSA加解密示例 ,在centos下 yum install openssl,再利用生成命令产生privkey和publickey能生成签名。 2、例子说明,参考支付宝官网文档https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1 服务端SDK 方法说明 加签方法(如果不用SDK调用,推荐用该方法加签) 1 2 3 4 5 6 /** @param params 参数列表 key-参数名称 value-参数值 @param privateKey 加签私钥 @param charset 加签字符集 **/ String AlipaySignature.rsaSign(Map<String, String> params, String privateKey, String charset) 验签方法(同步请求时,如果不用SDK调用

Decrypting “long” message encrypted with RSA java

不打扰是莪最后的温柔 提交于 2020-01-10 09:20:50
问题 Hi this is the same question, that was asked two years ago: Java/JCE: Decrypting “long” message encrypted with RSA I had a large byte array and rsa keypair, initiated by value 1024. Using rsa encryption and the specified size of the key is strong requirement, I can't change it. So I can't use symmetric encryption with asymetric encryption symmetric key. I can't use any other keys. I had a byte array and need ciphered byte array to be returned. I wonder if there is any ready tool, that can

iOS RSA公钥加密数据 服务端接受PHP私钥解密 反过服务端公钥加密数据 iOS端私钥解密数据

扶醉桌前 提交于 2020-01-10 01:56:36
*/ /*--> */ */ /*--> */ 加密传输的数据,采用 RSA算法,RSA是一种非对称算法,算法需要一对密钥,使用其中一个加密,需要使用另外一个才能解密。有可私钥可以直接生产公钥。 iOS SDK并不支持私钥加密,公钥解密。可以通过OPENSSL第三方库解决。 使用openssl命令行生成密钥对 openssl req -x509 - out public_key.der -outform der - new -newkey rsa: 1024 -keyout private_key.pem 根据提示,填入私钥的密码(代码中需要用到),签名证书的组织名、邮件等信息之后,就会生成包含有公钥的证书文件public_key.der合私钥文件private_key.pem。 public_key.der文件用于分发到iOS客户端进行公钥加解密,而private_key.pem文件留在服务器端供php使用。当然,为了在服务器端进行加解密测试,那么我们还可以生成一个服务器端PHP使用的pem公钥文件: openssl rsa - in private_key.pem -pubout - out public_key.pem 上面这个命令就会根据输入的私钥文件生成pem格式的公钥文件了。从这里也可以看到,根据私钥,我们是可以生成相对应的公钥的,这也就是为什么我们要把公钥放在客户端

openssl rsautl和openssl pkeyutl(文件的非对称加密)

坚强是说给别人听的谎言 提交于 2020-01-09 20:32:18
openssl系列文章: http://www.cnblogs.com/f-ck-need-u/p/7048359.html rsautl是rsa的工具,相当于rsa、dgst的部分功能集合,可用于 生成数字签名、验证数字签名、加密和解密文件 。 pkeyutl是非对称加密的通用工具,大体上和rsautl的用法差不多,所以此处只解释rsautl。 openssl rsautl [-in file] [-out file] [-inkey file] [-pubin] [-certin] [-passin arg] [-sign] [-verify] [-encrypt] [-decrypt] [-hexdump] openssl pkeyutl [-in file] [-out file] [-sigfile file] [-inkey file] [-passin arg] [-pubin] [-certin] [-sign] [-verify] [-encrypt] [-decrypt] [-hexdump] 共同的选项说明: -in file:指定输入文件 -out file:指定输出文件 -inkey file:指定密钥输入文件,默认是私钥文件,指定了"-pubin"则表示为公钥文件,使用"-certin"则表示为包含公钥的证书文件 -pubin:指定"-inkey

openssl RSA加密方法初识

那年仲夏 提交于 2020-01-09 18:34:38
作为非对称加密算法,有两对密钥 一般用法 加密结果=RSA_EN(数据,公钥); 解密结果=RSA_DE(数据,私钥); RSA填充 (RSA_public_encrypt和RSA_private_decrypt自动完成,不用操心,这里只做些说明) RSA也是对数据分块处理,一般有两种填充方法,当然也可以不填充。 RSA_PKCS1_PADDING:数据块长度(字节)至少比RSA_size(rsa)小11字节。 这里的RSA_size(rsa) = 密钥长度(字节) 例如128Byte(1024bit)的密钥,每个数据块的最大长度就是128-11=117, 然后,加密出来的数据也是128Byte 如果要加密的数据块小于117Byte,比如说64Byte,就要对明文进行填充方式: [0x02(表示采用公钥加密)][51个伪随机字节][0x00][数据块(64Byte)] 得到总长度117的数据块 如果采用私钥加密 [0x01(表示采用私钥加密)][51个0xff][0x00][数据块(64Byte)] 得到总长度117的数据块 接着填充分组(分组长度128Byte) [0x00][0x01][8个0xff][0x00][数据块(117Byte)] RSA_PKCS1_OAEP_PADDING:数据块长度(字节)至少比RSA_size(rsa)小41字节。 解释类似。 -_-

java 安全加密api(cipher,signature,mac...)

南笙酒味 提交于 2020-01-09 17:01:58
前言 本文主要讲解常用加密算法,消息摘要,二进制字符变换等的java实现,对于加密算法本身的原理只会做简单的介绍,详细的原理可百度。 相关概念 加密 加密是指将可读取的明文作为输入,通过特定的变换操作得到不易读取的输出(通常是二进制序列),目前常用的加密算法包括 对称加密的AES/DES,非对称加密的RSA/DSA/EC,加密很重要的一点就是解密,无论多复杂的数学变换,一定可以通过相应的逆变换得到原始输入,这是的加密行为才有意义。 hash(哈希变换) hash值又称散列值或者消息摘要,对输入的字符串或者二进制序列通过一定的变换得到固定长度的输出,它是一个不可逆的过程,理解这个不可逆的过程可以从数学中的求余函数理解,例如:11/10 = 1 … 1余数是1,以除以10作为变换,余数1作为输出,不可能通过余数是1得到被除数是11,因为有可能是21、31、41。。。。。。等等,同时和求余类似,hash碰撞指的就是不同的输入可能得到相同的输出。当然对于真正的hash变换,不可能像求余过程如此简单,但是这个不可逆过程的原理是类似的。常用的hash变换有MD5/SHA1/HmacSHA1/HmacMD5…等,hash变换的目的并不是让输入不可读取,而是让输入不可改变。 字节变换 文件通常会分为文本文件和二进制文件,文本文件通过(Unicode/UTF-8/ASCII)编码之后是可以读取的

Implementing RSA in C#

家住魔仙堡 提交于 2020-01-09 10:28:25
问题 I'm currently trying to implement a class to handle secure communications between instances of my app using RSACrytoServiceProveider class. First question : is it a good idea implement a single class to handle sender/reciever roles or should i split the roles into individual classes ?. This is what i have done so far: using System; using System.Text; using System.Security.Cryptography; namespace Agnus.Cipher { public class RSA { private byte[] plaintextBytes; private byte[] ciphertextBytes;

Java/JCE: Decrypting “long” message encrypted with RSA

半腔热情 提交于 2020-01-09 10:12:49
问题 I've got a message contained in an byte[], encrypted with "RSA/ECB/PKCS1Padding". To decrypt it I create a Cipher c and initiate it with c = Cipher.getInstance("RSA/ECB/PKCS1Padding"); Untill now I have only decrypted small messages, using the doFinal() method, returning an byte[] with the decrypted bytes. c.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptetBytes = c.doFinal(encryptedBytes); But in this case the data is bigger (approx 500 Bytes), and the doFinal() -method throws an

Java/JCE: Decrypting “long” message encrypted with RSA

ⅰ亾dé卋堺 提交于 2020-01-09 10:11:32
问题 I've got a message contained in an byte[], encrypted with "RSA/ECB/PKCS1Padding". To decrypt it I create a Cipher c and initiate it with c = Cipher.getInstance("RSA/ECB/PKCS1Padding"); Untill now I have only decrypted small messages, using the doFinal() method, returning an byte[] with the decrypted bytes. c.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptetBytes = c.doFinal(encryptedBytes); But in this case the data is bigger (approx 500 Bytes), and the doFinal() -method throws an