当前位置:网站首页>OpenSSL: a full-featured toolkit for TLS and SSL protocols, and a general encryption library
OpenSSL: a full-featured toolkit for TLS and SSL protocols, and a general encryption library
2022-07-06 22:40:00 【Ink painting long sky】
OpenSSL: apply TLS And SSL The full-featured toolkit of the Protocol , Universal encryption library
One 、SSL Library and cryptographic algorithm library
name | library |
---|---|
EVP Packaging Library | libeay32.lib |
SSL library | ssleay32.lib |
Two 、 Use OpenSSL
- install OpenSSL-Win32
- introduce libeay32.lib and ssleay32.lib
- Set up OpenSSL Header Directory
3、 ... and 、 The format of the certificate
- XML A standard format ------- C# RSA certificate
<RSAKeyValue>
<Modulus> </Modulus>
<Exponent> </Exponent>
<P> </P>
<Q> </Q>
<DP> </DP>
<DQ> </DQ>
<InverseQ> </InverseQ>
<D> </D>
</RSAKeyValue>
- PEM PKCS#8 Unencrypted format ------- Java
-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----
- PEM PKCS#1 Format
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
Four 、 Common encryption algorithms
Symmetric encryption algorithm :
- 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 encryption * encData: 8*n bytes * encKey: 3 Multiple key length ¬24 bytes * encIV: Encryption vector ¬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 Decrypt * decData: 8*n bytes * decKey: 3 Multiple key length ¬24 bytes * decIV: Encryption vector ¬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;
}
Asymmetric encryption algorithm :
- RSA
//RSA Private key decryption
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 Public key encryption
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;
}
边栏推荐
- NPM cannot install sharp
- pytorch_ Yolox pruning [with code]
- Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
- three.js绚烂的气泡效果
- 2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
- Windows auzre background operation interface of Microsoft's cloud computing products
- 【无标题】
- Aardio - construct a multi button component with customplus library +plus
- 枚举与#define 宏的区别
- memcached
猜你喜欢
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
The SQL response is slow. What are your troubleshooting ideas?
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
Signed and unsigned keywords
Rust knowledge mind map XMIND
视图(view)
在IPv6中 链路本地地址的优势
如何用程序确认当前系统的存储模式?
config:invalid signature 解决办法和问题排查详解
二分图判定
随机推荐
Build op-tee development environment based on qemuv8
return 关键字
LeetCode 练习——剑指 Offer 26. 树的子结构
Aardio - 封装库时批量处理属性与回调函数的方法
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
Export MySQL table data in pure mode
Inno setup packaging and signing Guide
cuda 探索
C# 三种方式实现Socket数据接收
MATLAB小技巧(27)灰色预测
Puppeteer连接已有Chrome浏览器
Inno Setup 打包及签名指南
That's why you can't understand recursion
SQL Server生成自增序号
[leetcode] 19. Delete the penultimate node of the linked list
On the problems of born charge and non analytical correction in phonon and heat transport calculations
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
void关键字
How to use flexible arrays?
signed、unsigned关键字