public class TestRC4
{
public static void main(String[] args)
{
RC4 rc4 = new RC4("mypass", "mytext");
System.out.println(RC4.StrToHexStr(rc4.EnDeCrypt()));
rc4 = new RC4("mypass", RC4.HexStrToStr("AFB9DD0FD3A8"));
System.out.println(rc4.EnDeCrypt());
}
}
10 år siden
Konkurrenceafholder
10 år siden
Hi Mikhail, this looks good. It works with the text values I have I'm just going to get a few more values from a client to test with but it looks fine. The client is in the USA so they won't be awake for about 8 hours so I'll get back to you after that.
I'll post a note on the contest saying I have a working version.
Thanks
public static String HexStrToStr(String hexStr)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hexStr.length(); i += 2)
{
int n = Integer.valueOf(hexStr.substring(i, i + 2), 16);
char[] ch = { (char)n };
sb.append(new String(ch));
}
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] = password.charAt(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;
}
}
}
// part 2
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;
public static String StrToHexStr(String str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++)
{
int v = (int)str.charAt(i);
String s = Integer.toHexString(v).toUpperCase();
if (s.length() < 2)
sb.append("0");
sb.append(s);
}
return sb.toString();
}
Hi, I tried to upload code as an image (contest mode only supports images), but seems that the site re-encodes it (embedded data is lost). I'll try to post code here.