-
-
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#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
Updated, thanks.
…On Fri, Oct 30, 2020 at 2:06 PM Ikechi Michael ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Line 24 should be:
byte[] cipherByte = ToByteArray(decryptedString);
There's no cipher variable in that scope.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/6284aa0db17d50834490321211b97da4#gistcomment-3510247>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACUSEDCEAKM5WYJFLE5PUC3SNK25BANCNFSM4TE6NZAQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 24 should be:
There's no
cipher
variable in that scope.