Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

Hi pple..

Can someone assist me on implemetation of data encryption using ICipher1 ?
And how to initialize "initialization vector" of "CIPHER_PARAM_IV ".

Any sample code would be higly appreciated.

Thanks.
Rohit

I finally figured it out !!!

I finally figured it out !!!

Hi Rohit,
can u pls provide with the sample using the ICipher1 ? I am facing problems since its not able to identify the AEECLSID_CIPHERMODE_ECB and AEECLSID_BLOCK_DES although I have added the necessary .h files

Hi Rohit,
can u pls provide with the sample using the ICipher1 ? I am facing problems since its not able to identify the AEECLSID_CIPHERMODE_ECB and AEECLSID_BLOCK_DES although I have added the necessary .h files

Can anyone pls tell me how to use the ICipher1 set of API's,a sample illustrating the same would be helpful

Can anyone pls tell me how to use the ICipher1 set of API's,a sample illustrating the same would be helpful

Hi Sandeep....
I used AEECLSID_ARC4 and it was working fine for me.
As you are little unsure about the implementation of ICipher1 , iam copying the code that worked for me.
Its a sample code that i made to learn the implementation of ICipher1.
************************************************************
#include "AEEModGen.h" // Module interface definitions
#include "AEEAppGen.h" // Applet interface definitions
#include "AEEShell.h" // Shell interface definitions
#include "AEEDB.h"
#include "ICipherFactory.h"
#include "ICipher1.h"
#include "AEESecurity.h"
#include "AEEstdlib.h"
#include "STREAM_CIPHER.bid"
#include "CIPHERFACTORY.bid"
#include "AEEMenu.h" // Database interface definitions
#include "encrypt.bid"
typedef struct _encrypt {
AEEApplet a ; // First element of this structure must be AEEApplet
AEEDeviceInfo DeviceInfo; // always have access to the hardware device information
ICipher1 *cipher;
ICipherFactory *cfac;
ICipher *m_pICipher;
IMenuCtl * m;
ICipher1 *cipher2;
// add your own variables here...
} encrypt;
//Handle event
static boolean encrypt_HandleEvent(encrypt* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
int code;
char cipher[10] = {0};
const char *bufin="hey whats going on";
char *bufout;
unsigned a=0;
char *bufresult;
switch (eCode)
{
// App is told it is starting up
case EVT_APP_START:
if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_ARC4, (void**) &(pMe->m_pICipher))==SUCCESS)
{
DBGPRINTF("OLDcipher initialized");
}
else
{
DBGPRINTF("OLDcipher not initialized");
return FALSE;
}
if(ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_CIPHERFACTORY,(void **) &(pMe->cfac))==SUCCESS)
{
DBGPRINTF("cipher initialized");
}
else
DBGPRINTF("cipher initialized");
if((code=ICIPHERFACTORY_CreateCipher(pMe->cfac,AEECLSID_STREAM_ARC4, CIPHER_DIRECTION_ENCRYPT ,0,NULL,&(pMe->cipher)))==SUCCESS)
{
DBGPRINTF(" cipher created");
}
else
DBGPRINTF("cipher not created");
DBGPRINTF("%d",code);
cipher[0] = 0x7d;
cipher[1] = 0x69;
cipher[2] = 0x70;
cipher[3] = 0x27;
cipher[4] = 0x60;
cipher[5] = 0x40;
cipher[6] = 0x20;
cipher[7] = 0x70;
ICIPHER1_SetParam(pMe->cipher,CIPHER_PARAM_KEY,(char*)cipher,STRLEN(cipher));
bufout=(char*)MALLOC(STRLEN(bufin)+1);
a=STRLEN(bufin)+1;
ICIPHER1_Process(pMe->cipher, (byte *)bufin,STRLEN(bufin),(byte *)bufout,&a);
DBGPRINTF("%s%s%x",bufin,":",bufout);
ICIPHER1_Release(pMe->cipher);
if((code=ICIPHERFACTORY_CreateCipher(pMe->cfac,AEECLSID_STREAM_ARC4, CIPHER_DIRECTION_DECRYPT,0,NULL,&(pMe->cipher2)))==SUCCESS)
{
DBGPRINTF(" cipher decrypt created");
}
else
DBGPRINTF("cipher decrypt not created");
ICIPHER1_SetParam(pMe->cipher2,CIPHER_PARAM_KEY,(char*)cipher,STRLEN(cipher));
a=STRLEN(bufout)+1;
bufresult=(char*)MALLOC(a);
ICIPHER1_Process(pMe->cipher2,(byte *)bufout,STRLEN(bufout),(byte *)bufresult,&a);
DBGPRINTF("%s%s%s","result",":",bufresult);
// Add your code here...
return(TRUE);
.......
.....
....
...
**************************************************************
I hope it helps.........
Let me know if you have any more concerns.

Hi Sandeep....
I used AEECLSID_ARC4 and it was working fine for me.
As you are little unsure about the implementation of ICipher1 , iam copying the code that worked for me.
Its a sample code that i made to learn the implementation of ICipher1.
************************************************************
#include "AEEModGen.h" // Module interface definitions
#include "AEEAppGen.h" // Applet interface definitions
#include "AEEShell.h" // Shell interface definitions
#include "AEEDB.h"
#include "ICipherFactory.h"
#include "ICipher1.h"
#include "AEESecurity.h"
#include "AEEstdlib.h"
#include "STREAM_CIPHER.bid"
#include "CIPHERFACTORY.bid"
#include "AEEMenu.h" // Database interface definitions
#include "encrypt.bid"
typedef struct _encrypt {
AEEApplet a ; // First element of this structure must be AEEApplet
AEEDeviceInfo DeviceInfo; // always have access to the hardware device information
ICipher1 *cipher;
ICipherFactory *cfac;
ICipher *m_pICipher;
IMenuCtl * m;
ICipher1 *cipher2;
// add your own variables here...
} encrypt;
//Handle event
static boolean encrypt_HandleEvent(encrypt* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
int code;
char cipher[10] = {0};
const char *bufin="hey whats going on";
char *bufout;
unsigned a=0;
char *bufresult;
switch (eCode)
{
// App is told it is starting up
case EVT_APP_START:
if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_ARC4, (void**) &(pMe->m_pICipher))==SUCCESS)
{
DBGPRINTF("OLDcipher initialized");
}
else
{
DBGPRINTF("OLDcipher not initialized");
return FALSE;
}
if(ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_CIPHERFACTORY,(void **) &(pMe->cfac))==SUCCESS)
{
DBGPRINTF("cipher initialized");
}
else
DBGPRINTF("cipher initialized");
if((code=ICIPHERFACTORY_CreateCipher(pMe->cfac,AEECLSID_STREAM_ARC4, CIPHER_DIRECTION_ENCRYPT ,0,NULL,&(pMe->cipher)))==SUCCESS)
{
DBGPRINTF(" cipher created");
}
else
DBGPRINTF("cipher not created");
DBGPRINTF("%d",code);
cipher[0] = 0x7d;
cipher[1] = 0x69;
cipher[2] = 0x70;
cipher[3] = 0x27;
cipher[4] = 0x60;
cipher[5] = 0x40;
cipher[6] = 0x20;
cipher[7] = 0x70;
ICIPHER1_SetParam(pMe->cipher,CIPHER_PARAM_KEY,(char*)cipher,STRLEN(cipher));
bufout=(char*)MALLOC(STRLEN(bufin)+1);
a=STRLEN(bufin)+1;
ICIPHER1_Process(pMe->cipher, (byte *)bufin,STRLEN(bufin),(byte *)bufout,&a);
DBGPRINTF("%s%s%x",bufin,":",bufout);
ICIPHER1_Release(pMe->cipher);
if((code=ICIPHERFACTORY_CreateCipher(pMe->cfac,AEECLSID_STREAM_ARC4, CIPHER_DIRECTION_DECRYPT,0,NULL,&(pMe->cipher2)))==SUCCESS)
{
DBGPRINTF(" cipher decrypt created");
}
else
DBGPRINTF("cipher decrypt not created");
ICIPHER1_SetParam(pMe->cipher2,CIPHER_PARAM_KEY,(char*)cipher,STRLEN(cipher));
a=STRLEN(bufout)+1;
bufresult=(char*)MALLOC(a);
ICIPHER1_Process(pMe->cipher2,(byte *)bufout,STRLEN(bufout),(byte *)bufresult,&a);
DBGPRINTF("%s%s%s","result",":",bufresult);
// Add your code here...
return(TRUE);
.......
.....
....
...
**************************************************************
I hope it helps.........
Let me know if you have any more concerns.

Hi Rohit,
Thanx for the sample,it helped me a lot,I am now able to encrypt and decrypt data of block lengths greater than 8 bytes using AEECLSID_BLOCK_DES but if we give a data which is less than or equal to 8 bytes it cribs and the encryption itself is unsuccessful,any suggestions why such a thing is happening?
for ur info,I am using CIPHER_PADDING_ZERO and Chaining mode as AEECLSID_CIPHERMODE_ECB.If u need any further inputs regarding my code/prob pls specify.

Hi Rohit,
Thanx for the sample,it helped me a lot,I am now able to encrypt and decrypt data of block lengths greater than 8 bytes using AEECLSID_BLOCK_DES but if we give a data which is less than or equal to 8 bytes it cribs and the encryption itself is unsuccessful,any suggestions why such a thing is happening?
for ur info,I am using CIPHER_PADDING_ZERO and Chaining mode as AEECLSID_CIPHERMODE_ECB.If u need any further inputs regarding my code/prob pls specify.

The link whatever u have specified dint take me to a valid page :)

The link whatever u have specified dint take me to a valid page :)

Hey Buddy...
I havent't tried AEECLSID_BLOCK_DES..... so i would not be the right person to comment on that....
Is there anything else i can help you with?

Hey Buddy...
I havent't tried AEECLSID_BLOCK_DES..... so i would not be the right person to comment on that....
Is there anything else i can help you with?

Can u pls tell me how to initialise the IV?

Can u pls tell me how to initialise the IV?

ICipher* desCipher;
--
-
-
-
-
if( ICIPHER_SetParam( desCipher,
CIPHER_PARAM_KEY,
encKey,
encKeySize() ) == SUCCESS )
{
byte* buffer = ( byte* ) MALLOC( DES_BLOCK_SIZE + plainData.getSize() );
if( ICIPHER_Process(desCipher,
plainData,
plainDataSize(),
buffer,
&bytesEncrypted ) == SUCCESS )
{
MEMCPY( encryptedBuffer, buffer, bytesEncrypted );
encryptedBytes += bytesEncrypted;
success = true;
}
bytesEncrypted = DES_BLOCK_SIZE;
if( ( res = ICIPHER_ProcessLast( desCipher,
buffer,
&bytesEncrypted ) ) == SUCCESS )
{
MEMCPY( encryptedBuffer + encryptedBytes, buffer, bytesEncrypted );
}
}
@sandeep
the code in bold will ensure that you will get encrypted data if the plain data is less than or equal to 8.

ICipher* desCipher;
--
-
-
-
-
if( ICIPHER_SetParam( desCipher,
CIPHER_PARAM_KEY,
encKey,
encKeySize() ) == SUCCESS )
{
byte* buffer = ( byte* ) MALLOC( DES_BLOCK_SIZE + plainData.getSize() );
if( ICIPHER_Process(desCipher,
plainData,
plainDataSize(),
buffer,
&bytesEncrypted ) == SUCCESS )
{
MEMCPY( encryptedBuffer, buffer, bytesEncrypted );
encryptedBytes += bytesEncrypted;
success = true;
}
bytesEncrypted = DES_BLOCK_SIZE;
if( ( res = ICIPHER_ProcessLast( desCipher,
buffer,
&bytesEncrypted ) ) == SUCCESS )
{
MEMCPY( encryptedBuffer + encryptedBytes, buffer, bytesEncrypted );
}
}
@sandeep
the code in bold will ensure that you will get encrypted data if the plain data is less than or equal to 8.

sandeep wrote:Can u pls tell me how to initialise the IV?
byte initializationVector[8];
initializationVector[0] = 0x0;
initializationVector[1] = 0x0;
initializationVector[2] = 0x0;
initializationVector[3] = 0x0;
initializationVector[4] = 0x0;
initializationVector[5] = 0x0;
initializationVector[6] = 0x0;
initializationVector[7] = 0x0;
// set the key here
if( ( res = ICIPHER_SetParam( desCipher,
CIPHER_PARAM_IV,
initializationVector, 8 ) ) == SUCCESS )
after this do the normal steps for encryption....
try this, it will help you

sandeep wrote:Can u pls tell me how to initialise the IV?
byte initializationVector[8];
initializationVector[0] = 0x0;
initializationVector[1] = 0x0;
initializationVector[2] = 0x0;
initializationVector[3] = 0x0;
initializationVector[4] = 0x0;
initializationVector[5] = 0x0;
initializationVector[6] = 0x0;
initializationVector[7] = 0x0;
// set the key here
if( ( res = ICIPHER_SetParam( desCipher,
CIPHER_PARAM_IV,
initializationVector, 8 ) ) == SUCCESS )
after this do the normal steps for encryption....
try this, it will help you

Has anybody tried this API in real fones....
i was trying to run a test crypto app, which instantiate cipher factory in latest MOTO KRZR K1M , which supports BREW 3.1.4, the factory is not instantiated ....
says "class not supported"
int res = ISHELL_CreateInstance( ((AEEApplet*) GETAPPINSTANCE())->m_pIShell,
AEECLSID_CIPHERFACTORY,
(void**) &desCipherFactory_ );
function returns error code 3.
works fine in emulator, 3.1.4...
no luck in fone....
any body knows the reason why

Has anybody tried this API in real fones....
i was trying to run a test crypto app, which instantiate cipher factory in latest MOTO KRZR K1M , which supports BREW 3.1.4, the factory is not instantiated ....
says "class not supported"
int res = ISHELL_CreateInstance( ((AEEApplet*) GETAPPINSTANCE())->m_pIShell,
AEECLSID_CIPHERFACTORY,
(void**) &desCipherFactory_ );
function returns error code 3.
works fine in emulator, 3.1.4...
no luck in fone....
any body knows the reason why