[C#] Substracting Hex String

Discussion in 'Mixed Languages' started by FreeStyler, Jan 5, 2012.

  1. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    #1 FreeStyler, Jan 5, 2012
    Last edited by a moderator: Apr 20, 2017
    For a client we have to replace an old iMail Server 8.x with a new email server, we like to keep all the current users and their current passwords intact so clients do not have to change setting after this server migration.

    IMail adds the value of the first character of the username with the value of the first character of the password. It then puts the sum of the two in hex into the registry.
    It then repeats this with the second letters of both the username and the password, and so on, and so on
    If the password is longer than the username, the username is repeated.

    Example:
    Code:
    
    username: test (the hex values of the username are: 74657374)
    password: BDD4EAE2EDD4E8
    
    Which results in:
    
        BD       D4    EA     E2      ED      D4     E8
        -74    -65    -73    -74    -74    -65    -73
    
    =   49      6F     77     6E      79      6F     75
    
    decrypted password = Iownyou
    Now I would like to create a (console) application where I can insert username and password parameters and the programs returns the decrypted password.

    Code:
    namespace ConsoleApplication2
    {
        class Program
        {
    
            public static void Main(string[] args)
            {
                string user = "test"; //username value, hardcoded for now
                string passHex = "BDD4EAE2EDD4E8"; //password hex string, hardcoded for now
                int passLength = (passHex.Length / 2); //password length
                string userHex = ConvertToHex(user);
    
                //todo 
                // adjust user length to be identical to passLength
                // subtract userHex from passHex & convert back to asciistring
                
                //debug
                Console.WriteLine(passHex);
                Console.WriteLine(userHex);
                Console.WriteLine(passLength.ToString());
                Console.ReadKey();
            }
    
            static string ConvertToHex(string asciiString)
            {
                string hex = "";
                foreach (char c in asciiString)
                {
                    int tmp = c;
                    hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
                }
                return hex;
            }
        }
    
    }
    
    This is what i got this far, but have no idea how to go from here, i am a newbie to C# and this hex stuff seems complicated...
    anyone can help me out here?

    TIA,
    [fs]
     
  2. woot332

    woot332 MDL Senior Member

    Feb 18, 2011
    390
    815
    10
    If no can help you with the C# code i could post a asm code example, if that would be useful.
     
  3. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,330
    1,377
    90
    #3 BobSheep, Jan 5, 2012
    Last edited by a moderator: Apr 20, 2017
    This works or me (VS 2008). Let me know how you get on.

    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                string pswd = RetrievePassword("test", "BDD4EAE2EDD4E8");
            }
    
    
            private string RetrievePassword(string userName, string encoded)
            {
                string user = userName;
    
                while (user.Length < encoded.Length / 2)
                    user += userName;
    
                Encoding enc = new ASCIIEncoding();
                byte[] bytesUser = enc.GetBytes(user);
    
                byte[] bytesEncoded = StringToByteArray(encoded);
    
                byte[] bytesPassword = new byte[encoded.Length];
    
                for(int i = 0; i < bytesEncoded.Length; i++)
                {
                    bytesPassword = (byte)((int)bytesEncoded - (int)bytesUser);
                }
    
                return enc.GetString(bytesPassword).Replace("\0", string.Empty);
            }
    
            public static byte[] StringToByteArray(String hex)
            {
                int NumberChars = hex.Length;
                byte[] bytes = new byte[NumberChars / 2];
                for (int i = 0; i < NumberChars; i += 2)
                    bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
                return bytes;
            }
     
  4. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    Seems to work perfectly, gonna try it out on a few passwords to make sure it works for longer passwords as well