Hi all,
i am trying to implement security related functoinality in my application
my code consists of encryption & decryption of data, my code is working fine for encyprion but while decrypting application is getting crashed
due to E_SYSTEM unknown system error
please help me here is my code
#include "encryptSampleMainForm.h"
#include "AppResourceId.h"
#include <FIo.h>
#include <FSecurity.h>
using namespace Tizen::Base;
using namespace Tizen::App;
using namespace Tizen::Ui;
using namespace Tizen::Ui::Controls;
using namespace Tizen::Ui::Scenes;
using namespace Tizen::Security;
using namespace Tizen::Security::Crypto;
using namespace Tizen::Base::Utility;
String str; //global var holds the output of encryption function ,later the same value is sent for decrytpion
encryptSampleMainForm::encryptSampleMainForm(void)
{
}
encryptSampleMainForm::~encryptSampleMainForm(void)
{
}
bool
encryptSampleMainForm::Initialize(void)
{
Construct(IDF_FORM);
return true;
}
result
encryptSampleMainForm::OnInitializing(void)
{
result r = E_SUCCESS;
// TODO:
// Add your initialization code here
// Setup back event listener
SetFormBackEventListener(this);
// Get a button via resource ID
Tizen::Ui::Controls::Button *pButtonOk = static_cast<Button*>(GetControl(IDC_BUTTON_OK));
if (pButtonOk != null)
{
pButtonOk->SetActionId(ID_BUTTON_OK);
pButtonOk->AddActionEventListener(*this);
}
Button* pButton1 = static_cast<Button*>(GetControl(IDC_BUTTON1));
if(pButton1)
{
pButton1->SetActionId(1029);
pButton1->AddActionEventListener(*this);
}
return r;
}
result
encryptSampleMainForm::OnTerminating(void)
{
result r = E_SUCCESS;
// TODO:
// Add your termination code here
return r;
}
void
encryptSampleMainForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
{
if(actionId==ID_BUTTON_OK) //action related to encryption
{
ByteBuffer *p=StringUtil::StringToUtf8N("e");
p->SetLimit(p->GetCapacity()-1);
str=encyrptKey(*p);
}
else if(actionId==1029) //action related to decryption
{
ByteBuffer *p=StringUtil::StringToUtf8N("e");
p->SetLimit(p->GetCapacity()-1);
decryptKey(str,*p);
}
}
void
encryptSampleMainForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
{
UiApp* pApp = UiApp::GetInstance();
AppAssert(pApp);
pApp->Terminate();
}
void
encryptSampleMainForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
{
// TODO:
// Add your scene activate code here
AppLog("OnSceneActivatedN");
}
void
encryptSampleMainForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
const Tizen::Ui::Scenes::SceneId& nextSceneId)
{
// TODO:
// Add your scene deactivate code here
AppLog("OnSceneDeactivated");
}
String encryptSampleMainForm::encyrptKey(ByteBuffer &pwd)
{
ByteBuffer* inp=StringUtil::StringToUtf8N("e");
inp->SetLimit(inp->GetCapacity()-1);
Md5Hash mhash;
ByteBuffer *pOutput=mhash.GetHashN(*inp);
AppLog("ouptut is %s",pOutput->GetPointer());
String sHash;
{
int byteCount = pOutput->GetLimit();
for (int i = 0; i < byteCount; i++) {
byte b; pOutput -> GetByte(i, b);
unsigned int ui = b;
String sHex;
sHex.Format(25, L"%02x", ui);
sHash.Append(sHex);
}
AppLog("Shex is %ls",sHash.GetPointer());
}
ByteBuffer* o2=StringUtil::StringToUtf8N(sHash);
o2->SetLimit(o2->GetCapacity()-1);
AppLog("encrypt Button is clicked!");
ISecureRandom *prandom=new AesSecureRandom();
SecretKeyGenerator *keygen=new SecretKeyGenerator();
ByteBuffer *prn=prandom->GenerateRandomBytesN(32);
AppLog("rnd no is %s",prn->GetPointer());
AesCipher *asc=new AesCipher();
asc->Construct("ECB/256/PKCS7PADDING",CIPHER_ENCRYPT);
keygen->Construct(*o2);
ISecretKey *skey2=keygen->GenerateKeyN();
asc->SetKey(*skey2);
out=asc->EncryptN(*prn);
AppLog("encrypted data is %s",out->GetPointer());
String str2=reinterpret_cast< char* >(const_cast< byte* >(out->GetPointer()));
return str2;
}
ByteBuffer* encryptSampleMainForm::decryptKey(String pwdkey,ByteBuffer &p)
{
Md5Hash mhash;
ByteBuffer* inp=StringUtil::StringToUtf8N("e");
inp->SetLimit(inp->GetCapacity()-1);
SecretKeyGenerator *keygen=new SecretKeyGenerator();
ByteBuffer *pOutput=mhash.GetHashN(*inp);
AppLog("ouptut is %s",pOutput->GetPointer());
String sHash;
{
int byteCount = pOutput->GetLimit();
for (int i = 0; i < byteCount; i++) {
byte b; pOutput -> GetByte(i, b);
unsigned int ui = b;
String sHex;
sHex.Format(25, L"%02x", ui);
sHash.Append(sHex);
}
AppLog("Shex is %ls",sHash.GetPointer());
}
ByteBuffer* o2=StringUtil::StringToUtf8N(sHash);
o2->SetLimit(o2->GetCapacity()-1);
AppLog("decrypt button clicked!");
AesCipher *asc=new AesCipher();
asc->Construct("ECB/256/PKCS7PADDING",CIPHER_DECRYPT);
keygen->Construct(*o2);
ISecretKey *skey1=keygen->GenerateKeyN();
asc->SetKey(*skey1);
ByteBuffer* out1=StringUtil::StringToUtf8N(pwdkey);
out1->SetLimit(out1->GetCapacity()-1);
ByteBuffer *output=asc->DecryptN(*out1);
AppLog("decrypted data is %s",output->GetPointer());
return out1;
}