当前位置:网站首页>OpenSSL:适用TLS与SSL协议的全功能工具包,通用加密库
OpenSSL:适用TLS与SSL协议的全功能工具包,通用加密库
2022-07-06 15:32:00 【水墨长天】
OpenSSL:适用TLS与SSL协议的全功能工具包,通用加密库
一、SSL库和密码学算法库
| 名称 | 库 |
|---|---|
| EVP封装库 | libeay32.lib |
| SSL库 | ssleay32.lib |
二、使用OpenSSL
- 安装OpenSSL-Win32
- 引入 libeay32.lib 和 ssleay32.lib
- 设置OpenSSL头文件目录
三、证书的格式
- XML标准格式 ------- C# RSA证书
<RSAKeyValue>
<Modulus> </Modulus>
<Exponent> </Exponent>
<P> </P>
<Q> </Q>
<DP> </DP>
<DQ> </DQ>
<InverseQ> </InverseQ>
<D> </D>
</RSAKeyValue>
- PEM PKCS#8非加密格式 ------- Java
-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----
- PEM PKCS#1格式
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
四、常见加密算法
对称加密算法:
- AES
- DES/3DES(TripleDES)
#pragma comment(lib, "libeay32.lib")
#include <openssl/des.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
/* 3DES加密 * encData: 8*n bytes * encKey: 3倍密钥长度¬24 bytes * encIV: 加密向量¬8bytes,default:{1,2,3,4,5,6,7,8} * out : enc output data * return: error:-1 successful: out length */
int iEVP_Encrypt(byte* data, byte* key, byte* iv, byte* out)//end with 0
{
int ret;
int outLen;
int tmpLen;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_set_padding(&ctx,0);
EVP_CIPHER_CTX_init(&ctx);
ret = EVP_EncryptInit_ex(&ctx,EVP_des_ede3_cbc(),NULL,key,iv);
EVP_CIPHER_CTX_set_padding(&ctx,0);//will reInit enc and dec
if(ret != 1)
{
return NULL;//error
}
ret = EVP_EncryptUpdate(&ctx,out,&outLen,(unsigned char *)data,8);
if(ret != 1)
{
return NULL;//error
}
ret = EVP_EncryptFinal_ex(&ctx,out+outLen,&tmpLen);
if(ret != 1)
{
return NULL;//Err
}
outLen = outLen + tmpLen;
return outLen;
}
/* 3DES解密 * decData: 8*n bytes * decKey: 3倍密钥长度¬24 bytes * decIV: 加密向量¬8bytes,default:{1,2,3,4,5,6,7,8} * out :dec output data * return: error:-1 successful: out length */
int iEVP_Decrypt(byte* data, byte* key, byte* iv, byte* out)
{
int ret = 0;
int outLen = 8;
int tmpLen = 0;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_set_padding(&ctx,0);
EVP_CIPHER_CTX_init(&ctx);
ret = EVP_DecryptInit_ex(&ctx,EVP_des_ede3_cbc(),NULL,key,iv);
EVP_CIPHER_CTX_set_padding(&ctx,0);//will reInit enc and dec
if(ret!=1)
{
return NULL;//Err
}
ret = EVP_DecryptUpdate(&ctx,out,&outLen,data,outLen);
if(ret!=1)
{
return NULL;//Err
}
ret = EVP_DecryptFinal_ex(&ctx,out+outLen,&tmpLen);
if(ret!=1)
{
return NULL;//Err
}
outLen = outLen + tmpLen;
return outLen;
}
非对称加密算法:
- RSA
//RSA私钥解密
string bio_read_privateKey(string data)
{
OpenSSL_add_all_algorithms();
BIO* bp = BIO_new( BIO_s_file() );
BIO_read_filename( bp, "private.pem" );
RSA* rsaK = PEM_read_bio_RSAPrivateKey( bp, NULL, NULL, NULL );
if (NULL == rsaK)
{
return NULL;//Error
}
int nLen = RSA_size(rsaK);
if (nLen == NULL)
{
return NULL;//Error
}
char* pEncode = new char[nLen +1];
memset(pEncode,0,nLen+1);
int ret = RSA_private_decrypt(data.length(),(byte*)data.c_str(),(byte*)pEncode,rsaK,RSA_PKCS1_PADDING);
string strRet;
if (ret >= 0)
{
strRet = string(pEncode, ret);
}
else
{
return NULL;
}
delete[] pEncode;
CRYPTO_cleanup_all_ex_data();
BIO_free_all( bp );
RSA_free(rsaK);
return strRet;
}
//rsa公钥加密
string bio_read_publicKey(string data)
{
OpenSSL_add_all_algorithms();
BIO* bp = BIO_new( BIO_s_file());
BIO_read_filename( bp, "public.pem" );
RSA* rsaK ;
if((rsaK = PEM_read_bio_RSA_PUBKEY(bp,NULL,NULL,NULL)) == NULL)
{
return NULL;
}
if (NULL == rsaK)
{
return NULL;//read failed
}
int nLen = RSA_size(rsaK);
char *pEncode = new char[nLen + 1];
memset(pEncode,0,nLen+1);
int ret = RSA_public_encrypt(data.length(),(const byte*)data.c_str(),(byte*)pEncode,rsaK,RSA_PKCS1_PADDING);
string strRet;
if (ret >= 0)
{
strRet = string(pEncode, ret);
}
else
{
return NULL;
}
delete[] pEncode;
CRYPTO_cleanup_all_ex_data();
BIO_free_all( bp );
RSA_free(rsaK);
return strRet;
}
边栏推荐
- Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
- C# 三种方式实现Socket数据接收
- 做国外LEAD2022年下半年几点建议
- memcached
- 【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
- Aardio - construct a multi button component with customplus library +plus
- [linear algebra] determinant of order 1.3 n
- Should novice programmers memorize code?
- Aardio - 利用customPlus库+plus构造一个多按钮组件
- 2022-07-05 使用tpcc对stonedb进行子查询测试
猜你喜欢
随机推荐
[leetcode] 19. Delete the penultimate node of the linked list
视图(view)
Sizeof keyword
The ceiling of MySQL tutorial. Collect it and take your time
2014阿里巴巴web前实习生项目分析(1)
(十八)LCD1602实验
剪映+json解析将视频中的声音转换成文本
What are the interface tests? What are the general test points?
【LeetCode】19、 删除链表的倒数第 N 个结点
C# 三种方式实现Socket数据接收
Self made j-flash burning tool -- QT calls jlinkarm DLL mode
OpenCV VideoCapture. Get() parameter details
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
qt quick项目offscreen模式下崩溃的问题处理
Return keyword
HDR image reconstruction from a single exposure using deep CNN reading notes
rust知识思维导图xmind
Jafka来源分析——Processor
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)









