Home > Software engineering >  AES encryption algorithm, and sometimes the results right, sometimes wrong (decryption), you help to
AES encryption algorithm, and sometimes the results right, sometimes wrong (decryption), you help to

Time:12-23

//encryption
Void CAESDlg: : OnEncrypt ()
{
Int checkstring=((CButton *) GetDlgItem (IDC_STRING)) - & gt; GetCheck ();
Int checkfile=((CButton *) GetDlgItem (IDC_FILE)) - & gt; GetCheck ();
Cstrings strpass;
Strpass="1314";
Unsigned char * pass=(unsigned char *) LPCTSTR (strpass);
Unsigned char inBuff [25], ouBuff [25];
Memset (inBuff, 0, 25);
Memset (ouBuff, 0, 25);
Aes Aes (32, pass);
Cstrings stryuanwen stryw;
GetDlgItemText (IDC_YUANWEN stryuanwen);
Cstrings STR=", "strTmp;
for (int i=0; I{
Stryw=stryuanwen. Mid (I, 16);
Memcpy (inBuff stryw, 16);
Aes Cipher (inBuff ouBuff);
for(int i=0; I<16. I++)
{
StrTmp. The Format (" % 02 x, "ouBuff [I]);
STR +=strTmp;
}
}
SetDlgItemText (IDC_CIPHERTEXT, STR);

}
Could it be the cause of the memory leak? How to change!!

CodePudding user response:

Stryw=stryuanwen. Mid (I, 16); Memcpy (inBuff stryw, 16); If less than the length of the string, then back to may be??????
Suggest another CHAR WCHAR determine type, or susceptible to compile environmental impact

CodePudding user response:

Don't repeat the wheels, need what copy and paste
 
#include
#include //StreamTransformationFilter
#include //CFB_Mode
#include //STD: cerr
#include //STD: : stringstream
#include

using namespace std;
Using the namespace CryptoPP;


# ifdef _DEBUG
# pragma comment (lib, "cryptlibd. Lib")
# endif

# ifdef _RELEASE
//# pragma comment (lib, "cryptlib. Lib")
# endif



STD: : string ECB_AESEncryptStr (STD: : string sKey, const char * plainText)
{
STD: : string outstr;

//fill key
SecByteBlock key (AES: : MAX_KEYLENGTH);
Memset (key, 0 x30, key. The size ());
SKey. The size () & lt;=AES: : MAX_KEYLENGTH? Memcpy (key, sKey c_str (), sKey. The size ()) : memcpy (key, sKey c_str (), the AES: : MAX_KEYLENGTH);


AES: : Encryption aesEncryption ((byte *) key, AES: : MAX_KEYLENGTH);

ECB_Mode_ExternalCipher: : Encryption ecbEncryption (aesEncryption);
StreamTransformationFilter ecbEncryptor (ecbEncryption, new HexEncoder (new StringSink (outstr)));
EcbEncryptor. Put ((byte *) plainText, strlen (plainText));
EcbEncryptor. MessageEnd ();

return outstr;
}

STD: : string ECB_AESDecryptStr (STD: : string sKey, const char * cipherText)
{
STD: : string outstr;

//fill key
SecByteBlock key (AES: : MAX_KEYLENGTH);
Memset (key, 0 x30, key. The size ());
SKey. The size () & lt;=AES: : MAX_KEYLENGTH? Memcpy (key, sKey c_str (), sKey. The size ()) : memcpy (key, sKey c_str (), the AES: : MAX_KEYLENGTH);

ECB_Mode & lt; AES & gt; : : Decryption ecbDecryption ((byte *) key, AES: : MAX_KEYLENGTH);

HexDecoder decryptor (new StreamTransformationFilter (ecbDecryption, new StringSink (outstr)));
The decryptor. Put ((byte *) cipherText, strlen (cipherText));
The decryptor. MessageEnd ();

return outstr;
}

STD: : string CBC_AESEncryptStr (STD: : string sKey, STD: : string sivs, const char * plainText)
{
STD: : string outstr;

//fill key
SecByteBlock key (AES: : MAX_KEYLENGTH);
Memset (key, 0 x30, key. The size ());
SKey. The size () & lt;=AES: : MAX_KEYLENGTH? Memcpy (key, sKey c_str (), sKey. The size ()) : memcpy (key, sKey c_str (), the AES: : MAX_KEYLENGTH);

//fill iv
Byte [AES: : BLOCKSIZE iv];
Memset (iv, 0 x30, AES: : BLOCKSIZE);
Sivs. The size () & lt;=AES: : BLOCKSIZE? Memcpy (iv, sivs c_str (), sIV, the size ()) : memcpy (iv, sIV c_str (), the AES: : BLOCKSIZE);

AES: : Encryption aesEncryption ((byte *) key, AES: : MAX_KEYLENGTH);

CBC_Mode_ExternalCipher: : Encryption cbcEncryption aesEncryption, (iv);

StreamTransformationFilter cbcEncryptor (cbcEncryption, new HexEncoder (new StringSink (outstr)));
CbcEncryptor. Put ((byte *) plainText, strlen (plainText));
CbcEncryptor. MessageEnd ();

return outstr;
}

STD: : string CBC_AESDecryptStr (STD: : string sKey, STD: : string sivs, const char * cipherText)
{
STD: : string outstr;

//fill key
SecByteBlock key (AES: : MAX_KEYLENGTH);
Memset (key, 0 x30, key. The size ());
SKey. The size () & lt;=AES: : MAX_KEYLENGTH? Memcpy (key, sKey c_str (), sKey. The size ()) : memcpy (key, sKey c_str (), the AES: : MAX_KEYLENGTH);

//fill iv
Byte [AES: : BLOCKSIZE iv];
Memset (iv, 0 x30, AES: : BLOCKSIZE);
Sivs. The size () & lt;=AES: : BLOCKSIZE? Memcpy (iv, sivs c_str (), sIV, the size ()) : memcpy (iv, sIV c_str (), the AES: : BLOCKSIZE);


CBC_Mode & lt; AES & gt; : : Decryption cbcDecryption ((byte *) key, AES: : MAX_KEYLENGTH, iv);

HexDecoder decryptor (new StreamTransformationFilter (cbcDecryption, new StringSink (outstr)));
The decryptor. Put ((byte *) cipherText, strlen (cipherText));
The decryptor. MessageEnd ();

return outstr;
}

STD: : string CBC_CTS_AESEncryptStr (STD: : string sKey, STD: : string sivs, const char * plainText)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related