You Are Here:

Community: Wiki

This page was last modified on 9 October 2008, at 20:04.

CS000944 - Encrypting and decrypting text with Open C

From Forum Nokia Wiki



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

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

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fHttpE3aE2fE2f217E2e218E2e225E2e2E3a2082E2findeE78E2ehtmlE253FX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ