Click here to Skip to main content
12,510,006 members (54,305 online)
Click here to Skip to main content
Add your own
alternative version

Stats

205.3K views
8.1K downloads
42 bookmarked
Posted

RC6 encryption and decryption

, 14 Jul 2002
Rate this:
Please Sign up or sign in to vote.
RC6 is an evolutionary improvement of RC5, designed to meet the requirements of the Advanced Encryption Standard (AES).

Sample Image -<br mode=screenshot.jpg" height="247" width="350">

Introduction

RC6 is an evolutionary improvement of RC5, designed to meet the requirements of the Advanced Encryption Standard (AES). Like RC5, RC6 makes essential use of data-dependent rotations. New features of RC6 include the use of four working registers instead of two, and the inclusion of integer multiplication as an additional primitive operation. The use of multiplication greatly increases the diffusion achieved per round, allowing for greater security, fewer rounds, and increased throughput. I found an article about it online and fulfilled the algorithm using C++ for fun. Hope it'd be helpful to some interested people.

Details of RC6

Like RC5, RC6 is a fully parameterized family of encryption algorithms. A version of RC6 is more accurately specified as RC6-w/r/b where the word size is w bits, encryption consists of a nonnegative number of rounds r, and b denotes the length of the encryption key in bytes. Since the AES submission is targeted at w = 32 and r = 20, we shall use RC6 as shorthand to refer to such versions. When any other value of w or r is intended in the text, the parameter values will be specified as RC6-w/r. Of particular relevance to the AES effort will be the versions of RC6 with 16-, 24-, and 32-byte keys. For all variants, RC6-w/r/b operates on units of four w-bit words using the following six basic operations. The base-two logarithm of w will be denoted by lgw.

  • a + b: integer addition modulo 2^w
  • a - b: integer subtraction modulo 2^w
  • a @ b: bitwise exclusive-or of w-bit words
  • a * b: integer multiplication modulo 2^w
  • a <<< b: rotate the w-bit word a to the left by the amount given by the least significant lgw bits of b
  • a >>> b: rotate the w-bit word a to the right by the amount given by the least significant lgw bits of b

Note that in the description of RC6 the term "round" is somewhat analogous to the usual DES-like idea of a round: half of the data is updated by the other half; and the two are then swapped. In RC5, the term "half-round" was used to describe this style of action, and an RC5 round was deemed to consist of two half-rounds. This seems to have become a potential cause of confusion, and so RC6 reverts to using the term "round" in the more established way.

To get the detailed algorithm description of RC6-w/r/b. Please read the article "The RC6 Block Cipher" by Ronald L. Rivest, M.J.B. Robshaw, R. Sidney and, Y.L. Yin.

Details of Code

In my program, I fulfilled RC6-32/16. Since the integer addition, subtraction and multiplication don't exceed 2^32 in my program, I don't let their results modulo 2^32 like the operations described above. Anyway, the encryption and decryption go well.

I wrapped the bits rotation operations in two functions DWORD CHexDoc::LeftRotate(DWORD dwVar, DWORD dwOffset) and DWORD CHexDoc::RightRotate(DWORD dwVar, DWORD dwOffset).

DWORD CHexDoc::LeftRotate(DWORD dwVar, DWORD dwOffset)
{
    DWORD temp1, temp2;

    temp1 = dwVar >> (W - dwOffset);
    temp2 = dwVar << dwOffset;
    temp2 = temp2 | temp1;

    return temp2;
}

DWORD CHexDoc::RightRotate(DWORD dwVar, DWORD dwOffset)
{
    DWORD temp1, temp2;

    temp1 = dwVar << (W - dwOffset);
    temp2 = dwVar >> dwOffset;
    temp2 = temp2 | temp1;

    return temp2;
}

The key generation part is like

void CHexDoc::KeyGen(DWORD dwKey)
{
    DWORD P32 = 0xB7E15163;
    DWORD Q32 = 0x9E3779B9;
    DWORD i, A, B;
    DWORD dwByteOne, dwByteTwo, dwByteThree, dwByteFour;

    dwByteOne = dwKey >> 24;
    dwByteTwo = dwKey >> 8;
    dwByteTwo = dwByteTwo & 0x0010;
    dwByteThree = dwKey << 8;
    dwByteThree = dwByteThree & 0x0100;
    dwByteFour = dwKey << 24;

    dwKey = dwByteOne | dwByteTwo | dwByteThree
        | dwByteFour;

    m_dwS[0] = P32;

    for(i = 1; i < 2 * R + 4; i++)
        m_dwS[i] = m_dwS[i - 1] + Q32;

    i = A = B = 0;

    int v = 3 * max(1, 2 * R + 4);

    for(int s = 1; s <= v; s++)
    {
        A = m_dwS[i] = LeftRotate(m_dwS[i] + A + B, 
            OffsetAmount(3));
        B = dwKey = LeftRotate(dwKey + A + B, 
            OffsetAmount(A + B));

        i = (i + 1) % (2 * R + 4);
    }
}

Finally, the core parts of encryption and decryption are as following:

// encrypt the file
void CHexDoc::EncodeFile()
{
    DWORD* pdwTemp;

    for(UINT i = 0; i < m_nDocLength; i += 16)
    {
        pdwTemp = (DWORD*)&m_pFileData[i];

        pdwTemp[0] = (pdwTemp[0] - m_dwS[2 * R + 2]);
        pdwTemp[2] = (pdwTemp[2] - m_dwS[2 * R + 3]);

        for(int j = R; j >= 1; j--)
        {
            DWORD temp = pdwTemp[3];
            pdwTemp[3] = pdwTemp[2];
            pdwTemp[2] = pdwTemp[1];
            pdwTemp[1] = pdwTemp[0];
            pdwTemp[0] = temp;

            DWORD t = 
                LeftRotate((pdwTemp[1] * (2 * pdwTemp[1] + 1)),
                OffsetAmount((DWORD)(log((double)W)/log(2.0))));
            DWORD u = 
                LeftRotate((pdwTemp[3] * (2 * pdwTemp[3] + 1)),
                OffsetAmount((DWORD)(log((double)W)/log(2.0))));
            pdwTemp[0] =
                (RightRotate((pdwTemp[0] - m_dwS[2 * j]),
                OffsetAmount(u))) ^ t;
            pdwTemp[2] = 
                (RightRotate((pdwTemp[2] - m_dwS[2 * j + 1]),
                OffsetAmount(t))) ^ u;
        }

        pdwTemp[1] = (pdwTemp[1] - m_dwS[0]);
        pdwTemp[3] = (pdwTemp[3] - m_dwS[1]);
    }
    pdwTemp = NULL;
    SetModifiedFlag(TRUE);

    POSITION pos = GetFirstViewPosition();
    while(pos != NULL)
    {
        CView* pView = GetNextView(pos);
        pView->RedrawWindow();
    }
}

// decrypt the file
void CHexDoc::DecodeFile()
{
    DWORD* pdwTemp;

    for(UINT i = 0; i < m_nDocLength; i += 16)
    {
        pdwTemp = (DWORD*)&m_pFileData[i];

        pdwTemp[1] = (pdwTemp[1] + m_dwS[0]);
        pdwTemp[3] = (pdwTemp[3] + m_dwS[1]);

        for(int j = 1; j <= R; j++)
        {
            DWORD t = 
                LeftRotate((pdwTemp[1] * (2 * pdwTemp[1] + 1)),
                OffsetAmount((DWORD)(log((double)W)/log(2.0))));
            DWORD u = 
                LeftRotate((pdwTemp[3] * (2 * pdwTemp[3] + 1)),
                OffsetAmount((DWORD)(log((double)W)/log(2.0))));
            pdwTemp[0] = 
                (LeftRotate(pdwTemp[0] ^ t, OffsetAmount(u)) +
                m_dwS[2 * j]);
            pdwTemp[2] = 
                (LeftRotate(pdwTemp[2] ^ u, OffsetAmount(t)) +
                m_dwS[2 * j + 1]);

            DWORD temp = pdwTemp[0];
            pdwTemp[0] = pdwTemp[1];
            pdwTemp[1] = pdwTemp[2];
            pdwTemp[2] = pdwTemp[3];
            pdwTemp[3] = temp;
        }

        pdwTemp[0] = (pdwTemp[0] + m_dwS[2 * R + 2]);
        pdwTemp[2] = (pdwTemp[2] + m_dwS[2 * R + 3]);
    }
    pdwTemp = NULL;
    SetModifiedFlag(TRUE);

    POSITION pos = GetFirstViewPosition();
    while(pos != NULL)
    {
        CView* pView = GetNextView(pos);
        pView->RedrawWindow();
    }
}

Conclusion

In the view window, I showed the Hex and Char content of the loaded file and their addresses. You can see the changes every time you encrypt/decrypt it. Thanks!

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

Share

About the Author

Mingming Lu
Web Developer
United States United States
Hey you,
Out there in the cold,
Getting lonely, getting old,
Can you feel me?
Hey you,
Standing in the aisle,
With itchy feet and fading smile,
Can you feel me?
Hey you,
Don't help them to bury the light.
Don't give in without a fight.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionOpen source license type Pin
Song Huang17-Dec-15 18:58
memberSong Huang17-Dec-15 18:58 
Generalrc6 in jar Pin
endra hermawan27-May-09 22:55
memberendra hermawan27-May-09 22:55 
GeneralThe bug with overrun Pin
mircea13131325-Apr-09 1:24
membermircea13131325-Apr-09 1:24 
Questionis there a rc6 algorithm in vb.net Pin
moonshaddow30-Oct-08 15:35
membermoonshaddow30-Oct-08 15:35 
AnswerRe: is there a rc6 algorithm in vb.net Pin
didit adja5-Aug-15 5:23
memberdidit adja5-Aug-15 5:23 
GeneralHep block error... Pin
Roel Virgel L. Baldon6-Oct-08 23:28
memberRoel Virgel L. Baldon6-Oct-08 23:28 
GeneralI have a errors, help me [modified] Pin
Srgteam22-Nov-07 14:58
memberSrgteam22-Nov-07 14:58 
GeneralPls Help~~; Pin
ofuka14-Mar-06 22:07
memberofuka14-Mar-06 22:07 
GeneralDES cryptanalysis with 6 rounds Pin
nasor6-May-05 23:14
membernasor6-May-05 23:14 
GeneralRC3, RC4, RC5, RC6 Pin
Mansureh Shahraki3-Feb-05 6:00
sussMansureh Shahraki3-Feb-05 6:00 
GeneralRe: RC3, RC4, RC5, RC6 Pin
Mingming Lu3-Feb-05 6:14
memberMingming Lu3-Feb-05 6:14 
QuestionPlease help me ? Pin
lonelywind198218-Mar-04 21:47
memberlonelywind198218-Mar-04 21:47 
QuestionHow to setup RC6 with C# ? Pin
lonelywind198214-Mar-04 0:12
memberlonelywind198214-Mar-04 0:12 
GeneralThis program is not run in all Windows OS Pin
Brahma12-Dec-03 1:33
memberBrahma12-Dec-03 1:33 
Questionhelp me?? Pin
xxhimanshu4-Nov-03 23:16
memberxxhimanshu4-Nov-03 23:16 
Generalweak keys detection Pin
krishni13-Aug-03 4:22
memberkrishni13-Aug-03 4:22 
GeneralKey Length Pin
brad_brady13-Apr-03 10:43
memberbrad_brady13-Apr-03 10:43 
Generalkey gen Pin
abritto24-Mar-03 5:48
memberabritto24-Mar-03 5:48 
Generalopening a file Pin
abritto20-Mar-03 6:42
memberabritto20-Mar-03 6:42 
GeneralRe: opening a file Pin
Mingming Lu20-Mar-03 12:07
memberMingming Lu20-Mar-03 12:07 
Generalvsc++6.0 Pin
abritto26-Feb-03 3:16
memberabritto26-Feb-03 3:16 
GeneralRe: vsc++6.0 Pin
Mingming Lu26-Feb-03 5:53
memberMingming Lu26-Feb-03 5:53 
Questioncan this be changed easily for RC5 Pin
abritto25-Feb-03 3:43
memberabritto25-Feb-03 3:43 
AnswerRe: can this be changed easily for RC5 Pin
Mingming Lu25-Feb-03 4:39
memberMingming Lu25-Feb-03 4:39 
GeneralOverrun Pin
BossShot23-Oct-02 2:18
sussBossShot23-Oct-02 2:18 
GeneralTHE BUGG!!!!! Pin
SUPER_ZORRO2-Dec-02 2:25
memberSUPER_ZORRO2-Dec-02 2:25 
QuestionHyperSnap? Pin
Joel Holdsworth8-Jul-02 10:32
memberJoel Holdsworth8-Jul-02 10:32 
AnswerRe: HyperSnap? Pin
Mingming Lu8-Jul-02 11:16
memberMingming Lu8-Jul-02 11:16 
AnswerRe: HyperSnap? Pin
Marc Richarme8-Jul-02 11:22
memberMarc Richarme8-Jul-02 11:22 
GeneralI get all sorts of errors Pin
Dins_C8-Jul-02 6:23
memberDins_C8-Jul-02 6:23 
GeneralRe: I get all sorts of errors Pin
Mingming Lu8-Jul-02 8:32
memberMingming Lu8-Jul-02 8:32 
Generalfind chinese error Pin
Anonymous7-Jul-02 15:27
memberAnonymous7-Jul-02 15:27 
GeneralRe: find chinese error Pin
Mingming Lu7-Jul-02 15:38
memberMingming Lu7-Jul-02 15:38 
Generalhave chinese error Pin
Anonymous7-Jul-02 15:27
memberAnonymous7-Jul-02 15:27 
GeneralLet me get this straight. Pin
WREY7-Jul-02 12:15
memberWREY7-Jul-02 12:15 
GeneralRe: Let me get this straight. Pin
Mingming Lu7-Jul-02 14:51
memberMingming Lu7-Jul-02 14:51 
GeneralRe: Let me get this straight. Pin
WREY8-Jul-02 21:29
memberWREY8-Jul-02 21:29 
GeneralRe: Let me get this straight. Pin
Mingming Lu9-Jul-02 5:51
memberMingming Lu9-Jul-02 5:51 
GeneralRe: Let me get this straight. Pin
WREY22-Jul-02 0:32
memberWREY22-Jul-02 0:32 
GeneralOffsetAmount and the use of log Pin
Matthias Mann7-Jul-02 11:39
memberMatthias Mann7-Jul-02 11:39 
GeneralRe: OffsetAmount and the use of log Pin
Mingming Lu7-Jul-02 15:04
memberMingming Lu7-Jul-02 15:04 
GeneralRe: OffsetAmount and the use of log Pin
BossShot23-Oct-02 1:53
sussBossShot23-Oct-02 1:53 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 15 Jul 2002
Article Copyright 2002 by Mingming Lu
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid