Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Archived:Encrypting and decrypting text with Open C

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.


Article Metadata
Tested with
Devices(s): Nokia N93
Compatibility
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition (initial release)
Article
Keywords: RC4_KEY, MD5(), RC4_set_key(), RC4()
Created: aknyman (28 Apr 2008)
Last edited: lpvalente (02 Sep 2012)

Contents

Overview

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.

MMP file

The following libraries are required:

LIBRARY libc.lib
LIBRARY euser.lib
LIBRARY libcrypto.lib

Source file

#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
}

Postconditions

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().


This page was last modified on 2 September 2012, at 21:40.
40 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×