关于go语言的RSA加解密的介绍,这里有一篇文章,已经介绍的很完整了。

对应的go语言的加解密代码,参考git

因为原文跨语言是跟php,我这里要跟c语言进行交互,所以,这里贴上c语言的例子。

参考原文:http://hayageek.com/rsa-encryption-decryption-openssl-c/

#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/buffer.h> #include <stdio.h>
#include <stdint.h> //编译
//gcc rsa_example.c -o example -L/usr/local/Cellar/openssl/1.0.2e/lib/ -lssl -lcrypto //openssl的base64编解码: https://gist.github.com/barrysteyn/7308212
int Base64Encode(const unsigned char* buffer, size_t length, char** b64text) {
BIO *bio, *b64;
BUF_MEM *bufferPtr; b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
BIO_write(bio, buffer, length);
BIO_flush(bio);
BIO_get_mem_ptr(bio, &bufferPtr);
BIO_set_close(bio, BIO_NOCLOSE);
BIO_free_all(bio); *b64text=(*bufferPtr).data; return (); //success
} int padding = RSA_PKCS1_PADDING; RSA * createRSA(unsigned char * key,int public)
{
RSA *rsa= NULL;
BIO *keybio ;
keybio = BIO_new_mem_buf(key, -);
if (keybio==NULL)
{
printf( "Failed to create key BIO");
return ;
}
if(public)
{
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL);
}
else
{
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL);
}
if(rsa == NULL)
{
printf( "Failed to create RSA");
} return rsa;
} int public_encrypt(unsigned char * data,int data_len,unsigned char * key, unsigned char *encrypted)
{
RSA * rsa = createRSA(key,);
int result = RSA_public_encrypt(data_len,data,encrypted,rsa,padding);
return result;
}
int private_decrypt(unsigned char * enc_data,int data_len,unsigned char * key, unsigned char *decrypted)
{
RSA * rsa = createRSA(key,);
int result = RSA_private_decrypt(data_len,enc_data,decrypted,rsa,padding);
return result;
} int private_encrypt(unsigned char * data,int data_len,unsigned char * key, unsigned char *encrypted)
{
RSA * rsa = createRSA(key,);
int result = RSA_private_encrypt(data_len,data,encrypted,rsa,padding);
return result;
}
int public_decrypt(unsigned char * enc_data,int data_len,unsigned char * key, unsigned char *decrypted)
{
RSA * rsa = createRSA(key,);
int result = RSA_public_decrypt(data_len,enc_data,decrypted,rsa,padding);
return result;
} void printLastError(char *msg)
{
char * err = malloc();;
ERR_load_crypto_strings();
ERR_error_string(ERR_get_error(), err);
printf("%s ERROR: %s\n",msg, err);
free(err);
} int main(){ char plainText[/] = ""; //原始数据为字符串1024;key length : 2048, 数据最长为 keylen/8个char char publicKey[]="-----BEGIN PUBLIC KEY-----\n"\
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy8Dbv8prpJ/0kKhlGeJY\n"\
"ozo2t60EG8L0561g13R29LvMR5hyvGZlGJpmn65+A4xHXInJYiPuKzrKUnApeLZ+\n"\
"vw1HocOAZtWK0z3r26uA8kQYOKX9Qt/DbCdvsF9wF8gRK0ptx9M6R13NvBxvVQAp\n"\
"fc9jB9nTzphOgM4JiEYvlV8FLhg9yZovMYd6Wwf3aoXK891VQxTr/kQYoq1Yp+68\n"\
"i6T4nNq7NWC+UNVjQHxNQMQMzU6lWCX8zyg3yH88OAQkUXIXKfQ+NkvYQ1cxaMoV\n"\
"PpY72+eVthKzpMeyHkBn7ciumk5qgLTEJAfWZpe4f4eFZj/Rc8Y8Jj2IS5kVPjUy\n"\
"wQIDAQAB\n"\
"-----END PUBLIC KEY-----\n"; char privateKey[]="-----BEGIN RSA PRIVATE KEY-----\n"\
"MIIEowIBAAKCAQEAy8Dbv8prpJ/0kKhlGeJYozo2t60EG8L0561g13R29LvMR5hy\n"\
"vGZlGJpmn65+A4xHXInJYiPuKzrKUnApeLZ+vw1HocOAZtWK0z3r26uA8kQYOKX9\n"\
"Qt/DbCdvsF9wF8gRK0ptx9M6R13NvB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FwgZYxr8fGMoG\n"\
"PxQ1VK9DxCvZL4tRpVaU5Rmknud9hg9DQG6xIbgIDR+f79sb8QjYWmcFGc1SyWOA\n"\
"SkjlykZ2yt4xnqi3BfiD9QKBgGqLgRYXmXp1QoVIBRaWUi55nzHg1XbkWZqPXvz1\n"\
"I3uMLv1jLjJlHk3euKqTPmC05HoApKwSHeA0/gOBmg404xyAYJTDcCidTg6hlF96\n"\
"ZBja3xApZuxqM62F6dV4FQqzFX0WWhWp5n301N33r0qR6FumMKJzmVJ1TA8tmzEF\n"\
"yINRAoGBAJqioYs8rK6eXzA8ywYLjqTLu/yQSLBn/4ta36K8DyCoLNlNxSuox+A5\n"\
"w6z2vEfRVQDq4Hm4vBzjdi3QfYLNkTiTqLcvgWZ+eX44ogXtdTDO7c+GeMKWz4XX\n"\
"uJSUVL5+CVjKLjZEJ6Qc2WZLl94xSwL71E41H4YciVnSCQxVc4Jw\n"\
"-----END RSA PRIVATE KEY-----\n"; unsigned char encrypted[]={};
unsigned char decrypted[]={}; int encrypted_length= public_encrypt(plainText,strlen(plainText),publicKey,encrypted);
if(encrypted_length == -)
{
printLastError("Public Encrypt failed ");
exit();
}
printf("Encrypted length =%d\n",encrypted_length); char *b64text; Base64Encode( encrypted, encrypted_length, &b64text);
printf("Base64Encode = %s\n", b64text); int decrypted_length = private_decrypt(encrypted,encrypted_length,privateKey, decrypted);
if(decrypted_length == -)
{
printLastError("Private Decrypt failed ");
exit();
}
printf("Decrypted Text =%s\n",decrypted);
printf("Decrypted Length =%d\n",decrypted_length); encrypted_length= private_encrypt(plainText,strlen(plainText),privateKey,encrypted);
if(encrypted_length == -)
{
printLastError("Private Encrypt failed");
exit();
}
printf("Encrypted length =%d\n",encrypted_length); decrypted_length = public_decrypt(encrypted,encrypted_length,publicKey, decrypted);
if(decrypted_length == -)
{
printLastError("Public Decrypt failed");
exit();
}
printf("Decrypted Text =%s\n",decrypted);
printf("Decrypted Length =%d\n",decrypted_length); }

编译执行之后,可以得到加密之后的base64数据:

./example
Encrypted length =
Base64Encode = BXFuS6snRRhYUQhTYKG00UoGMzaptbZpaw7h+JEA+yP1sEd5dXaBEjdzzZ8jXRVfjigYFj41zEBSDsafxO440+vi/aODOAY1f4Q2dKf5eBLnXDyN9uT6uxCp67XgZC3YkN+iufMOKIEjSbb361kZrRLwl+6OUt4EYvkhmKl5YpklM+qQYyGW5xyCU5MYMUTXJoAthRTeNuoloSiZDQWKPH4i7bVfR/XDjXIA3iNWfoy5552L4AdtyRhRNvZ0RerbVtPqdWABvJX39hbskmGuzY9HtoxOQaoD0IWGHx29VmCM3JRrTvStnEKjYnUNT9ikbeK1gXX5JjU6HmLHbgPKUg==
Decrypted Text =
Decrypted Length =
Encrypted length =
Decrypted Text =
Decrypted Length =

把base64数据,传递给go语言(这里需要把对应的go语言的公钥、私钥替换成c语言中的示例),用私钥进行解密,可以得到对应的原始数据,这样验证 c 和 go之间的加解密OK。

go run rsa.go -d BXFuS6snRRhYUQhTYKG00UoGMzaptbZpaw7h+JEA+yP1sEd5dXaBEjdzzZ8jXRVfjigYFj41zEBSDsafxO440+vi/aODOAY1f4Q2dKf5eBLnXDyN9uT6uxCp67XgZC3YkN+iufMOKIEjSbb361kZrRLwl+6OUt4EYvkhmKl5YpklM+qQYyGW5xyCU5MYMUTXJoAthRTeNuoloSiZDQWKPH4i7bVfR/XDjXIA3iNWfoy5552L4AdtyRhRNvZ0RerbVtPqdWABvJX39hbskmGuzY9HtoxOQaoD0IWGHx29VmCM3JRrTvStnEKjYnUNT9ikbeK1gXX5JjU6HmLHbgPKUg==
[1024]

【go语言】RSA加解密的更多相关文章

  1. 与非java语言使用RSA加解密遇到的问题:algid parse error, not a sequence

    遇到的问题 在一个与Ruby语言对接的项目中,决定使用RSA算法来作为数据传输的加密与签名算法.但是,在使用Ruby生成后给我的私钥时,却发生了异常:IOException: algid parse ...

  2. openssl在多平台和多语言之间进行RSA加解密注意事项

    首先说一下平台和语言: 系统平台为CentOS6.3,RSA加解密时使用NOPADDING进行填充 1)使用C/C++调用系统自带的openssl 2)Android4.2模拟器,第三方openssl ...

  3. Rsa加解密Java、C#、php通用代码 密钥转换工具

    之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...

  4. Lua 中的 RSA 加解密实现

    记得之前,部门某款游戏陆陆续续收到一些玩家反馈,抱怨在登录游戏时会等待很久.初步排查后基本断定可能是此游戏的登录服务器程序某块代码有问题,于是即安排了服务器同事作排查分析但一直无果. 之后我时间有了空 ...

  5. RSA加解密&RSA加验签详解

    RSA 加密算法是目前最有影响力的 公钥加密算法,并且被普遍认为是目前 最优秀的公钥方案 之一.RSA 是第一个能同时用于 加密 和 数字签名 的算法,它能够 抵抗 到目前为止已知的 所有密码攻击,已 ...

  6. java RSA加解密以及用途

    在公司当前版本的中间件通信框架中,为了防止非授权第三方和到期客户端的连接,我们通过AES和RSA两种方式的加解密策略进行认证.对于非对称RSA加解密,因为其性能耗费较大,一般仅用于认证连接,不会用于每 ...

  7. openssl - rsa加解密例程

    原文链接: http://www.cnblogs.com/cswuyg/p/3187462.html openssl是可以很方便加密解密的库,可以使用它来对需要在网络中传输的数据加密.可以使用非对称加 ...

  8. RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密

    package com.geostar.gfstack.cas.util; import org.apache.commons.codec.binary.Base64; import javax.cr ...

  9. PHP RSA加解密详解(附代码)

    前言:RSA加密一般用在涉及到重要数据时所使用的加密算法,比如用户的账户密码传输,订单的相关数据传输等. 加密方式说明:公钥加密,私钥解密.也可以  私钥加密,公钥解密 一.RSA简介 RSA公钥加密 ...

随机推荐

  1. iOS开发-- RunLoop的基本概念与例子分析

    看了一下,上一篇貌似5个月前的

  2. bzoj1051 [HAOI2006]受欢迎的牛

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4773  Solved: 2541[Submit][Sta ...

  3. nginx相关的一些记录

    http redirect to https: if ($http_cf_visitor ~ '"scheme":"http"'){ rewrite ^/(.* ...

  4. spring3 项目更新

    列志华 (组长) http://www.cnblogs.com/liezhihua/ 团队guihub https://github.com/LWHTF/OrderingFood 黄柏堂 http:/ ...

  5. tomcat7/8 启用调试模式,可进行远程调试

    tomcat7,和 tomcat6 的jpda 不一样,tomcat7已经把jpda配置的属性在catalina.sh/catalina.bat里面已经写好了,我们不需要向tomcat6那样去设置参数 ...

  6. 常用MySQL命令整理

    备份数据库 #.备份数据库到sql文件 mysqldump --add-drop-database -h localhost -uusername ppassword dbname > dbna ...

  7. Maximo子表中增加附件功能

    附件功能的实现(详见ewell.webclient.beans.warranty.WarrantysDateBean ,ewell.webclient.beans.doclinks.custom.Ad ...

  8. nodejs+express+jade配置

    安装步骤 一.首先可跟着这个网址安装http://jingyan.baidu.com/article/91f5db1b2bb6941c7f05e33c.html,路径可由自己定. 二.同时参考http ...

  9. C#图片上传服务器缩放存储功能

    项目需求上需要用户上传头像.本来是用第三方的插件的.但是很多都是收费的. 不过,我需要花这钱么?是不?所以网络上搜集了些资料.. 果然.这个世界 大神是很多很多的. 用了个免费插件. <scri ...

  10. hashMap的get()方法,错用并发造成cpu和负载高

    一次线上问题的解决 线上发现服务cpu使用达到98%,负载高达200多,64核心cpu,下面介绍解决过程: 1.top命令查出占用cpu高的进程pid 2.使用jstack -l pid >du ...