# help with rc5 algorithm implementation

• 03-25-2010, 07:11 AM
raghav
help with rc5 algorithm implementation
can any one help me with this code...

import javax.swing.*;
import java.util.io.*;
public class Rc5
{
private int[] s;
private int[] l;
private int b, u, t, c;
private byte[] key;
private int rounds;
public Rc5()
{
String str = "tallwalkers";
key = GetKeyFromString(str);
rounds = 16;
b = (int)key.length;
u = 4;
t = (int)(34);
c = 12 / u;
s = new int[34];
l = new int[12];
GenerateKey(key, rounds);
}

{
rounds = round;
b = (int)key.length;
u = 4;
t = (int)(2 * rounds + 2);
c = Math.max(b, 1) / u;
s = new int[2 * rounds + 2];
l = new int[key.length];
GenerateKey(key, rounds);
}

{
rounds = round;
u = 4;
t = (int)(2 * rounds + 2);
c = Math.max(b, 1) / u;
s = new int[2 * rounds + 2];
GenerateKey(key, rounds);
}

private int leftRotate(int x, int offset)
{
int t1, t2;
t1 = x >> (32 - offset);
t2 = x << offset;
return t1 | t2;
}
private int RightRotate(int x, int offset)
{
int t1, t2;
t1 = x << (32 - offset);
t2 = x >> offset;
return t1 | t2;
}
private void Encode(RefObject<Integer> r1, RefObject<Integer> r2, int rounds)
{
r1.argvalue = r1.argvalue + s[0];
r2.argvalue = r2.argvalue + s[1];
for (int i = 1; i <= rounds; i++)
{
r1.argvalue = leftRotate(r1.argvalue ^ r2.argvalue, (int)r2.argvalue) + s[2 * i];
r2.argvalue = leftRotate(r2.argvalue ^ r1.argvalue, (int)r1.argvalue) + s[2 * i + 1];
}
}
private void Decode(RefObject<Integer> r1, RefObject<Integer> r2, int rounds)
{
for (int i = rounds; i >= 1; i--)
{
r2.argvalue = (RightRotate(r2.argvalue - s[2 * i + 1], (int)r1.argvalue)) ^ r1.argvalue;
r1.argvalue = (RightRotate(r1.argvalue - s[2 * i], (int)r2.argvalue)) ^ r2.argvalue;
}
r2.argvalue = r2.argvalue - s[1];
r1.argvalue = r1.argvalue - s[0];
}

private void GenerateKey(byte[] key, int rounds)
{
int P32 = Integer.parseInt("b7e15163", System.Globalization.NumberStyles.HexNumber);
int Q32 = Integer.parseInt("9e3779b9", System.Globalization.NumberStyles.HexNumber);
for (int i = key.length - 1; i >= 0; i--)
{
l[i] = leftRotate((int)i, 8) + key[i];
}
s[0] = P32;
for (int i = 1; i <= t - 1; i++)
{
s[i] = s[i - 1] + Q32;
}
int ii, jj;
ii = jj = 0;
int x, y;
x = y = 0;
int v = 3 * Math.max(t, c);
for (int counter = 0; counter <= v; counter++)
{
x = s[ii] = leftRotate((s[ii] + x + y), 3);
y = l[jj] = leftRotate((l[jj] + x + y), (int)(x + y));
ii = (ii + 1) % t;
jj = (jj + 1) % c;
}
}
private byte[] GetKeyFromString(String str)
{
char[] mykeyinchar = str.toCharArray();
byte[] mykeyinbytes = new byte[mykeyinchar.length];
for (int i = 0; i < mykeyinchar.length; i++)
{
mykeyinbytes[i] = (byte)mykeyinchar[i];
}
return mykeyinbytes;
}

public final void Encrypt(FileStream streamreader, FileStream streamwriter)
{
int r1, r2;
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(streamwriter);
while (filelength > 0)
{
try
{
try
{
}
catch (java.lang.Exception e)
{
r2 = 0;
}
}
catch (java.lang.Exception e2)
{
r1 = r2 = 0;
}

RefObject<Integer> tempRefObject = new RefObject<Integer>(r1);
RefObject<Integer> tempRefObject2 = new RefObject<Integer>(r2);
Encode(tempRefObject, tempRefObject2, rounds);
r1 = tempRefObject.argvalue;
r2 = tempRefObject2.argvalue;
bw.Write(r1);
bw.Write(r2);
filelength -= 8;
}
streamwriter.close();
}
public final void Decrypt(FileStream streamreader, FileStream streamwriter)
{
int r1, r2;
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(streamwriter);
while (filelength > 0)
{
try
{
RefObject<Integer> tempRefObject = new RefObject<Integer>(r1);
RefObject<Integer> tempRefObject2 = new RefObject<Integer>(r2);
Decode(tempRefObject, tempRefObject2, rounds);
r1 = tempRefObject.argvalue;
r2 = tempRefObject2.argvalue;
if (!(r1 == 0 && r2 == 0 && (filelength - 8 <= 0)))
{
bw.Write(r1);
bw.Write(r2);
}
if (r2 == 0 && (filelength - 8 <= 0))
{
bw.Write(r1);
}
filelength -= 8;
}
catch (java.lang.Exception e)
{
JOptionPane.showConfirmDialog(null, "May be U try to decrypt an normal file (plain file)", "Error",

JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
return;
}
}