C#을

2017-09-12 1 views
0

를 분석 토큰 지금과 같이 작동 하나 개 base64 문자열에 날짜와 사용자의 이메일을 결합하고 싶습니다 :C#을

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); 
} 

난 단지 디코딩에서 이메일을받을 수 있도록 나는 지금 그것을 구문 분석하고 싶습니다 함께 문자열,하지만 난 점점 오전 모든 :

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

이 같은 형태로 그것을 얻을 :

\ [email protected]

+3

별도로 이메일로 보낼 수 있습니다 | ** 및 디코딩 된 문자열을 분할합니다. – Youssef13

답변

3

당신이 날의 길이를 알다시피 당신은 시간을 읽고 당신은 **와 같은 특별한 기호로 가입 할 수있다 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 
0

#과 같이 전자 메일 이름과 날짜 사이에 구분 기호를 넣으십시오. 그런 다음 string.Split()을 사용하여 문자열을 분리하고 문자열 배열에 놓습니다.

전자 메일은 인덱스 [0]에있는 날짜 동안 배열의 인덱스 [1]에 있습니다.

+0

당신은 어떤 문제를 찾을 수 있기 때문에 코드로 무엇을 의미하는지 증명하고 싶을지도 모릅니다. 날짜와 이메일은 문자열이 아닌 바이트로 저장되므로 구분 문자로 문자열 문자가 작동하지 않습니다. 마찬가지로, 임의의 바이트 값은 이진 날짜 값에 나타날 수 있으므로 문제가 될 수 있습니다. – Chris

관련 문제