Encrypt method (from decrypt method)

This is a discussion on Encrypt method (from decrypt method) within the C# Programming forums, part of the General Programming Boards category; Hi, In the code below there is a method called " decrypt ". I am trying to reverse the code ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Encrypt method (from decrypt method)

    Hi,

    In the code below there is a method called "decrypt". I am trying to reverse the code to implement the corresponding "encrypt":


    Code:
    public partial class Form1 : Form
    {
    private uint P;
    private uint Q;
    private uint[] _S;
    char[] k = new char[0x80];
    
    
            public byte Crr
            {
                get
                {
                    return 12;
                }
            }
            public byte Crb
            {
                get
                {
                    return 16;
                }
            }
            public byte Crt
            {
                get
                {
                    return 26;
                }
            }
            public byte Crc
            {
                get
                {
                    return 4;
                }
            }
    
    //Constructor
    public Form1()
    {
                this.P = 0xb7e15163;
                this.Q = 0x9e3779b9;
                this._S = new uint[this.Crt];
                k[0] = '\x0017';
                k[1] = '\x009b';
                k[2] = '\x0084';
                k[3] = '\x0096';
                k[4] = 'V';
                k[5] = '\x00f0';
                k[6] = '\x0011';
                k[7] = '\x00d1';
                k[8] = '\x0088';
                k[9] = '\x0082';
                k[10] = '\x00bc';
                k[11] = '\x00e4';
                k[12] = '\x0089';
                k[13] = 'C';
                k[14] = '\x00da';
                k[15] = '\x0091';
    
                setup_S(k)
    }
    
            private void setup_S(char[] K)
            {
                int index = 0;
                int num2 = 0;
                int num3 = 0;
                int num4 = licBase.Crw / 8;
                uint[] numArray = new uint[this.Crc];
                uint num5 = 0;
                uint num6 = 0;
                for (int i = 0; i < numArray.Length; i++)
                {
                    numArray[i] = 0;
                }
                index = this.Crb - 1;
                numArray[this.Crc - 1] = 0;
                while (index != -1)
                {
                    numArray[index / num4] = (numArray[index / num4] << 8) + K[index];
                    index--;
                }
                this._S[0] = this.P;
                for (index = 1; index < this.Crt; index++)
                {
                    this._S[index] = this._S[index - 1] + this.Q;
                }
                num5 = num6 = 0;
                index = num2 = num3 = 0;
                while (num3 < (3 * this.Crt))
                {
                    num5 = this._S[index] = rotLeft(this._S[index] + (num5 + num6), 3);
                    num6 = numArray[num2] = rotLeft(numArray[num2] + (num5 + num6), num5 + num6);
                    num3++;
                    index = (index + 1) % this.Crt;
                    num2 = (num2 + 1) % this.Crc;
                }
            }
    
            public uint rotRight(uint x, uint y)
            {
                 uint num = x;
                 uint num2 = x;
                 for (uint i = 0; i < (y & (this.Crw - 1)); i++)
                 {
                     num = num >> 1;
                 }
                 for (uint j = 0; j < (this.Crw - (y & (this.Crw - 1))); j++)
                 {
                         num2 = num2 << 1;
                 }
                 return (num | num2);
            }
    
            public uint rotLeft(uint x, uint y)
            {
                uint num = x;
                uint num2 = x;
                for (uint i = 0; i < (y & (this.Crw - 1)); i++)
                {
                    num = num << 1;
                }
                for (uint j = 0; j < (this.Crw - (y & (this.Crw - 1))); j++)
                {
                    num2 = num2 >> 1;
                }
                return (num | num2);
            }
    
            public void decrypt(uint[] ct, uint[] pt)
            {
                uint y = ct[1];
                uint num2 = ct[0];
                for (uint i = this.Crr; i > 0; i--)
                {
                    y = rotRight(y - this._S[(int)((IntPtr)((2 * i) + 1))], num2) ^ num2;
                    num2 = rotRight(num2 - this._S[(int)((IntPtr)(2 * i))], y) ^ y;
                }
    
                pt[1] = y - this._S[1];
                pt[0] = num2 - this._S[0];
            }
    
            public void encrypt(uint[] ct, uint[] pt)
            {
                //  HELP NEEDED
            }
    
    
    }
    
    }
    I am having a hard time trying to reverse the XOR in the decrypt method. If anyone can help I would highly appreciate it.

    Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Is your question how to reverse XOR? How to get y? What?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    I have the "decrypt" moethod.
    I am trying to implement the method "encrypt" method that will work as below.

    Code:
    uint[] test = new uint[2]{1, 1};
    decrypt(test, test); //<-this stores in the test array some values {238972, 238972}
    // the values are not the actual ones; just for exemplification

    I suppose that if the "encrypt" method gets "238972" it should output "1".
    So the sequence when using both methods should be;


    Code:
    uint[] test = new uint[2]{1, 1};
    encrypt(test, test); //<- now the array is {238972, 238972}
    decrypt(test, test); //<- now the array is {1, 1}
    Thank you

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Are we sure that encrypt-->decrypt-->encrypt-->decrypt will give the initial value for all encryption methods?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to decrypt / encrypt using libgcrypt ? (ARC4)
    By jabka in forum C Programming
    Replies: 6
    Last Post: 04-21-2010, 11:34 PM
  2. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 06:50 PM
  3. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  4. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  5. Replies: 2
    Last Post: 01-22-2008, 03:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21