0

I would like to combine date and user email to one base64 string, which now works like this:

public string GenerateUniqueToken(string email)
{
    byte[] time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
    byte[] key = Encoding.ASCII.GetBytes(email);
    string encoded = Convert.ToBase64String(time.Concat(key).ToArray());
    return criptographyService.Encrypt(encoded);
}

I would like to parse it now so that I only get an email from the decoded string, but I am getting everything together:

public string TokenUserValid(string token)
{
    string decrypted = criptographyService.Decrypt(token);
    byte[] data = Convert.FromBase64String(decrypted);
    return Encoding.Default.GetString(data);
}

I get it in the form like this:

\�����[email protected]

1
  • 3
    You may join them with a special symbol like | and split the decoded string. Commented Sep 12, 2017 at 10:07

2 Answers 2

3

As you know the length of the date you can read the time and email separately from the byte[]

//combine time and email
byte[] time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
byte[] key = Encoding.ASCII.GetBytes("[email protected]");
string encoded = Convert.ToBase64String(time.Concat(key).ToArray());

//read time and email
byte[] data = Convert.FromBase64String(encoded);
DateTime date =  DateTime.FromBinary(BitConverter.ToInt64(data.Take(8).ToArray(), 0)); //read the date
string email  = Encoding.Default.GetString(data.Skip(8).ToArray()); //read the email
Sign up to request clarification or add additional context in comments.

Comments

0

Put a separator character between your date and your email name like # . Then use the string.Split(), to break them off and put them on an string array.

The email will be on the index[1], of your array, while the date on the Index[0].

1 Comment

You might want to demonstrate what you mean with code since you might then find some issues... As it is the date and email are stored as bytes rather than strings so a string character as a separator won't work. Likewise an arbitrary byte value might be problematic because it might appear in the binary date value causing you problems!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.