如何解码base64编码的字符串

本文关键字:base64 编码 字符串 解码 何解码 | 更新日期: 2023-09-27 18:06:26

我正在尝试"解码"以下Base64字符串:

OBFZDTcPCxlCKhdXCQ0kMQhKPh9uIgYIAQxALBtZAwUeOzcdcUEeW0dMO1kbPElWCV1ISFFKZ0kdWFlLAURPZhEFQVseXVtPOUUICVhMAzcfZ14AVEdIVVgfAUIBWVpOUlAeaUVMXFlKIy9rGUN0VF08Oz1POxFfTCcVFw1LMQNbBQYWAQ = =

这是我对字符串本身的了解:

  1. 原始字符串首先通过以下代码传递:

    private static string m000493(string p0, string p1)
    {
        StringBuilder builder = new StringBuilder(p0);
        StringBuilder builder2 = new StringBuilder(p1);
        StringBuilder builder3 = new StringBuilder(p0.Length);
        int num = 0;
    Label_0084:
        while (num < builder.Length)
        {
            int num2 = 0;
            while (num2 < p1.Length)
            {
                if ((num == builder.Length) || (num2 == builder2.Length))
                {
                    MessageBox.Show("EH?");
                    goto Label_0084;
                }
                char ch = builder[num];
                char ch2 = builder2[num2];
                ch = (char)(ch ^ ch2);
                builder3.Append(ch);
                num2++;
                num++;
            }
        }
        return m0001cd(builder3.ToString());
    }
    

    代码中的p1部分应该是字符串" _p0lizei. "

  2. 然后通过以下代码转换为Base64字符串:

    private static string m0001cd(string p0)
    {
        string str2;
        try
        {
            byte[] buffer = new byte[p0.Length];
            str2 = Convert.ToBase64String(Encoding.UTF8.GetBytes(p0));
        }
        catch (Exception exception)
        {
            throw new Exception("Error in base64Encode" + exception.Message);
        }
        return str2;
    }
    

问题是,我如何解码Base64字符串,以便我可以找出原始字符串是什么?

如何解码base64编码的字符串

Simple:

byte[] data = Convert.FromBase64String(encodedString);
string decodedString = System.Text.Encoding.UTF8.GetString(data);

m000493方法似乎执行某种异或加密。这意味着可以使用相同的方法对文本进行编码和解码。你所要做的就是反转m0001cd:

string p0 = Encoding.UTF8.GetString(Convert.FromBase64String("OBFZDT..."));
string result = m000493(p0, "_p0lizei.");
//    result == "gaia^unplugged^Ta..."

return m0001cd(builder3.ToString());改为return builder3.ToString();

    // Decode a Base64 string to a string
    public static string DecodeBase64(string value)
    {
        if(string.IsNullOrEmpty(value))
            return string.Empty;
        var valueBytes = System.Convert.FromBase64String(value);
        return System.Text.Encoding.UTF8.GetString(valueBytes);
    }

我们试试这个:

using System;
using System.IO;
class Program
{
    static void Main()
    {
        string inputFile = "input.txt";
        string originalString = File.ReadAllText(inputFile);
        string encodedString = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(originalString));
        string decodedString = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(encodedString));
        Console.WriteLine("Original string: " + originalString);
        Console.WriteLine("Encoded string: " + encodedString);
        Console.WriteLine("Decoded string: " + decodedString);
        string outputFile = "output.txt";
        File.WriteAllText(outputFile, decodedString);
    }
}

# csharp