| ID | CS000944 | Creation date | May 8, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N93 |
| Category | Open C | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): RC4_KEY, MD5(), RC4_set_key(), RC4() |
This snippet shows how the OpenSSL crypto library can be used to encrypt and decrypt descriptor content using a pass phrase. The example functions openc_encrypt() encrypts and openc_decrypt() decrypts a string of characters using Open C libcrypt.
Note: In order to use this code, you need to install Open C plug-in.
This snippet can be self-signed.
The following libraries are required:
LIBRARY libc.lib
LIBRARY euser.lib
LIBRARY libcrypto.lib
#include <stddef.h>
#include <openssl/rc4.h>
#include <openssl/md5.h>
//#include<filelogger.h>
void openc_encrypt(int len, unsigned char* in, unsigned char* crypted, unsigned char*
password, int passlen)
{
unsigned char digest[MD5_DIGEST_LENGTH];
RC4_KEY key;
MD5(password, passlen, digest);
RC4_set_key(&key, MD5_DIGEST_LENGTH, digest);
RC4(&key, len, in, crypted);
}
void openc_decrypt(int len, unsigned char* in, unsigned char* decrypted, unsigned
char* password, int passlen)
{
unsigned char digest[MD5_DIGEST_LENGTH];
RC4_KEY key;
MD5(password, passlen, digest);
RC4_set_key(&key, MD5_DIGEST_LENGTH, digest);
RC4(&key, len, in, decrypted);
}
/////////////////////////////////////////////////////////////////////////////////////
const TInt KMaxTextLen = 100;
_LIT8(KExampleText, "Encrypt this text!");
_LIT8(KPassWord, "secret");
void doExampleL()
{
TBuf8<KMaxTextLen> buffer(KExampleText);
HBufC8* password = KPassWord().AllocLC();
HBufC8* crypted = HBufC8::NewLC(buffer.Size());
TPtr8 cryptedPtr = crypted->Des();
cryptedPtr.SetLength(buffer.Size());
openc_encrypt(buffer.Size(),
(unsigned char*)buffer.Ptr(),
(unsigned char*)cryptedPtr.Ptr(),
(unsigned char*)password->Ptr(),
password->Size());
//LOGDES16(buffer); //Encrypt this text!
//LOGDES8(*crypted) ; //Ór›Â Û¦ }ÖŠ4 b q
HBufC8* decrypted = HBufC8::NewLC(buffer.Size());
TPtr8 decryptedPtr = decrypted->Des();
buffer.Copy(cryptedPtr);
decryptedPtr.SetLength(buffer.Size());
openc_decrypt(buffer.Size(),
(unsigned char*)buffer.Ptr(),
(unsigned char*)decryptedPtr.Ptr(),
(unsigned char*)password->Ptr(),
password->Size());
//LOGDES16(buffer); //Ór›Â Û¦ }ÖŠ4 b q
//LOGDES8(*decrypted) ; //Encrypt this text!
CleanupStack::PopAndDestroy(3); //decrypted, crypted, password
}
The example text has been encrypted with the pass phrase and after that decrypted with the same pass phrase using the example functions openc_encrypt() and openc_decrypt().
No related wiki articles found