Miguel Moura
Miguel Moura

Reputation: 39354

Convert String to Int and back to String

I am trying to convert an email String to Int32 then hash it using HashidsNet.

Finally, I revert the process to get the email again ... I tried the following:

HashidsNet.Hashids hash = new HashidsNet.Hashids();
String email = "[email protected]";
Byte[] bytes = Encoding.UTF8.GetBytes(email);
Int32 number = BitConverter.ToInt32(bytes, 0);
String hashed = hash.Encrypt(number);
Int32[] numbers = hash.Decrypt(hashed);
Byte[] newBytes = BitConverter.GetBytes(numbers[0]);
String newEmail = Encoding.UTF8.GetString(newBytes);

Somehow newEmail becomes only "john".

What am I missing?

Upvotes: 1

Views: 1503

Answers (1)

Joey
Joey

Reputation: 354356

You convert the first four bytes (Int32 is a 32-bit type) to a number, encrypt, decrypt and decode them again. Four bytes in UTF-8 fit john quite nicely. You need an array of integers and in that case you can just as well use the byte[].

Upvotes: 2

Related Questions