0

Im currently working with an online API that sends me some users with a encrypted PIN that is encrypted with RC4. its sent in Hex(8) and ive managed to decrypt the hex into the int that it originally was, but i cant for the life of me reverse it to send an encrypted pin(is 4 digits long, pre enc) back. im not the sharpest tool in the shed when it comes to crypt especially not when its between data types so im hoping someone is abit more experianced.

My Classes for RC4: Decrypt Function:

        public string DecryptUserPin(Users user)
    {
        try
        {
            byte[] out_ = new byte[8];
            crypto crypto = new crypto((long)user.UID);
            byte[] bytes = BitConverter.GetBytes(int.Parse(user.PIN, NumberStyles.HexNumber));
            crypto.crypto_rc4(ref bytes, ref out_, (long)bytes.Length);
            return BitConverter.ToInt64(out_, 0).ToString();
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR" + ex.ToString());
            return (string)null;
        }
    }

Crypto class:

   public class crypto
{
    private string key = "Secret words ";
    public rc4_key_t ctx;

    public crypto(long userId)
    {
        this.key += userId.ToString();
        byte length = (byte)this.key.Length;
        byte[] numArray = new byte[256];
        for (short index = 0; index < (short)256; ++index)
            numArray[(int)index] = (byte)index;
        this.ctx.x = (byte)0;
        this.ctx.y = (byte)0;
        byte index1 = 0;
        byte index2 = 0;
        for (short index3 = 0; index3 < (short)256; ++index3)
        {
            index2 = (byte)((int)this.key[(int)index1] + (int)numArray[(int)index3] + (int)index2 & (int)byte.MaxValue);
            byte num = numArray[(int)index3];
            numArray[(int)index3] = numArray[(int)index2];
            numArray[(int)index2] = num;
            index1 = (byte)(((uint)index1 + 1U) % (uint)length);
        }
        this.ctx.state = numArray;
    }

    public void crypto_rc4(ref byte[] in_, ref byte[] out_, long len)
    {


        byte index1 = this.ctx.x;
        byte index2 = this.ctx.y;
        byte[] numArray = new byte[8];
        byte[] state = this.ctx.state;
        for (short index3 = 0; (long)index3 < len; ++index3)
        {
            index1 = (byte)((int)index1 + 1 & (int)byte.MaxValue);
            index2 = (byte)((int)state[(int)index1] + (int)index2 & (int)byte.MaxValue);
            byte num1 = state[(int)index1];
            state[(int)index1] = state[(int)index2];
            state[(int)index2] = num1;
            byte index4 = (byte)((int)state[(int)index1] + (int)state[(int)index2] & (int)byte.MaxValue);
            byte num2 = (byte)((uint)in_[(int)index3] ^ (uint)state[(int)index4]);
            numArray[(int)index3] = num2;
        }
        this.ctx.x = index1;
        this.ctx.y = index2;
        out_ = numArray;
    }
}

Support Class:

    public struct rc4_key_t
{
    public byte[] state;
    public byte x;
    public byte y;
}
xguysOG
  • 51
  • 6

0 Answers0