📄 cryptit.cpp
字号:
#include "CryptIt.h"
#include <stdio.h>#include <stdlib.h>#include <string>#include <time.h>#include <sys/types.h>#include <sys/stat.h>using namespace std;
CryptIt::CryptIt()
{
DesKey = "";
}
CryptIt::~CryptIt()
{
}
//产生密匙对
bool CryptIt::CreateRsaKeyPair( unsigned long ulBit /*= 1024 */ )
{
R_RANDOM_STRUCT randomstruct; R_RSA_PROTO_KEY protokey; int status; R_RandomCreate( &randomstruct ); protokey.bits = ulBit; protokey.useFermat4 = 1; status = R_GeneratePEMKeys( &PublicKey,&PrivateKey,&protokey,&randomstruct ); return ( !status );//成功state=0,失败= !0
}
//__________________________________________保存为一个文件的RsaKey
//保存Keys
bool CryptIt::SaveRsaKeys(char* filename,R_RSA_PUBLIC_KEY publicKey,R_RSA_PRIVATE_KEY privateKey)
{
FILE* KeyFile;
KeyFile=fopen(filename,"wb");
if( KeyFile== NULL )
return false;
fwrite(&privateKey,sizeof(R_RSA_PRIVATE_KEY),1,KeyFile);
fwrite(&publicKey,sizeof(R_RSA_PUBLIC_KEY),1,KeyFile);
fclose(KeyFile);
return true;
}
//取得Keys
bool CryptIt::LodeRsaKeys(char* filename,R_RSA_PUBLIC_KEY * publicKey,R_RSA_PRIVATE_KEY * privateKey)
{
FILE* KeyFile;
KeyFile=fopen(filename,"rb");
if( KeyFile== NULL )
return false;
fread(privateKey,sizeof(R_RSA_PRIVATE_KEY),1,KeyFile);
fread(publicKey,sizeof(R_RSA_PUBLIC_KEY),1,KeyFile);
fclose(KeyFile);
return true;
}
//__________________________________________End
//__________________________________________把publickey 与 privatekey保存为单个的RsaKey
//保存RSA Key
bool CryptIt::SavePublicKey( char *strFileName , R_RSA_PUBLIC_KEY publicKey)
{
FILE *file;
if((file = fopen ( strFileName, "w+")) != NULL) { fwrite(&publicKey, sizeof(char),sizeof(R_RSA_PUBLIC_KEY), file);
}
else
{
return false;
}
fclose( file );
return true;
}
bool CryptIt::SavePrivateKey( char *strFileName, R_RSA_PRIVATE_KEY privateKey )
{
FILE *file;
if((file = fopen ( strFileName, "w+")) != NULL) { fwrite(&privateKey, sizeof(char),sizeof(R_RSA_PRIVATE_KEY), file);
}
else
{
return false;
}
fclose( file );
return true;
}
//加载私匙
bool CryptIt::LoadPrivateKey( char *strFileName, R_RSA_PRIVATE_KEY * privateKey)
{
FILE* KeyFile;
KeyFile=fopen(strFileName,"rb");
if( KeyFile== NULL )
return false;
fread(privateKey,sizeof(R_RSA_PRIVATE_KEY),1,KeyFile);
fclose(KeyFile);
return true;
}
//加载公匙
bool CryptIt::LoadPulicKey( char *strFileName, R_RSA_PUBLIC_KEY *publicKey )
{
FILE* KeyFile;
KeyFile=fopen(strFileName,"rb");
if( KeyFile== NULL )
return false;
fread(publicKey,sizeof(R_RSA_PUBLIC_KEY),1,KeyFile);
fclose(KeyFile);
return true;
}
//__________________________________________End
//用RSA私钥加密字符串
bool CryptIt::RSAPrivateEncode( unsigned char *strIn,const unsigned int iInEncodeLength, unsigned char *strOut , unsigned int *iOutEncodeLength,R_RSA_PRIVATE_KEY *privKey)
{
unsigned int iInputLength = iInEncodeLength;
unsigned int ibufsize,obufsize,olen = 0;
unsigned char ibuf[MAX_RSA_MODULUS_LEN];
unsigned char obuf[MAX_RSA_MODULUS_LEN];
ibufsize=(PrivateKey.bits + 7) / 8-11;
while(iInputLength > 0)
{
memset(ibuf,0,MAX_RSA_MODULUS_LEN);
memset(obuf,0,MAX_RSA_MODULUS_LEN);
if (iInputLength < ibufsize)
ibufsize = iInputLength ;
memcpy(ibuf,strIn+(iInEncodeLength-iInputLength),ibufsize);
iInputLength -=ibufsize;
if( RSAPrivateEncrypt( obuf,&obufsize,ibuf,ibufsize,privKey))
return false ;
memcpy(strOut+olen,obuf,obufsize);
olen+=obufsize;
}
*iOutEncodeLength=olen;
return true;
}
//用RSA用公钥解密字符串
bool CryptIt::RsaPublicDecode(unsigned char *strIn,const unsigned int iInDeodeLength, unsigned char *strOut , unsigned int *iOutDecodeLength,R_RSA_PUBLIC_KEY *pubKey)
{
unsigned int iInputLength = iInDeodeLength;
unsigned int ibufsize,obufsize,olen = 0;
unsigned char ibuf[MAX_RSA_MODULUS_LEN];
unsigned char obuf[MAX_RSA_MODULUS_LEN];
ibufsize=(PublicKey.bits + 7) / 8;
while(iInputLength > 0)
{
memset(ibuf,0,MAX_RSA_MODULUS_LEN);
memset(obuf,0,MAX_RSA_MODULUS_LEN);
if (iInputLength < ibufsize)
ibufsize = iInputLength ;
memcpy(ibuf,strIn+(iInDeodeLength-iInputLength),ibufsize);
iInputLength -=ibufsize;
if( RSAPublicDecrypt( obuf,&obufsize,ibuf,ibufsize,pubKey))
return false ;
memcpy(strOut+olen,obuf,obufsize);
olen+=obufsize;
}
*iOutDecodeLength=olen;
return true;
}
//用RSA公钥加密字符串
bool CryptIt::RSAPublicEncode( unsigned char *strIn,const unsigned int iInEncodeLength, unsigned char *strOut , unsigned int *iOutEncodeLength,R_RSA_PUBLIC_KEY *pubKey)
{
unsigned int iInputLength = iInEncodeLength;
unsigned int ibufsize,obufsize,olen = 0;
unsigned char ibuf[MAX_RSA_MODULUS_LEN];
unsigned char obuf[MAX_RSA_MODULUS_LEN];
ibufsize=(PrivateKey.bits + 7) / 8-11;
while(iInputLength > 0)
{
memset(ibuf,0,MAX_RSA_MODULUS_LEN);
memset(obuf,0,MAX_RSA_MODULUS_LEN);
if (iInputLength < ibufsize)
ibufsize = iInputLength ;
memcpy(ibuf,strIn+(iInEncodeLength-iInputLength),ibufsize);
iInputLength -=ibufsize;
if( RSAPublicEncrypt( obuf,&obufsize,ibuf,ibufsize,pubKey))
return false ;
memcpy(strOut+olen,obuf,obufsize);
olen+=obufsize;
}
*iOutEncodeLength=olen;
return true;
}
//用RSA用私钥解密字符串
bool CryptIt::RsaPrivateDecode(unsigned char *strIn,const unsigned int iInDeodeLength, unsigned char *strOut , unsigned int *iOutDecodeLength,R_RSA_PRIVATE_KEY *privKey)
{
unsigned int iInputLength = iInDeodeLength;
unsigned int ibufsize,obufsize,olen = 0;
unsigned char ibuf[MAX_RSA_MODULUS_LEN];
unsigned char obuf[MAX_RSA_MODULUS_LEN];
ibufsize=(PublicKey.bits + 7) / 8;
while(iInputLength > 0)
{
memset(ibuf,0,MAX_RSA_MODULUS_LEN);
memset(obuf,0,MAX_RSA_MODULUS_LEN);
if (iInputLength < ibufsize)
ibufsize = iInputLength ;
memcpy(ibuf,strIn+(iInDeodeLength-iInputLength),ibufsize);
iInputLength -=ibufsize;
if( RSAPrivateDecrypt( obuf,&obufsize,ibuf,ibufsize,privKey))
return false ;
memcpy(strOut+olen,obuf,obufsize);
olen+=obufsize;
}
*iOutDecodeLength=olen;
return true;
}
//用DES3加密
bool CryptIt::Des3Encode(char *strIn,int inlength, char *strOut,int *outlength,char* strKey )
{
DES3_CBC_CTX pDes3Context;
unsigned char pDes3iv[] ="abcdefg";
DES3_CBCRestart( &pDes3Context );
DES3_CBCInit( &pDes3Context,(unsigned char*)strKey,pDes3iv,1);
//DES3_CBCUpdate( &pDes3Context,(unsigned char *)strOut,(unsigned char *)strIn,1024);
char tmpOut[8] ;
int times = inlength/8;
int remainder = inlength%8 ;
*outlength = inlength - remainder ;
int i= 0;
for (;i<times ;i++)
{
ZeroMemory(tmpOut,8);
DES3_CBCUpdate( &pDes3Context,(unsigned char *)tmpOut,(unsigned char *)strIn+i*8,8) ;
memcpy(strOut+i*8,tmpOut,8);
}
if (remainder >0 )
{
ZeroMemory(tmpOut,8);
memset(strIn+i*8+remainder,0,8-remainder);
DES3_CBCUpdate( &pDes3Context,(unsigned char *)tmpOut,(unsigned char *)strIn+i*8,8) ;
memcpy(strOut+i*8,tmpOut,8);
*outlength += 8 ;
}
return true;
}
//用Des3解密
bool CryptIt::Des3Decode( char *strIn,int inlength, char *strOut,int *outlength,char* strKey )
{
DES3_CBC_CTX pDes3Context;
unsigned char pDes3iv[] ="abcdefg";
DES3_CBCRestart( &pDes3Context );
DES3_CBCInit( &pDes3Context,(unsigned char*)strKey,pDes3iv,0);
//拆开加密
int remainder = inlength%8 ;
if (remainder > 0)
{
return false ;//出错了
}
else
DES3_CBCUpdate( &pDes3Context,(unsigned char *)strOut,(unsigned char *)strIn,inlength);
*outlength = inlength ;
return true;
}
//MD5
bool CryptIt::Md5Hash( unsigned char *strIn, char *strOut )
{
R_DIGEST_CTX md5ctxt;
if( R_DigestInit( &md5ctxt,DA_MD5)!=ID_OK )
return false;
int stringLen = strlen( (const char *)strIn );
for( int i=0;i<stringLen;i++ )
R_DigestUpdate( &md5ctxt,(unsigned char *)&strIn[i],1 );
unsigned char strOutTemp[128]="";
int digLen;
R_DigestFinal( &md5ctxt,strOutTemp,(unsigned int*)&digLen );
CString strTemp,strTemp1;
for( i=0;i<digLen;i++)
{
strTemp.Format("%02x",strOutTemp[i]);
strTemp1+=strTemp;
}
strcpy( strOut,strTemp1.GetBuffer());
strTemp1.ReleaseBuffer();
return true;
}
bool CryptIt::RC4Makey(unsigned char *key, int keylength/*= 256*/)
{
if( keylength == 0)
return false;
rc4_makey(key,keylength);
return true;
}
//RC4CODE
bool CryptIt::RC4Code( unsigned char *data, int lDataength,unsigned char *key, int lKeyength )
{
ZeroMemory(&s,sizeof(rc4_state));
//设置
rc4_setup( &s, key, lKeyength );
//加密或者解密
rc4_crypt( &s, data, lDataength );
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -