Click here to Skip to main content
13,289,439 members (63,075 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 8 Jul 2005

The RC4 algorithm within a COM component

, 11 Jul 2005
Rate this:
Please Sign up or sign in to vote.
The RC4 algorithm within a COM component made with C++ and ATL.


The Secure Storage component is made like a COM component with ATL. The component is encapsulating the RC4 stream cipher algorithm, which is placed in separated source and header files (the rc4.cpp and rc4.h files).

Basically the component is made on behalf of the needs of storing credentials in a way that is not readable to the human eye. When it was decided to use the RC4 algorithm then it was known that it wasn’t the hardest encryption that was chosen, but it needed to be a relative fast encryption. The algorithm could be improved. Changing it, to be using block cipher, could do this.

When using the RC4 algorithm it is obvious that it will return a cipher text with the same length as the clear text. This is definitely a disadvantage with this algorithm. This could be improved by padding the clear text before the encryption is made.

Before the encryption can take place it is necessary to set up the key, which should be used to perform the encryption. This is done with the method that matches the signature below:

prepare_key(unsigned char *key_data_ptr, int key_data_len, rc4_key *key)

The method takes a textual key (the pass phrase) and the length of that and returns a pointer to a struct, which is the "real" RC4 encryption key. After the key has been generated we are ready to perform the encryption. The encryption is made with the method that matches the signature below:

void rc4(unsigned char *buffer_ptr, int buffer_len, rc4_key *key)

It takes the clear text and the length of it (the clear text) as input parameters together with the RC4 encryption key, which we just created before. The method returns the cipher text within the same pointer that was used for the clear text.

Using the code

These two methods are (as mentioned before) encapsulated within an ATL COM component. The component has two kinds of interfaces: one that is just returning the result of the encryption/decryption, when it returns.

set obj = CreateObject("SECURESTORAGE.Secure")
cipher = obj.Encrypt ("encryption key or pass phrase", "a value to be stored")
MsgBox (obj.Decrypt ("encryption key or pass phrase", cipher))

and another one that stores the cipher text in the registry database. The cipher text can later be fetched in a second round-trip.

set obj = CreateObject("SECURESTORAGE.Secure")
obj.RegistryKey = "registry key"
obj.EncryptToRegistry "encryption key or pass phrase", _
                  "registry key", "a value to be stored"
clear = obj.DecryptFromRegistry("encryption key or pass phrase", "registry key")
MsgBox clear

The values in the registry are stored under KEY_LOCAL_MACHINE\SOFTWARE\SecureStorage. If a registry key is provided, the values will be stored under that key below the SecureStorage key.

Points of interest

When using the RC4, it is not obvious that the prepare_key method always has to be used before the rc4 method. Furthermore, please notice that I use the same method for decryption as for encryption. This is possible because it is a symmetric key encryption algorithm.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Denmark Denmark
I'm an independent researcher and software architect, who holds a master's degree from Aarhus University, Denmark. I have more than 10 yrs of commercial experience. I am specialized in the C++ programming language, software architectures and crypto systems.

Support & help!forum/nielsen-tools-support

You may also be interested in...

Comments and Discussions

Generalusing the COM file Pin
breakpoint27-Jul-06 21:26
memberbreakpoint27-Jul-06 21:26 
GeneralRe: using the COM file Pin
Jessn27-Jul-06 22:22
memberJessn27-Jul-06 22:22 
GeneralProblem with cutting text Pin
JohanEkwall10-Aug-05 6:05
memberJohanEkwall10-Aug-05 6:05 
GeneralRe: Problem with cutting text Pin
Jessn11-Aug-05 0:08
memberJessn11-Aug-05 0:08 
GeneralRe: Problem with cutting text Pin
hoomb4-Dec-05 8:55
memberhoomb4-Dec-05 8:55 
GeneralRe: Problem with cutting text Pin
Jessn9-Jan-06 6:20
memberJessn9-Jan-06 6:20 
GeneralCool, ... C++ Dude Pin
GUYFERD_224-Jul-05 21:02
memberGUYFERD_224-Jul-05 21:02 
GeneralRe: Cool, ... C++ Dude Pin
Jessn26-Jul-05 9:00
memberJessn26-Jul-05 9:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 11 Jul 2005
Article Copyright 2005 by Jessn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid