RC4 encryption algo in C#


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: RC4 encryption algo in C#

  1. #1
    Sachin Guest

    RC4 encryption algo in C#

    hi all
    after breaking my head for nearly one day..i have given up in tryin to
    implement RC4 cipher in C#..What the code does it after encrypting the
    string it stores the string in a DB(a nvarchar col) & then when required
    retrieves back the encrypted key & decrypts it by usng the same method.For
    some cases the the two strings are not matching.any help will be appreciated
    Here is the code & for the VB 6.0 gurus the same code in VB follows(ok
    something must be different thats why in VB the strings are matching but are
    not matchin in C# :-). )
    ....-TIA sachin
    C#
    public string Crypt(string inp)

    {


    int[] sbox = new int[256];

    int[] sbox2 = new int[256];

    int i, modLen, j;

    int t,k,temp;

    string output="";


    //inp="abcd";

    string key = "1234567";

    int len = key.Length;

    i=0;

    for(i=0;i<=255;i++)

    {

    sbox[i] = Convert.ToByte(i);

    modLen = i % len ;

    sbox2[i] =(int)Convert.ToChar(key.Substring(modLen,1));

    //Console.WriteLine(i.ToString() + " " + key.Substring(modLen,1) + " " +
    sbox[2].ToString());

    // key(a) = Asc(Mid(strPwd, (a Mod intLength) + 1, 1))

    }

    //Console.WriteLine("CALLED " + i.ToString());

    //return null;

    j=0;

    for(i=0;i<=255;i++)

    {

    j = (j + sbox[i] + sbox2[i]) % 256;


    temp = sbox[i];

    sbox[i] = sbox[j];

    sbox[j] =temp;


    //Console.WriteLine("J--" + j + "boxi--" + sbox[i] + "boxJ--" + sbox[j]);

    // Console.WriteLine("boxi--" + sbox[i]);

    // Console.WriteLine("boxJ--" + sbox[j]);



    }

    //return null;

    i=0;j=0;



    for(int x=1;x<=inp.Length;x++)

    {

    i = (i+1) % 256;

    j = (j + sbox[i]) % 256;

    temp = sbox[i];

    sbox[i] = sbox[j];

    sbox[j] = temp;

    t = (sbox[i] + sbox[j]) % 256;

    k = sbox[t];

    int v = (int)((int)(Convert.ToChar(inp.Substring(x-1,1))) ^ (int)k);

    Console.WriteLine("k.." + k + "charby..." + v + " v " + Convert.ToChar(v));

    output = output + Convert.ToChar(v);



    }



    return output;

    }

    }



    ......VB code (taken from aspalliance web site).....


    Dim cipher As String
    Private psw As String
    Private sbox(255)
    Private key(255)



    'Private Sub Class_Initialize()
    '
    'End Sub

    Private Sub RC4Initialize(strPwd)
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    '::: This routine called by EnDeCrypt function. Initializes the :::
    '::: sbox and the key array) :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    Dim tempSwap
    Dim a
    Dim b

    intLength = Len(strPwd)
    'App.LogEvent "in first for loop..."
    For a = 0 To 255
    key(a) = Asc(Mid(strPwd, (a Mod intLength) + 1, 1))

    ' App.LogEvent "a = " & a
    ' App.LogEvent "key(a) = " & key(a)
    ' App.LogEvent "mid mod intLength = " & Mid(strPwd, (a Mod
    intLength) + 1, 1)
    ' Debug.Print "a = " & a
    ' Debug.Print "key(a) = " & key(a)
    ' Debug.Print "mid mod intLength = " & Mid(strPwd, (a Mod intLength)
    + 1, 1)
    sbox(a) = a
    Next

    'App.LogEvent "outside first for loop..."

    b = 0
    For a = 0 To 255

    b = (b + sbox(a) + key(a)) Mod 256
    tempSwap = sbox(a)
    sbox(a) = sbox(b)
    sbox(b) = tempSwap
    ' App.LogEvent "b = " & b
    ' App.LogEvent "BOX1.." & sbox(a)
    ' App.LogEvent "box2..." & sbox(b)
    Next

    End Sub

    Public Function rc4(plaintxt)
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    '::: This routine does all the work. Call it both to ENcrypt :::
    '::: and to DEcrypt your data. :::
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    Dim temp
    Dim a
    Dim i
    Dim j
    Dim k
    Dim cipherby
    ' Dim cipher

    i = 0
    j = 0

    RC4Initialize psw

    For a = 1 To Len(plaintxt)
    i = (i + 1) Mod 256

    j = (j + sbox(i)) Mod 256
    temp = sbox(i)
    sbox(i) = sbox(j)
    sbox(j) = temp

    k = sbox((sbox(i) + sbox(j)) Mod 256)

    cipherby = Asc(Mid(plaintxt, a, 1)) Xor k

    App.LogEvent "k..." & k
    App.LogEvent "cipherby.." & cipherby
    App.LogEvent Chr(cipherby)
    cipher = cipher & Chr(cipherby)
    Next

    End Function







  2. #2
    Jason Guest

    Re: RC4 encryption algo in C#


    "Sachin" <sachin.nigam@orbit-e.com> wrote:
    >hi all
    >after breaking my head for nearly one day..i have given up in tryin to
    >implement RC4 cipher in C#..What the code does it after encrypting the
    >string it stores the string in a DB(a nvarchar col) & then when required
    >retrieves back the encrypted key & decrypts it by usng the same method.For
    >some cases the the two strings are not matching.any help will be appreciated
    >Here is the code & for the VB 6.0 gurus the same code in VB follows(ok
    >something must be different thats why in VB the strings are matching but

    are
    >not matchin in C# :-). )
    >....-TIA sachin


    Run the algorithm on the same data using the same key in both C# and VB6.
    Put a break point for each at the beginning of the encryption function.
    Step through each, line by line, and look at the data being manipulated
    before and after. When you find the difference, you will know where the
    problem lies.

    You can do this yourself. You don't need others to inspect your code on
    this one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center