1

I am looking for a way to create string in format of xxxx-xxxx-xxxx-xxxx-xxxx from string and if I have that string I can extract original string from that string in C#. I have search on google I found some solution which convert string to the required format but they don't get back the original string from the output. Is there any way to achieve it.

I found below sample of MD5 which converts string in my desired format but I can't convert that back to orignal string.

private static string GetHash(string s)
{
    MD5 sec = new MD5CryptoServiceProvider();
    ASCIIEncoding enc = new ASCIIEncoding();
    byte[] bt = enc.GetBytes(s);

    return GetHexString(sec.ComputeHash(bt));
}

private static string GetHexString(byte[] bt)
{
    int tmp = (int)'A';
    string s = string.Empty;
    for (int i = 0; i < bt.Length; i++)
    {
        byte b = bt[i];
        int n, n1, n2;
        n = (int)b;
        n1 = n & 15;
        n2 = (n >> 4) & 15;
        if (n2 > 9)
        {
            tmp = 0;
            tmp = (n2 - 10 + (int)'A');
            s += ((char)(n2 - 10 + (int)'A')).ToString();
        }
        else
            s += n2.ToString();
        if (n1 > 9)
        {
            tmp = 0;
            tmp = (n1 - 10 + (int)'A');
            s += ((char)(n1 - 10 + (int)'A')).ToString();
        }
        else
            s += n1.ToString();
        if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
    }
    return s;
}
Bassie
  • 9,529
  • 8
  • 68
  • 159
Aqeel
  • 689
  • 5
  • 20
  • 42
  • 1
    MD5 and other *hashing* algorithms are *not reversible*, they cannot do what you ask. You need an encryption algorithm such as AES and a plaint text encoded & formatted representation of its output. – Alex K. Jul 28 '16 at 10:58
  • ah, right. I have encryption implemented and I can decode the ecrypted string. But here I need it in following format xxxx-xxxx-xxxx-xxxx-xxxx. That should look like activation code and then I can decode to the original string. – Aqeel Jul 28 '16 at 11:02
  • Take the byte buffer output of the string encryption, encode it (hex|base64), format the string, receive it, remove formatting, decode it to a byte buffer, decrypt it. – Alex K. Jul 28 '16 at 11:04
  • Thanks @AlexK. for encryption I have no issue it is working fine but the output of encrypted string is something like xxx9xxx7658xxxxx/xxxxxx= whereas I need that string to be parsed as xxxx-xxxx-xxxx-xxxx-xxxx and then from this string to get bac original string xxx9xxx7658xxxxx/xxxxxx= – Aqeel Jul 28 '16 at 11:10
  • @dotctor its not just to divide byte array in chunks but also to get back that original string or byte array. Dividing I have added code for converting byte into chuck of strings. I need the reverse of process to get that original value as well. Kindly review the question again. – Aqeel Jul 28 '16 at 11:12

1 Answers1

0

You should use some sort of encryption, so that you can decrypt your original string again. MD5 is a hashing function, which means that after hashing the string there is no (algorithmic) function that can reverse the hash to the original string.

Once you've encrypted the plaintext to ciphertext using an encryption function, you can format this encrypted string in any desired format you like, as long as it is a reversible function. So let's say your encrypted string is "123456789", you'd format this into let's say: "12 - 3456 - 6 - 789".

Considering you know after how many characters you added a separator, you can simply reverse this key to the ciphertext and use the decryption method to obtain the original string.

On side note: I assume you need this for some kind of email verification. A good method to do this in a more simple way is to just create a hash from the email ( or whatever you're verifying) with a salt, and send this string with the email to the user. Since you saved the activation key into your database you can then easily couple the key with the corresponding email address.

Glubus
  • 2,819
  • 1
  • 12
  • 26
  • I have encryption algorithm which is working fine I just need to format the output as mention above if string is "123456789", how can i format to following string "12 - 3456 - 6 - 789" and after that decode it again. – Aqeel Jul 28 '16 at 11:35
  • Right so just do that then. Or are you asking what code could do that for you? In that case I recommend you asking another question on here or just Google that yourself: "format string with separators". Decoding it again is just a matter of calling the decryption method that belongs to the encryption method. If you only found an encryption function, then you need to search the decryption function with it, however I'd just use some plugin if I were you. Those usually have both the encrypt and decrypt functions already defined. – Glubus Jul 28 '16 at 11:38