Aes string encryption

86 views
Skip to first unread message

Fil Nick

unread,
Feb 23, 2015, 8:16:06 AM2/23/15
to cryptop...@googlegroups.com
How would it be possible to encrypt and decrypt strings in AES.
Or in short how can one replicate the functionality of the code below in AES.
http://pastebin.com/Gw0RLBNR
I've tried to do this for a very long time.I have also done more than more fair share of looking around. None of my attempts seem to work correctly and they will not compiler under vs2012 or code blocks. So all I ask is this does someone have an example anything?

Jean-Pierre Münch

unread,
Feb 23, 2015, 11:26:26 AM2/23/15
to cryptop...@googlegroups.com
Hey Fil,

if you want to use Crypto++ there're some issues with this code, I'll come back to them after answering your initial question.

#include <aes.h>
#include <modes.h>
#include <string>

using namespace CryptoPP;
using namespace std;

void EncryptAES(
const string& In,const string& Key,string* Out)
{
   ECB_Mode<AES>::Encryption Encryptor(Key);
   // resize Out to be a multiple of the blocksize
   Encryptor.ProcessData(*Out,In,In.size());
}

void DecryptAES
(const string& In,const string& Key,string* Out)
{

   ECB_Mode<AES>::Decryption Decryptor(Key);
   // resize Out to be a multiple of the blocksize
   Decryptor.ProcessData(*Out,In,In.size());

}

This will work and encrypt you some data with AES.

However it is not secure.
  1. You may not want to encrypt each block seperately but rather combine them somehow. You should consider using CTR mode if not authentication is required and GCM mode if authentication is required. You'll then also need an unique IV for each message.
  2. You may not want to use std::string s to hold sensitive data (such as keys and plaintext), so you should consider using SecByteBlock and FixedSizeSecBlock<byte,X> to transmit data. (as otherwise secret data may be leaked to hard-drive or other porgrams after de-allocation)
  3. You may not wanto to call an encryption routine with a high-level object. In my opinion the parameters should be (const byte* In, const byte* Key, byte* Out, const byte* IV), to reach maximal portability or at least use something like SecByteBlock (passed by reference)

BR

JPM

Fil Nick

unread,
Feb 23, 2015, 8:13:30 PM2/23/15
to cryptop...@googlegroups.com


N
:\code\Aes\aes3>cl aes.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

aes
.cpp
F
:\VISUALCPP\VC\INCLUDE\xlocale(336) : warning C4530: C++ exception handler used
, but unwind semantics are not enabled. Specify /EHsc
aes
.cpp(10) : error C2664: 'CryptoPP::CipherModeFinalTemplate_CipherHolder<CIPHE
R,BASE>::CipherModeFinalTemplate_CipherHolder(const CryptoPP::CipherModeFinalTem
plate_CipherHolder<CIPHER,BASE> &)'
: cannot convert parameter 1 from 'const std
::string'
to 'const CryptoPP::CipherModeFinalTemplate_CipherHolder<CIPHER,BASE>
&'

       
with
       
[
            CIPHER
=CryptoPP::Rijndael::Encryption,
            BASE
=CryptoPP::ECB_OneWay
       
]
       
Reason: cannot convert from 'const std::string' to 'const CryptoPP::Ciph
erModeFinalTemplate_CipherHolder<CIPHER,BASE>'

       
with
       
[
            CIPHER
=CryptoPP::Rijndael::Encryption,
            BASE
=CryptoPP::ECB_OneWay
       
]
       
No user-defined-conversion operator available that can perform this conv
ersion
, or the operator cannot be called
aes
.cpp(12) : error C2664: 'CryptoPP::ECB_OneWay::ProcessData' : cannot convert
parameter
1 from 'std::string' to 'byte *'
       
No user-defined-conversion operator available that can perform this conv
ersion
, or the operator cannot be called
aes
.cpp(17) : error C2664: 'CryptoPP::CipherModeFinalTemplate_CipherHolder<CIPHE
R,BASE>::CipherModeFinalTemplate_CipherHolder(const CryptoPP::CipherModeFinalTem
plate_CipherHolder<CIPHER,BASE> &)'
: cannot convert parameter 1 from 'const std
::string'
to 'const CryptoPP::CipherModeFinalTemplate_CipherHolder<CIPHER,BASE>
&'

       
with
       
[
            CIPHER
=CryptoPP::Rijndael::Decryption,
            BASE
=CryptoPP::ECB_OneWay
       
]
       
Reason: cannot convert from 'const std::string' to 'const CryptoPP::Ciph
erModeFinalTemplate_CipherHolder<CIPHER,BASE>'

       
with
       
[
            CIPHER
=CryptoPP::Rijndael::Decryption,
            BASE
=CryptoPP::ECB_OneWay
       
]
       
No user-defined-conversion operator available that can perform this conv
ersion
, or the operator cannot be called
aes
.cpp(19) : error C2664: 'CryptoPP::ECB_OneWay::ProcessData' : cannot convert
parameter
1 from 'std::string' to 'byte *'
       
No user-defined-conversion operator available that can perform this conv
ersion
, or the operator cannot be called

N
:\code\Aes\aes3>

 
 

Jean-Pierre Münch

unread,
Feb 24, 2015, 10:42:40 AM2/24/15
to cryptop...@googlegroups.com
I'm sorry about this but I though you'd expect this as the types aren't compliant(what you may have noticed with an IDE).
I'll correct the code below:

#include <aes.h>
#include <modes.h>
#include <string>

using namespace CryptoPP;
using namespace std;

void EncryptAES(string& In,const string& Key,string* Out)// out is assumed to be empty
{
   ECB_Mode<AES>::Encryption Encryptor(Key.c_str(),Key.size());
   while(In.size() % AES::Encryption::BLOCKSIZE != 0)
// resize Out to be a multiple of the blocksize
    In.append('\0',1);
  
   SecByteBlock OutBuffer(In.size());
   Encryptor.ProcessData(In.c_str()
,OutBuffer,In.size()); // please verify directions in original code
   Out->append(OutBuffer,OutBuffer.size());
}

void DecryptAES
(string& In,const string& Key,string* Out) // out is assumed to be empty
{

   ECB_Mode<AES>::Decryption Decryptor(Key.c_str(),Key.size());
   while(In.size() % AES::Encryption::BLOCKSIZE != 0) // resize Out to be a multiple of the blocksize   
    In.append('\0',1);

   SecByteBlock OutBuffer(In.size());
   Decryptor.ProcessData(
In.c_str(),OutBuffer,In.size()); // please verify directions in original code
   Out->append(OutBuffer,
OutBuffer.size());
}


BR

JPM

Fil Nick

unread,
Mar 20, 2015, 7:33:48 AM3/20/15
to cryptop...@googlegroups.com
Damn it creates a mile of errors.
Reply all
Reply to author
Forward
0 new messages