Skip to content

Instantly share code, notes, and snippets.

@abakam
Last active October 30, 2020 13:17
Show Gist options
  • Save abakam/6284aa0db17d50834490321211b97da4 to your computer and use it in GitHub Desktop.
Save abakam/6284aa0db17d50834490321211b97da4 to your computer and use it in GitHub Desktop.
Helper methods for decrypting Cipher (hex encoded string) given the IV and key for the Find Treasure Game using C#
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace FindTreasureApp.Services
{
public class Decryption
{
const string keyString = "57067125438768260656188878670043";
const string ivString = "5706712543876826";
/// <summary>
/// Decrypt the Ciper
/// </summary>
/// <param name="cipherId">The hex encoded string</param>
/// <param name="rounds">The number of times string was encoded</param>
public static string DecryptCiper(string cipherId, int rounds)
{
string decryptedString = cipherId;
while(rounds > 0)
{
byte[] cipherByte = ToByteArray(decryptedString);
decryptedString = AES_HEX_Decrypt_CBC(cipherByte);
rounds--;
}
return decryptedString;
}
static byte[] ToByteArray(string hexString)
{
byte[] retval = new byte[hexString.Length / 2];
for (int i = 0; i < hexString.Length; i += 2)
retval[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
return retval;
}
static string AES_HEX_Decrypt_CBC(byte[] cipherData)
{
byte[] key = Encoding.UTF8.GetBytes(keyString);
byte[] iv = Encoding.UTF8.GetBytes(ivString);
try
{
using (var rijndaelManaged =
new RijndaelManaged { Key = key, IV = iv, Mode = CipherMode.CBC })
using (var memoryStream =
new MemoryStream(cipherData))
using (var cryptoStream =
new CryptoStream(memoryStream,
rijndaelManaged.CreateDecryptor(key, iv),
CryptoStreamMode.Read))
{
return new StreamReader(cryptoStream).ReadToEnd();
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
}
}
@abakam
Copy link
Author

abakam commented Oct 30, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment