AES EncryptN BADA solution

216 views Asked by At

I would like to know what could be the problem in the code below.

I'm part of a university team developing solutions in Bada using NFC and server connections. To encrypt the comunication we have though use Aes encryption, but I'm stopped in the following error:

String
ReadForm::aesEncryp(Osp::Base::String ip, Osp::Base::String mac, Osp::Base::String msg)
{
 AppLog("<<<<<<<<<<<<<< aesEncrypt Start >>>>>>>>>>>>>>>>>>>>>>>>>");

 Osp::Text::Utf8Encoding utf8Enc;
 Osp::Text::AsciiEncoding* dato = new Osp::Text::AsciiEncoding();
 result r;



 //msg
 ByteBuffer* pbuffer=dato->GetBytesN(msg);
 AppLog("asigno mensaje: %ls", msg.GetPointer());
 int messageLen; // msg.GetLength()*8;
 r=dato->GetByteCount(msg,messageLen);
 AppLog("obtiene bytes: %s", GetErrorMessage(r));
 byte message[messageLen+1];
 r=pbuffer->GetArray(message,0,messageLen);
 AppLog("Establece mensaje en array: %s", GetErrorMessage(r));


 // KEY: 16 bytes
 int secretKeyLen;
 ip="xxxxxxxxxxxxx";
 r=dato->GetByteCount(ip,secretKeyLen);
 AppLog("obtiene bytes: %s", GetErrorMessage(r));
 pbuffer=null;
 pbuffer=dato->GetBytesN(ip);
    byte secretKey[secretKeyLen+1];
 r=pbuffer->GetArray(secretKey,0,secretKeyLen);
 AppLog("Establece key en array: %s", GetErrorMessage(r));


 // IV: 16 bytes
 int ivectorLen;
 String ivM="xxxxxxxxxxxx"; 
 pbuffer=null;
 pbuffer=dato->GetBytesN(ivM);
 r=dato->GetByteCount(ivM,ivectorLen);
 AppLog("obtiene bytes: %s", GetErrorMessage(r));
 byte ivector[ivectorLen+1];
 r=pbuffer->GetArray(ivector,0,ivectorLen);
 AppLog("Establece vector en array: %s", GetErrorMessage(r));


 String transformation;
 ISymmetricCipher *pCipher = null;
 SecretKeyGenerator *pKeyGen = null;
 ISecretKey *pKey = null;
 ByteBuffer input;
 ByteBuffer *pOutput = null;
 ByteBuffer keyBytes;
 ByteBuffer iv;
 //msg
 input.Construct(messageLen+1);
 input.SetArray(message, 0, messageLen);
 input.Flip();
 //key
 keyBytes.Construct(secretKeyLen+1);
 keyBytes.SetArray(secretKey, 0, 16);
 keyBytes.Flip();
 //vector
 iv.Construct(ivectorLen);
 iv.SetArray(ivector, 0, ivectorLen);
 iv.Flip();

 //cifrado
 pCipher = new AesCipher();
 transformation = "CBC/128/NOPADDING";
 pCipher->Construct(transformation,CIPHER_ENCRYPT);
 AppLog("AesCipher construct:%s", GetErrorMessage(r));
 pKeyGen = new SecretKeyGenerator();
 pKeyGen->Construct(keyBytes);
 pKey = pKeyGen->GenerateKeyN();
 if (pKey==null){
   r = GetLastResult();
   AppLog("Generate -> pKey Esnulo: %s",GetErrorMessage(r));

  }

 r=pCipher->SetKey(*pKey);
 AppLog("AesCipher setKey:%s", GetErrorMessage(r));

 r=pCipher->SetInitialVector(iv);
 AppLog("AesCipher setInitialVector:%s", GetErrorMessage(r));

 //encripto

 pOutput = pCipher->EncryptN(input);    <---------- returns null  why!!!!!!!!!!!
 if (pOutput==null){
  r = GetLastResult();
  AppLog("pOutput nulo: %s",GetErrorMessage(r));

 }
 AppLog("Encriptado");


 //preparo para devolver
 Osp::Text::AsciiEncoding* as = new Osp::Text::AsciiEncoding();
 as->GetString(*pOutput,ResultadoAes);

 AppLog("aes= %ls", ResultadoAes.GetPointer() );
 AppLog("<<<<<<<<<<<<<< aesEncrypt Finish >>>>>>>>>>>>>>>>>>>>>>>>>");
 return ResultadoAes;
}
1

There are 1 answers

0
Ashish On

The input and keylength should be multiple of 16. What i understand from your code, your message length is multiple of 8.

Also since you are using NOPADDING, when you are constructing bytebuffer for input and keybytes, please make sure that they are exactly multiple of 16.

Finally, incase you are still getting problem, please post what error message you are getting from r = GetLastResult()