fork(3) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using System.Globalization;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. String cryptedText = "debac58d0bc8526339678667bca923e15a7106a0c16c8148bd1468cefad57762ccf53a4a780bc27748c5583a02c41dee";
  15. String key = "qwertyuioplkjhgfdsaz";
  16. Console.WriteLine("Crypted Text = " + cryptedText);
  17. Console.WriteLine("Key = " + key);
  18. Console.WriteLine("Decrypted = " + Decrypt(cryptedText, key));
  19. Console.ReadKey();
  20. }
  21.  
  22. public static string Decrypt(string toDecrypt, string key)
  23. {
  24. byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); // AES-256 key
  25. //TODO: make it support 16 bytes, 32 bytes 64 bytes etc.. only supports 24 bytes key atm
  26. PadToMultipleOf(ref keyArray, 8);
  27. //byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
  28. byte[] toEncryptArray = ConvertHexStringToByteArray(toDecrypt);
  29.  
  30. RijndaelManaged rDel = new RijndaelManaged();
  31. rDel.KeySize = (keyArray.Length * 8); // in bits
  32. rDel.Key = keyArray;
  33. rDel.Mode = CipherMode.ECB; // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
  34. rDel.Padding = PaddingMode.None; // better lang support
  35. ICryptoTransform cTransform = rDel.CreateDecryptor();
  36. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  37. return UTF8Encoding.UTF8.GetString(resultArray);
  38. }
  39.  
  40. public static void PadToMultipleOf(ref byte[] src, int pad)
  41. {
  42. int len = (src.Length + pad - 1) / pad * pad;
  43. Array.Resize(ref src, len);
  44. }
  45.  
  46. public static byte[] ConvertHexStringToByteArray(string hexString)
  47. {
  48. if (hexString.Length % 2 != 0)
  49. {
  50. throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
  51. }
  52.  
  53. byte[] HexAsBytes = new byte[hexString.Length / 2];
  54. for (int index = 0; index < HexAsBytes.Length; index++)
  55. {
  56. string byteValue = hexString.Substring(index * 2, 2);
  57. HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
  58. }
  59.  
  60. return HexAsBytes;
  61. }
  62. }
  63. }
  64.  
Success #stdin #stdout 0.09s 34264KB
stdin
Standard input is empty
stdout
Crypted Text = debac58d0bc8526339678667bca923e15a7106a0c16c8148bd1468cefad57762ccf53a4a780bc27748c5583a02c41dee
Key = qwertyuioplkjhgfdsaz
Decrypted = http://w...content-available-to-author-only...o.com/hellohello.asp