/// <summary> /// Decript value preveously encripted by EncriptIntToHexString() /// </summary> /// <param name="value">Hex string, like "FFFFFFFF"</param> /// <param name="skip32Key">The same key as was passed to EncriptIntToHexString()</param> /// <param name="defaultValue">If a non-null value is passed, fail silently and return it. Otherwise throw.</param> /// <returns>Original int value passed to EncriptIntToHexString()</returns> public static int DecriptHexStringToInt(string value, string skip32Key, int?defaultValue = null) { try { if (skip32Key.Length != 10) { throw new ArgumentOutOfRangeException("The key must be 10 chars long."); } var unsigned = UInt32.Parse(value, NumberStyles.HexNumber); var bytes = BitConverter.GetBytes(unsigned); var encrypted = BitConverter.ToInt32(bytes, 0); var cipher = new Eleven41.Skip32.Skip32Cipher(Encoding.ASCII.GetBytes(skip32Key)); return(cipher.Decrypt(encrypted)); } catch { if (defaultValue.HasValue) { // Fail silently. return(defaultValue.Value); } else { throw; } } }
/// <summary> /// Encript an int value into a 8-char hex string like "FFFFFFFF" /// </summary> /// <param name="value">Original value</param> /// <param name="skip32Key">The key must be 10 chars long</param> /// <returns>8-char hex string like "FFFFFFFF"</returns> public static string EncriptIntToHexString(int value, string skip32Key) { if (skip32Key.Length != 10) { throw new ArgumentOutOfRangeException("The key must be 10 chars long."); } var cipher = new Eleven41.Skip32.Skip32Cipher(Encoding.ASCII.GetBytes(skip32Key)); var encrypted = cipher.Encrypt(value); // Convert to UInt32 to avoid a sign. var bytes = BitConverter.GetBytes(encrypted); var unsigned = BitConverter.ToUInt32(bytes, 0); var encryptedString = unsigned.ToString("X"); // 8 chars return(encryptedString); }