对openssl做的一些简单封装
#include
"
stdafx.h
"
int GetRSA(RSA ** RsaKeys)
{
* RsaKeys = RSA_generate_key(RSALEN,RSA_F4,NULL,NULL);
if (NULL ==* RsaKeys)
return - 1 ;
return 0 ;
}
// 取得私钥
int GetPrivateKey(RSA * RsaKeys,RSA ** Pvtkey)
{
* Pvtkey = RSAPrivateKey_dup(RsaKeys);
if (NULL ==* Pvtkey)
return - 1 ;
return 0 ;
}
// 私钥To数据流
int PrivateKeyToData(RSA * Pvtkey,unsigned char * bufkey)
{
BIO * pBio = BIO_new(BIO_s_mem());
if (pBio == NULL) {
return - 1 ;
}
memset(bufkey, ' \0 ' ,RSALEN);
if ( i2d_RSAPrivateKey_bio(pBio,Pvtkey) < 0 ) {
BIO_free(pBio);
return - 1 ;
}
BIO_read(pBio,bufkey,RSALEN);
BIO_free(pBio);
return 0 ;
}
// 数据流To私钥
int DataToPrivateKey(unsigned char * bufkey,RSA ** Pvtkey)
{
BIO * pBio = BIO_new(BIO_s_mem());
if (pBio == NULL) {
return - 1 ;
}
BIO_write(pBio,bufkey,RSALEN);
if ( NULL == d2i_RSAPrivateKey_bio(pBio,Pvtkey)) {
BIO_free(pBio);
return - 1 ;
}
BIO_free(pBio);
return 0 ;
}
// 取得公钥
int GetPublicKey(RSA * RsaKeys,RSA ** Pubkey)
{
* Pubkey = RSAPublicKey_dup(RsaKeys);
if (NULL ==* Pubkey)
return - 1 ;
return 0 ;
}
// 公钥To数据流
int PublicKeyToData(RSA * Pubkey,unsigned char * bufkey)
{
BIO * pBio = BIO_new(BIO_s_mem());
if (pBio == NULL) {
return - 1 ;
}
memset(bufkey, ' \0 ' ,RSALEN);
if (i2d_RSAPublicKey_bio(pBio,Pubkey) < 0 ) {
BIO_free(pBio);
return - 1 ;
}
BIO_read(pBio,bufkey,RSALEN);
BIO_free(pBio);
return 0 ;
}
// 数据流To公钥
int DataToPublicKey(unsigned char * bufkey,RSA ** Pubkey)
{
BIO * pBio = BIO_new(BIO_s_mem());
if (pBio == NULL)
{
return - 1 ;
}
BIO_write(pBio,bufkey,RSALEN);
if ( d2i_RSAPublicKey_bio(pBio,Pubkey) < 0 ) {
BIO_free(pBio);
return - 1 ;
}
BIO_free(pBio);
return 0 ;
}
/* 公钥加密->私钥解密 */
int RSAPublicEncrypt(RSA * Publickey, char * From, char * To)
{
int len = 0 ;
len = RSA_size(Publickey) - 11 ;
if ( - 1 == (len = RSA_public_encrypt(len,(unsigned char * )From,(unsigned char * )To,Publickey,RSA_PKCS1_PADDING)) )
return - 1 ;
return len;
}
/* 私钥解密<-公钥加密 */
int RSAPrivateDecrypt(RSA * Privtekey, char * From, char * To)
{
if ( - 1 == (RSA_private_decrypt(RSALEN / 8 ,(unsigned char * )From,(unsigned char * )To,Privtekey,RSA_PKCS1_PADDING)))
return - 1 ;
return 0 ;
}
/* 私钥加密->公钥解密 */
int RSAPrivateEncrypt(RSA * Privtekey, char * From, char * To)
{
int len = RSA_size(Privtekey) - 11 ;
if ( - 1 == (len = RSA_private_encrypt(len,(unsigned char * )From,(unsigned char * )To,Privtekey,RSA_PKCS1_PADDING)))
return - 1 ;
return len;
}
/* 公钥解密<-私钥加密 */
int RSAPublicDecrypt(RSA * Publickey, char * From, char * To)
{
if ( - 1 == (RSA_public_decrypt(RSALEN / 8 ,(unsigned char * )From,(unsigned char * )To,Publickey,RSA_PKCS1_PADDING)) )
return - 1 ;
return 0 ;
}
// void DesEncrypt(char *Key,char *Msg, char *Result,int Length)
// {
// int n=0;
// DES_cblock desblock;
// DES_key_schedule schedule;
//
// DES_string_to_key(Key,&desblock);
// DES_set_key_checked( &desblock, &schedule );
//
// DES_cfb64_encrypt( (unsigned char *)Msg, (unsigned char *)Result,
// Length, &schedule, &desblock, &n, DES_ENCRYPT );
//
// }
//
//
// void DesDecrypt( char *Key, char *Msg, char *Result,int Length)
// {
//
// int n=0;
//
// DES_cblock desblock;
// DES_key_schedule schedule;
//
// DES_string_to_key(Key,&desblock);
// DES_set_key_checked( &desblock, &schedule );
//
// DES_cfb64_encrypt( (unsigned char *) Msg, (unsigned char *)Result,
// Length, &schedule, &desblock, &n, DES_DECRYPT );
//
// }
void DESGenerateKey( char * pKey)
{
int nLen = 33 ;
int flag = 0 ;
int i,k = 0 ;
srand((unsigned)time(NULL));
for (i = 0 ;i < nLen - 1 ;i ++ )
{
flag = rand() % 2 ;
if (flag)
pKey[k ++ ] = ' A ' + rand() % 26 ;
else
pKey[k ++ ] = ' a ' + rand() % 26 ;
}
pKey[k] = ' \0 ' ;
}
int GetRSA(RSA ** RsaKeys)
{
* RsaKeys = RSA_generate_key(RSALEN,RSA_F4,NULL,NULL);
if (NULL ==* RsaKeys)
return - 1 ;
return 0 ;
}
// 取得私钥
int GetPrivateKey(RSA * RsaKeys,RSA ** Pvtkey)
{
* Pvtkey = RSAPrivateKey_dup(RsaKeys);
if (NULL ==* Pvtkey)
return - 1 ;
return 0 ;
}
// 私钥To数据流
int PrivateKeyToData(RSA * Pvtkey,unsigned char * bufkey)
{
BIO * pBio = BIO_new(BIO_s_mem());
if (pBio == NULL) {
return - 1 ;
}
memset(bufkey, ' \0 ' ,RSALEN);
if ( i2d_RSAPrivateKey_bio(pBio,Pvtkey) < 0 ) {
BIO_free(pBio);
return - 1 ;
}
BIO_read(pBio,bufkey,RSALEN);
BIO_free(pBio);
return 0 ;
}
// 数据流To私钥
int DataToPrivateKey(unsigned char * bufkey,RSA ** Pvtkey)
{
BIO * pBio = BIO_new(BIO_s_mem());
if (pBio == NULL) {
return - 1 ;
}
BIO_write(pBio,bufkey,RSALEN);
if ( NULL == d2i_RSAPrivateKey_bio(pBio,Pvtkey)) {
BIO_free(pBio);
return - 1 ;
}
BIO_free(pBio);
return 0 ;
}
// 取得公钥
int GetPublicKey(RSA * RsaKeys,RSA ** Pubkey)
{
* Pubkey = RSAPublicKey_dup(RsaKeys);
if (NULL ==* Pubkey)
return - 1 ;
return 0 ;
}
// 公钥To数据流
int PublicKeyToData(RSA * Pubkey,unsigned char * bufkey)
{
BIO * pBio = BIO_new(BIO_s_mem());
if (pBio == NULL) {
return - 1 ;
}
memset(bufkey, ' \0 ' ,RSALEN);
if (i2d_RSAPublicKey_bio(pBio,Pubkey) < 0 ) {
BIO_free(pBio);
return - 1 ;
}
BIO_read(pBio,bufkey,RSALEN);
BIO_free(pBio);
return 0 ;
}
// 数据流To公钥
int DataToPublicKey(unsigned char * bufkey,RSA ** Pubkey)
{
BIO * pBio = BIO_new(BIO_s_mem());
if (pBio == NULL)
{
return - 1 ;
}
BIO_write(pBio,bufkey,RSALEN);
if ( d2i_RSAPublicKey_bio(pBio,Pubkey) < 0 ) {
BIO_free(pBio);
return - 1 ;
}
BIO_free(pBio);
return 0 ;
}
/* 公钥加密->私钥解密 */
int RSAPublicEncrypt(RSA * Publickey, char * From, char * To)
{
int len = 0 ;
len = RSA_size(Publickey) - 11 ;
if ( - 1 == (len = RSA_public_encrypt(len,(unsigned char * )From,(unsigned char * )To,Publickey,RSA_PKCS1_PADDING)) )
return - 1 ;
return len;
}
/* 私钥解密<-公钥加密 */
int RSAPrivateDecrypt(RSA * Privtekey, char * From, char * To)
{
if ( - 1 == (RSA_private_decrypt(RSALEN / 8 ,(unsigned char * )From,(unsigned char * )To,Privtekey,RSA_PKCS1_PADDING)))
return - 1 ;
return 0 ;
}
/* 私钥加密->公钥解密 */
int RSAPrivateEncrypt(RSA * Privtekey, char * From, char * To)
{
int len = RSA_size(Privtekey) - 11 ;
if ( - 1 == (len = RSA_private_encrypt(len,(unsigned char * )From,(unsigned char * )To,Privtekey,RSA_PKCS1_PADDING)))
return - 1 ;
return len;
}
/* 公钥解密<-私钥加密 */
int RSAPublicDecrypt(RSA * Publickey, char * From, char * To)
{
if ( - 1 == (RSA_public_decrypt(RSALEN / 8 ,(unsigned char * )From,(unsigned char * )To,Publickey,RSA_PKCS1_PADDING)) )
return - 1 ;
return 0 ;
}
// void DesEncrypt(char *Key,char *Msg, char *Result,int Length)
// {
// int n=0;
// DES_cblock desblock;
// DES_key_schedule schedule;
//
// DES_string_to_key(Key,&desblock);
// DES_set_key_checked( &desblock, &schedule );
//
// DES_cfb64_encrypt( (unsigned char *)Msg, (unsigned char *)Result,
// Length, &schedule, &desblock, &n, DES_ENCRYPT );
//
// }
//
//
// void DesDecrypt( char *Key, char *Msg, char *Result,int Length)
// {
//
// int n=0;
//
// DES_cblock desblock;
// DES_key_schedule schedule;
//
// DES_string_to_key(Key,&desblock);
// DES_set_key_checked( &desblock, &schedule );
//
// DES_cfb64_encrypt( (unsigned char *) Msg, (unsigned char *)Result,
// Length, &schedule, &desblock, &n, DES_DECRYPT );
//
// }
void DESGenerateKey( char * pKey)
{
int nLen = 33 ;
int flag = 0 ;
int i,k = 0 ;
srand((unsigned)time(NULL));
for (i = 0 ;i < nLen - 1 ;i ++ )
{
flag = rand() % 2 ;
if (flag)
pKey[k ++ ] = ' A ' + rand() % 26 ;
else
pKey[k ++ ] = ' a ' + rand() % 26 ;
}
pKey[k] = ' \0 ' ;
}