Click here to Skip to main content
6,292,811 members and growing! (10,290 online)
Email Password   helpLost your password?
General Programming » Cryptography & Security » Cryptography     Intermediate

Fast Simple Secure Encryption Routine

By Shrishail Rana

A simple secure algorithm that can be used in your programs
VC6, VC7Win2K, WinXP, Visual Studio, MFC, Dev
Posted:28 Jul 2002
Views:100,020
Bookmarked:32 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
9 votes for this article.
Popularity: 3.23 Rating: 3.39 out of 5
1 vote, 16.7%
1
1 vote, 16.7%
2

3
1 vote, 16.7%
4
3 votes, 50.0%
5

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

About the Author

Shrishail Rana


Member

Location: United States United States

Other popular Cryptography & Security articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
Generalimplementation of RSA algorithm in c/c++ PinmemberAnuradha Sehgal23:35 11 Mar '07  
Questionit combine symmetric,asymmetric and hash encryption techniquesneed RSA algorithm in java Pinmember22:06 12 Jan '07  
GeneralElliptical Curve Cryptography PinmemberVPNampoothiri20:45 19 Mar '06  
GeneralNeed help PinmemberZDurrani23:03 30 Jun '05  
GeneralImplementation of RSA algorithm in C/C++ Pinmembersyedmfaisal21:56 30 Dec '03  
GeneralProblem with code PinmemberAl Newton13:21 27 Nov '02  
GeneralRe: Problem with code PinsussAnonymous19:58 23 Dec '02  
GeneralRe: Problem with code PinsussPeter, The Pumpkin Eater8:33 24 Dec '02  
GeneralRe: Problem with code PinsussFiffi_12:30 27 Feb '04  
GeneralRe: Problem with code PinmemberPeter, The Pumpkin Eater9:44 1 Mar '04  
GeneralRe: Problem with code PinmembershankarR11:18 9 Sep '04  
GeneralRe: Problem with code PinsussAnonymous21:37 8 Oct '04  
GeneralRe: Problem with code PinmembershankarR4:18 11 Oct '04  
GeneralSlightly Confused PinsussAnonymous13:21 31 Oct '02  
GeneralRe: Slightly Confused Pinmembercodermallu3:28 27 Mar '03  
Generalhow Pinmembershotgun6:45 14 Sep '02  
Generalsimpler.... PinsussAnonymous20:08 24 Aug '02  
GeneralSmall bug in example code PinsussAnonymous10:45 8 Aug '02  
GeneralRC4 is not a good choice PinmemberCarl Berg5:52 30 Jul '02  
GeneralRe: RC4 is not a good choice PinmemberSwinefeaster14:13 5 Aug '02  
GeneralRe: RC4 is not a good choice PinmemberDaniel Madden22:27 5 Aug '02  
GeneralRe: RC4 is not a good choice PinmemberCarl Berg2:15 6 Aug '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Jul 2002
Editor: Chris Maunder
Copyright 2002 by Shrishail Rana
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project