Click here to Skip to main content
15,885,801 members
Articles / Desktop Programming / MFC
Article

Fast Simple Secure Encryption Routine

Rate me:
Please Sign up or sign in to vote.
3.73/5 (9 votes)
28 Jul 20021 min read 158.6K   40   25
A simple secure algorithm that can be used in your programs

Introduction

This Crypt routine uses an algorithm created by Rodney Thayer. This algorithm can use keys of various sizes. With a 40-bit key (5 characters) it can be freely exported from the U.S. The cipher is considered robust with 128 bits of key material but can use up to 2048 bits. It is compatible with RSA’s RC4 algorithm.

void Crypt(TCHAR *inp, DWORD inplen, TCHAR* key = "", DWORD keylen = 0)
{
    //we will consider size of sbox 256 bytes
    //(extra byte are only to prevent any mishep just in case)
    TCHAR Sbox[257], Sbox2[257];
    unsigned long i, j, t, x;

    //this unsecured key is to be used only when there is no input key from user
    static const TCHAR  OurUnSecuredKey[] = "www.systweak.com" ;
    static const int OurKeyLen = _tcslen(OurUnSecuredKey);    
    TCHAR temp , k;
    i = j = k = t =  x = 0;
    temp = 0;

    //always initialize the arrays with zero
    ZeroMemory(Sbox, sizeof(Sbox));
    ZeroMemory(Sbox2, sizeof(Sbox2));

    //initialize sbox i
    for(i = 0; i < 256U; i++)
    {
        Sbox[i] = (TCHAR)i;
    }

    j = 0;
    //whether user has sent any inpur key
    if(keylen)
    {
        //initialize the sbox2 with user key
        for(i = 0; i < 256U ; i++)
        {
            if(j == keylen)
            {
                j = 0;
            }
            Sbox2[i] = key[j++];
        }    
    }
    else
    {
        //initialize the sbox2 with our key
        for(i = 0; i < 256U ; i++)
        {
            if(j == OurKeyLen)
            {
                j = 0;
            }
            Sbox2[i] = OurUnSecuredKey[j++];
        }
    }

    j = 0 ; //Initialize j
    //scramble sbox1 with sbox2
    for(i = 0; i < 256; i++)
    {
        j = (j + (unsigned long) Sbox[i] + (unsigned long) Sbox2[i]) % 256U ;
        temp =  Sbox[i];                    
        Sbox[i] = Sbox[j];
        Sbox[j] =  temp;
    }

    i = j = 0;
    for(x = 0; x < inplen; x++)
    {
        //increment i
        i = (i + 1U) % 256U;
        //increment j
        j = (j + (unsigned long) Sbox[i]) % 256U;

        //Scramble SBox #1 further so encryption routine will
        //will repeat itself at great interval
        temp = Sbox[i];
        Sbox[i] = Sbox[j] ;
        Sbox[j] = temp;

        //Get ready to create pseudo random  byte for encryption key
        t = ((unsigned long) Sbox[i] + (unsigned long) Sbox[j]) %  256U ;

        //get the random byte
        k = Sbox[t];

        //xor with the data and done
        inp[x] = (inp[x] ^ k);
    }    
}

The only moral here is that we are generating a 256 bit key given from the user. To encrypt we always pick a random byte from the key and encrypt the text with that key. If we have a large pattern of repeating text with this algorithm the key repeat interval (distance between n repetitions) will be approximately of 21700 (2 to the 1700th power).

For very good security I recommend using 128 bits (16 characters) of key material. If you want optimal security you could use 2048 bits (256 characters) of key material (but this is usually considered an overkill).

This routine is constructed from a draft taken from Internet Engineering Task Force (IETF) at <a href="http: www.ietf.cnri.reston.va.us="" home.htm"="">http://www.ietf.cnri.reston.va.us/home.htm

I have tested the routine on a 27 MB file to test that it produce valid decrypted output from input and get the valid results.

Using this routine it is fairly simple.

//to encrypt a text
TCHAR HelloCrypt[] = "Hello Crypt";

//we do not enter the key so we will be using default key provide by the crypt 
//routine
Crypt(HelloCrypt, _tcslen(HelloCrypt));

//now to decrypt
Crypt(HelloCrypt, _tcslen(HelloCrypt));

////////////////////////////////////////////////////////////////////////////////
//if we have to supply a key we have to use crypt as follows
TCHAR Data[] = "abcdefghijklmnopqrstuvwxyz"
TCHAR Key[] = "Strong key";

//Encrypt
Crypt(Data, _tcslen(Data), Key, _tcslen(Data));

//Decrypt
Crypt(Data, _tcslen(Data), Key, _tcslen(Data));
/////////////////////////////////////////////////////////////////////////////

//thats all

Please visit My Web Site for more tutorials, tweaks, reference.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHere a C# version Pin
AticAtac17-Jun-13 2:45
AticAtac17-Jun-13 2:45 
Questionelliptical cryptography encryption and decryption process Pin
baludegala30-Sep-11 3:05
baludegala30-Sep-11 3:05 
Generalimplementation of RSA algorithm in c/c++ Pin
Anuradha Sehgal11-Mar-07 22:35
Anuradha Sehgal11-Mar-07 22:35 
Questionit combine symmetric,asymmetric and hash encryption techniquesneed RSA algorithm in java Pin
Member 369036712-Jan-07 21:06
Member 369036712-Jan-07 21:06 
GeneralElliptical Curve Cryptography Pin
VPNampoothiri19-Mar-06 19:45
VPNampoothiri19-Mar-06 19:45 
GeneralNeed help Pin
Member 207257830-Jun-05 22:03
Member 207257830-Jun-05 22:03 
GeneralImplementation of RSA algorithm in C/C++ Pin
syedmfaisal30-Dec-03 20:56
syedmfaisal30-Dec-03 20:56 
GeneralProblem with code Pin
Al Newton27-Nov-02 12:21
Al Newton27-Nov-02 12:21 
GeneralRe: Problem with code Pin
Anonymous23-Dec-02 18:58
Anonymous23-Dec-02 18:58 
GeneralRe: Problem with code Pin
Peter, The Pumpkin Eater24-Dec-02 7:33
Peter, The Pumpkin Eater24-Dec-02 7:33 
GeneralRe: Problem with code Pin
Fiffi_27-Feb-04 11:30
sussFiffi_27-Feb-04 11:30 
GeneralRe: Problem with code Pin
Peter, The Pumpkin Eater1-Mar-04 8:44
Peter, The Pumpkin Eater1-Mar-04 8:44 
GeneralRe: Problem with code Pin
shankarR9-Sep-04 10:18
shankarR9-Sep-04 10:18 
GeneralRe: Problem with code Pin
Anonymous8-Oct-04 20:37
Anonymous8-Oct-04 20:37 
GeneralRe: Problem with code Pin
shankarR11-Oct-04 3:18
shankarR11-Oct-04 3:18 
GeneralRe: Problem with code Pin
Andy Medi4-Jun-11 7:55
Andy Medi4-Jun-11 7:55 
GeneralSlightly Confused Pin
Anonymous31-Oct-02 12:21
Anonymous31-Oct-02 12:21 
GeneralRe: Slightly Confused Pin
codermallu27-Mar-03 2:28
codermallu27-Mar-03 2:28 
Generalhow Pin
Shotgun14-Sep-02 5:45
Shotgun14-Sep-02 5:45 
Generalsimpler.... Pin
Anonymous24-Aug-02 19:08
Anonymous24-Aug-02 19:08 
GeneralSmall bug in example code Pin
Anonymous8-Aug-02 9:45
Anonymous8-Aug-02 9:45 
GeneralRC4 is not a good choice Pin
C-J Berg30-Jul-02 4:52
C-J Berg30-Jul-02 4:52 
The RC4 algorithm was designed in RSA Labratories by Ron Rivest in 1987. Rodney Thayer and Kalle Kaukonen documented it (read: reverse engineered it) and published it as an Internet Draft (http://www3.pkiclue.com/publications/draft-kaukonen-cipher-arcfour-01.txt); however, I'm not sure they were the first to do it.

It is important to make clear that RC4 already has several known weaknesses, and should therefore not be used today; this includes the implementation presented in this article. There are several better alternatives available, such as the new encryption standard AES (Rijndael), Twofish or Blowfish. Always pick an algorithm that doesn't have known weaknesses (that can't be avoided), and one that has undergone intensive peer review. And if security is of great importance, consult an expert.
GeneralRe: RC4 is not a good choice Pin
Swinefeaster5-Aug-02 13:13
Swinefeaster5-Aug-02 13:13 
GeneralRe: RC4 is not a good choice Pin
Dan Madden5-Aug-02 21:27
Dan Madden5-Aug-02 21:27 
GeneralRe: RC4 is not a good choice Pin
C-J Berg6-Aug-02 1:15
C-J Berg6-Aug-02 1:15 

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.