Results 1 to 1 of 1
  1. #1
    raghav is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default 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);
    }

    public Rc5(String password, int round)
    {
    key = GetKeyFromString(password);
    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);
    }

    public Rc5(byte[] password, int round)
    {
    rounds = round;
    key = password;
    b = (int)password.length;
    u = 4;
    t = (int)(2 * rounds + 2);
    c = Math.max(b, 1) / u;
    s = new int[2 * rounds + 2];
    l = new int[password.length];
    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.BinaryReader br = new System.IO.BinaryReader(streamreader);
    System.IO.BinaryWriter bw = new System.IO.BinaryWriter(streamwriter);
    long filelength = streamreader.getLength();
    while (filelength > 0)
    {
    try
    {
    r1 = br.ReadUInt32();
    try
    {
    r2 = br.ReadUInt32();
    }
    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;
    }
    streamreader.close();
    streamwriter.close();
    }
    public final void Decrypt(FileStream streamreader, FileStream streamwriter)
    {
    int r1, r2;
    System.IO.BinaryReader br = new System.IO.BinaryReader(streamreader);
    System.IO.BinaryWriter bw = new System.IO.BinaryWriter(streamwriter);
    long filelength = streamreader.getLength();
    while (filelength > 0)
    {
    try
    {
    r1 = br.ReadUInt32();
    r2 = br.ReadUInt32();
    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;
    }
    }
    streamreader.close();
    streamwriter.close();
    }

    }
    Last edited by raghav; 04-04-2010 at 06:37 AM.

Similar Threads

  1. Web service Implementation
    By kirtichopra2003 in forum Advanced Java
    Replies: 0
    Last Post: 10-14-2009, 10:13 AM
  2. Stack implementation
    By jefrazie in forum New To Java
    Replies: 3
    Last Post: 10-07-2009, 01:59 PM
  3. Soundex Algorithm Implementation in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:40 PM
  4. Graph DPS and BFS implementation
    By hey in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 10:19 PM
  5. Help with recursive implementation
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:57 AM

Posting Permissions

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