📄 crypt.cpp
字号:
// Crypt.cpp: implementation of the CCrypt class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "Client.h"
#include "Crypt.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCrypt::CCrypt()
{
strRsaPubFilename = "";
strRsaPriFilename = "";
strCreateKeySeed = "";
strRsaEncryptSeed = "";
strDesKey = "";
nRsaKeyLength = 0;
}
CCrypt::~CCrypt()
{
}
void CCrypt::setRsaPubFilename(const string &filename)
{
strRsaPubFilename = filename;
}
void CCrypt::setRsaPriFilename(const string &filename)
{
strRsaPriFilename = filename;
}
void CCrypt::setCreateKeySeed(const string &seed)
{
strCreateKeySeed = seed;
}
void CCrypt::setRsaEncryptSeed(const string &seed)
{
strRsaEncryptSeed = seed;
}
void CCrypt::setDesKey(const string &key)
{
strDesKey = key;
}
void CCrypt::setRsaKeyLength(const int &len)
{
nRsaKeyLength = len;
}
void CCrypt::setRsaDecryptor(void)
{
FileSource privFile(strRsaPriFilename.c_str(), true, new HexDecoder);
RSAES_OAEP_SHA_Decryptor privTmp(privFile);
rsa_priv = privTmp;
}
void CCrypt::setRsaEncryptor(void)
{
FileSource pubFile(strRsaPubFilename.c_str(), true, new HexDecoder);
RSAES_OAEP_SHA_Encryptor pubTmp(pubFile);
rsa_pub = pubTmp;
}
string CCrypt::getRsaPubFilename(void)
{
return strRsaPubFilename;
}
string CCrypt::getRsaPriFilename(void)
{
return strRsaPriFilename;
}
string CCrypt::getCreateKeySeed(void)
{
return strCreateKeySeed;
}
string CCrypt::getRsaEncryptSeed(void)
{
return strRsaEncryptSeed;
}
string CCrypt::getDesKey(void)
{
return strDesKey;
}
int CCrypt::getRsaKeyLength(void)
{
return nRsaKeyLength;
}
string CCrypt::desEncrypt(const char *message)
{
string outstr("");
DefaultEncryptorWithMAC encryptor(strDesKey.c_str(), new HexEncoder(new StringSink(outstr)));
encryptor.Put((byte *)message, strlen(message));
encryptor.MessageEnd();
return outstr;
}
string CCrypt::desDecrypt(const char *Ciphertext)
{
string outstr;
HexDecoder decryptor(new DefaultDecryptorWithMAC(strDesKey.c_str(), new StringSink(outstr)));
decryptor.Put((byte *)Ciphertext, strlen(Ciphertext));
decryptor.MessageEnd();
return outstr;
}
string CCrypt::rsaEncrypt(const char *message)
{
RandomPool randPool;
randPool.Put((byte *)strRsaEncryptSeed.c_str(), strRsaEncryptSeed.length());
string result;
StringSource(message, true, new PK_EncryptorFilter(randPool, rsa_pub, new HexEncoder(new StringSink(result))));
return result;
}
RandomPool & GlobalRNG()
{
static RandomPool randomPool;
return randomPool;
}
string CCrypt::rsaDecrypt(const char *Ciphertext)
{
string result;
StringSource(Ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), rsa_priv, new StringSink(result))));
return result;
}
void CCrypt::shaEncrypt(const char *message, byte *Ciphertext)
{
SHA sha;
sha.CalculateDigest(Ciphertext,(byte *)message,strlen(message));
}
void CCrypt::sha256Encrypt(const char *message, byte *Ciphertext)
{
SHA256 sha;
sha.CalculateDigest(Ciphertext,(byte *)message,strlen(message));
}
void CCrypt::sha512Encrypt(const char *message, byte *Ciphertext)
{
SHA512 sha;
sha.CalculateDigest(Ciphertext,(byte *)message,strlen(message));
}
void CCrypt::sha384Encrypt(const char *message, byte *Ciphertext)
{
SHA384 sha;
sha.CalculateDigest(Ciphertext,(byte *)message,strlen(message));
}
int CCrypt::rsaCreateKey(void)
{
if(nRsaKeyLength == 0)
return -1;
RandomPool randPool;
randPool.Put((byte *)strCreateKeySeed.c_str(), strCreateKeySeed.length());
RSAES_OAEP_SHA_Decryptor priv(randPool, nRsaKeyLength);
HexEncoder privFile(new FileSink(strRsaPriFilename.c_str())); //打开文件实行序列化操作
priv.DEREncode(privFile); //写密码对象priv到文件对象privFile里
privFile.MessageEnd();
RSAES_OAEP_SHA_Encryptor pub(priv);
HexEncoder pubFile(new FileSink(strRsaPubFilename.c_str()));
pub.DEREncode(pubFile);
pubFile.MessageEnd();
return 0;
}
string CCrypt::aesEncrypt(const char *message)
{
string outstr;
byte iv[AES::BLOCKSIZE]="123456";
AES::Encryption aesEncryption((byte *)strAesKey.c_str(), AES::DEFAULT_KEYLENGTH);
CFB_Mode_ExternalCipher::Encryption cfbEncryption(aesEncryption, iv);
StreamTransformationFilter
cfbEncryptor(cfbEncryption, new HexEncoder(new StringSink(outstr)));
cfbEncryptor.Put((byte *)message, strlen(message));
cfbEncryptor.MessageEnd();
return outstr;
}
string CCrypt::aesDecrypt(const char *message)
{
string outstr;
byte iv[AES::BLOCKSIZE]="123456";
CFB_Mode<AES >::Decryption
cfbDecryption((byte *)strAesKey.c_str(), AES::DEFAULT_KEYLENGTH, iv);
HexDecoder
decryptor(new StreamTransformationFilter(cfbDecryption, new StringSink(outstr)));
decryptor.Put((byte *)message, strlen(message));
decryptor.MessageEnd();
return outstr;
}
void CCrypt::setAesKey(const string &key)
{
strAesKey = key;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -