Port C# of RC4 implementation to Java - #CLOSED#

  • Status: Closed
  • Præmier: $100
  • Modtagne indlæg: 6
  • Vinder: SMikhail

Konkurrence Instruktioner

I've have received a working version, so no more entries please.

I'm going to do further tests then I'll end the contest. Thanks J

----------------------------------------------------


Port the C# below into Java

The C# code below as described at; http://tofuculture.com/Blog/post/RC4-Encryption-in-C.aspx
I need a Java version of it so I can encrypt and decrypt text with a given password.

I'll use the form at http://twinsquare.com/apps/RC4/Default.aspx to test it with a few different text & Password values

I need this ASAP so I'll choose the first working answer submitted.

Thanks


-----------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace TestRC4
{
public class RC4
{
private const int N = 256;
private int[] sbox;
private string password;
private string text;

public RC4(string password, string text)
{
this.password = password;
this.text = text;
}

public RC4(string password)
{
this.password = password;
}

public string Text
{
get { return text; }
set { text = value; }
}

public string Password
{
get { return password; }
set { password = value; }
}

public string EnDeCrypt()
{
RC4Initialize();

int i = 0, j = 0, k = 0;
StringBuilder cipher = new StringBuilder();
for (int a = 0; a < text.Length; a++)
{
i = (i + 1) % N;
j = (j + sbox[i]) % N;
int tempSwap = sbox[i];
sbox[i] = sbox[j];
sbox[j] = tempSwap;

k = sbox[(sbox[i] + sbox[j]) % N];
int cipherBy = ((int)text[a]) ^ k; //xor operation
cipher.Append(Convert.ToChar(cipherBy));
}
return cipher.ToString();
}

public static string StrToHexStr(string str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
int v = Convert.ToInt32(str[i]);
sb.Append(string.Format("{0:X2}", v));
}
return sb.ToString();
}

public static string HexStrToStr(string hexStr)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hexStr.Length; i += 2)
{
int n = Convert.ToInt32(hexStr.Substring(i, 2), 16);
sb.Append(Convert.ToChar(n));
}
return sb.ToString();
}

private void RC4Initialize()
{
sbox = new int[N];
int[] key = new int[N];
int n = password.Length;
for (int a = 0; a < N; a++)
{
key[a] = (int)password[a % n];
sbox[a] = a;
}

int b = 0;
for (int a = 0; a < N; a++)
{
b = (b + sbox[a] + key[a]) % N;
int tempSwap = sbox[a];
sbox[a] = sbox[b];
sbox[b] = tempSwap;
}
}
}
}

Anbefalede færdigheder

Arbejdsgiverfeedback

“Worked perfectly.”

Profilbillede jxa, United Kingdom.

Offentlig Præciserings Opslagstavle

  • jxa
    Konkurrenceafholder
    • 10 år siden

    Thanks for all those that submitted entries but I had to choose the first working entry as I explained I would in the brief.

    • 10 år siden
  • jxa
    Konkurrenceafholder
    • 10 år siden

    I've have received a working version, so no more entries please.
    I'm going to do further tests then I'll end the contest. Thanks J

    • 10 år siden
  • JSLProgrammer
    JSLProgrammer
    • 10 år siden

    Sorry, I don't think it works quite right...

    • 10 år siden
  • JSLProgrammer
    JSLProgrammer
    • 10 år siden

    I have the Java translation for the C# code you provided. If that is all that you wanted I can send it to you. How do you want me to send it to you?

    • 10 år siden
  • JSLProgrammer
    JSLProgrammer
    • 10 år siden

    Do you want the Java program to send data to the webpage or do you just want the functions converted to Java?

    • 10 år siden

Vis flere kommentarer

Sådan kommer du i gang med konkurrencer

  • Opret din konkurrence

    Opret din konkurrence Hurtigt og nemt

  • Få tonsvis af indlæg

    Få tonsvis af indlæg Fra hele verden

  • Tildel det bedste indlæg

    Tildel det bedste indlæg Download filerne - Nemt!

Opret en Konkurrence Nu eller slut dig til os i dag!