无法从AES解密c#获取纯文本

时间:2014-02-28 02:30:58

标签: c# aes

我正在尝试创建需要逆向工程的CTF挑战。

所以基本上我需要加密一个字符串,所以我知道纯文本的加密值。在这种情况下,“xor_flag”。一旦我有了,我将擦除纯文本值,并只使用解密函数。但就目前而言,我只需要加强工作。我最接近的是:它以十六进制值显示纯文本。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;

namespace XorFlag2
{
class Program
{
    // USe de4dot on the .exe and use .net reflector on cleaned one,after de4dot finishes.
    // IN reflector go to main method

    //num var is what you need . For xoring you can use windows calculator(->scientific mode)
    // so: num xor 53129566096 must be = 65535655351
    // num is = 65535655351 xor 53129566096 (=13371337255)
    // start program type: 13371337255 ,hit enter and you 'll get the flag

    private static void Main(string[] args)
    {
        Console.WriteLine("Greetings challenger! Step right up and try your shot at gaining the flag!");
        Console.WriteLine("You'll have to know the pascode to unlock the prize:");
        long num = Convert.ToInt64(Console.ReadLine());
        if ((num ^ 53129566096L) == 65535655351L)
        {
            Console.WriteLine("yay");
        }
        else
        {
            Console.WriteLine("Incorrect, try again!");
        }

            byte[] iV = new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
            byte[] array = new byte[16];
            BitConverter.GetBytes(num).CopyTo(array, 0);
            BitConverter.GetBytes(num).CopyTo(array, 8);
            string s = "ls/5RTxwflDrqr5G8pO9cQ1NlgQcFjcJj9x4z7oIhlfY4w42GAFqKbyzwqHAZQBZa5ctysKKWIbTgU2VxoR​YohxCbPyV6sEU/tn+sIxNg6A/r5OJnIMqTs0seMrzWh5J";
            string t = "flag_xor";
            byte[] encrypted = EncryptStringToBytes(t, array, iV);
            Console.WriteLine(BitConverter.ToString(encrypted));
           // str = DecryptStringFromBytes(Convert.FromBase64String(t), array, iV);
          //  Console.WriteLine(str);

       // catch (Exception exception)
        //{
           // Console.WriteLine("ERROR!!! darn. huh? how did I get here? Hmm, something must have gone wrong. What am I doing?", exception);
        //}
        Console.WriteLine("press key to continue");
        Console.ReadKey();
    }

    static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
    {

        // Check arguments. 
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");

        // Declare the string used to hold 
        // the decrypted text. 
        string plaintext = null;

        // Create an RijndaelManaged object 
        // with the specified key and IV. 
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;
            rijAlg.Padding = PaddingMode.None;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for decryption. 
            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {

                        // Read the decrypted bytes from the decrypting stream 
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }

        }

        return plaintext;

    }

    static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments. 
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        byte[] encrypted;
        // Create an RijndaelManaged object 
        // with the specified key and IV. 
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption. 
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }


        // Return the encrypted bytes from the memory stream. 
        return encrypted;

    }
}

}

0 个答案:

没有答案