Results 1 to 3 of 3

Thread: RC4 Encryption

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2003
    Posts
    51

    RC4 Encryption

    /////////////////////////////////////////////////////////
    RC4.cpp
    ////////////////////////////////////////////////////////
    // Rc4.cpp: implementation of the CRc4 class.
    // Arranged by: SonicMouse
    // web: http://www.mouseindustries.com
    //////////////////////////////////////////////////////////////////////

    #include "Rc4.h"

    #ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif

    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    CRc4::CRc4()
    {

    }

    CRc4::~CRc4()
    {

    }

    //////////////////////////////////////////////////////////////////////
    // inits a new rc4 session
    //////////////////////////////////////////////////////////////////////
    void CRc4::rc4_init(const char* key, int keylen){
    int keypos = 0, x;
    unsigned char K[256];
    for(x = 0; x < 256; ++x){
    S[x] = x;
    K[x] = key[keypos++];
    if(keypos >= keylen) keypos=0;
    }
    for(j = x = 0; x < 256; ++x){
    j += S[x] + K[x];
    t = S[x];
    S[x] = S[j];
    S[j] = t;
    }
    i = j = 0;
    }

    //////////////////////////////////////////////////////////////////////
    // encrypts a char array
    //////////////////////////////////////////////////////////////////////
    unsigned char* CRc4::rc4_encrypt_array(unsigned char* arr, int nLen){
    for(int q = 0; q < nLen; q++)
    arr[q] = arr[q] ^ next_rand();
    return &arr[0];
    }

    //////////////////////////////////////////////////////////////////////
    // encryption routine
    //////////////////////////////////////////////////////////////////////
    inline unsigned char CRc4::next_rand(){
    ++i;
    j += S[i];
    t = S[i];
    S[i] = S[j];
    S[j] = t;
    t = S[i] + S[j];
    return S[t];
    }

    //////////////////////////////////////////////////////////////////////
    // Utilitys to take a byte array and change it to uppercase hex
    //////////////////////////////////////////////////////////////////////
    char* CRc4::UCharArrayToHexString(const unsigned char* arr, int nLen){
    char* hex = new char[nLen*2+1];
    char tmp[3];
    int i;
    for(i = 0; i < nLen; i++){
    sprintf(tmp, "%02X", (unsigned int)arr[i]);
    hex[i*2]=tmp[0];
    hex[i*2+1]=tmp[1];
    }
    hex[i*2]=0;
    return &hex[0];
    }








    ///////////////////////////////////////////////////////
    rc4.h
    //////////////////////////////////////////////////////
    // Rc4.h: interface for the CRc4 class.
    // Arranged by: SonicMouse
    // web: http://www.mouseindustries.com/
    //////////////////////////////////////////////////////////////////////

    #ifndef __RC4_H__
    #define __RC4_H__

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    class CRc4
    {
    public:
    CRc4();
    virtual ~CRc4();

    void rc4_init(const char *key, int keylen);
    unsigned char* rc4_encrypt_array(unsigned char* arr, int nLen);

    // utility
    char* UCharArrayToHexString(const unsigned char* arr, int nLen);

    private:
    inline unsigned char next_rand();

    private:
    unsigned char S[256];
    unsigned char i;
    unsigned char j;
    unsigned char t;

    };

    #endif /* __RC4_H__ */







    ANY one ne idea how i can translate that to VB?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Umm...

    Instruction by instruction I'd recommend

    The only difficult thing is the single bitwise XOR in there.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Just a few hints:
    unsigned char <=> Byte
    char * <=> String
    int <=> Long

    [] is for array indexing, array indices start at 0.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width