0

我目前正在编写一个加密密码的程序(使用自定义方法),然后使用 To/FromBase64Transform 类将密码编码为 Base64。问题是,当我对我的加密密码进行编码时,我无法将其解码回正确的加密状态。Base64Helper 类只是 To/FromBase64Transform 类的包装器。

我的测试代码:

static void Main(string[] args)
    {
        bool Worked = false;
        string Password = "testing";
        Console.WriteLine("Password: " + Password);

        // == Encode then decode 64 test. DecPass64 should equal password == //

        // Encodes to Base64 using ToBase64Transform
        string EncPass64 = Base64Helper.EncodeString(Password);

        // Decodes a Base64 string using FromBase64Transform
        string DecPass64 = Base64Helper.DecodeString(EncPass64);

        // Test if base 64 ecoding / decoding works
        Worked = (Password == DecPass64);
        Console.WriteLine();
        Console.WriteLine("Base64 Pass Encoded: " + EncPass64);
        Console.WriteLine("Base64 Pass Decoded: " + DecPass64);
        Console.WriteLine("Base64 Encode to Base64 Decode Worked? : " + Worked); // True

        // gspassenc uses XOR to switch passwords back and forth between encrypted and decrypted
        string GsEncodedPass = gspassenc(Password);
        string GsDecodedPass = gspassenc(GsEncodedPass);
        Worked = (Password == GsDecodedPass);

        // GsDecodedPass should equal the original Password
        Console.WriteLine();
        Console.WriteLine("GsPass Encoded: " + GsEncodedPass);
        Console.WriteLine("GsPass Decoded: " + GsDecodedPass);
        Console.WriteLine("GsEncode to GsDecode Worked? : " + Worked); // True

        // Bas64 encode the encrypted password. Then decode the base64. B64_GsDecodedPass should equal
        // the GsEncoded Password... But it doesn't for some reason!
        string B64_GsEncodedPass = Base64Helper.EncodeString(GsEncodedPass);
        string B64_GsDecodedPass = Base64Helper.DecodeString(B64_GsEncodedPass);
        Worked = (B64_GsDecodedPass == GsEncodedPass);

        // Print results
        Console.WriteLine();
        Console.WriteLine("Base64 Encoded GsPass: " + B64_GsEncodedPass);
        Console.WriteLine("Base64 Decoded GsPass: " + B64_GsDecodedPass);
        Console.WriteLine("Decoded == GS Encoded Pass? : " + Worked); // False

        // Stop console from closing till we say so
        Console.Read();
    }

    private static int gslame(int num)
    {
        int c = (num >> 16) & 0xffff;
        int a = num & 0xffff;

        c *= 0x41a7;
        a *= 0x41a7;
        a += ((c & 0x7fff) << 16);

        if (a < 0)
        {
            a &= 0x7fffffff;
            a++;
        }

        a += (c >> 15);

        if (a < 0)
        {
            a &= 0x7fffffff;
            a++;
        }

        return a;
    }

    private static string gspassenc(string pass)
    {
        int a = 0;
        int num = 0x79707367; // gspy
        int len = pass.Length;
        char[] newPass = new char[len];

        for (int i = 0; i < len; ++i)
        {
            num = gslame(num);
            a = num % 0xFF;
            newPass[i] = (char)(pass[i] ^ a);
        }

        return new String(newPass);
    }

结果是:

在此处输入图像描述

任何帮助都感激不尽!

更新:这是我的 Base64Helper 类:

class Base64Helper
{
    public static string DecodeString(string encoded)
    {
        return Encoding.ASCII.GetString(Convert.FromBase64String(encoded));
    }

    public static string EncodeString(string decoded)
    {
        return Convert.ToBase64String(Encoding.ASCII.GetBytes(decoded));
    }
}
4

1 回答 1

1

这是因为您使用编码算法干扰字符串的 Unicode“字符”,然后使用那些可能无法形成有效 Unicode 流的“字符”构造字符串。

从您的字符串转换为字节数组并再次转换回来时,您需要决定使用哪种编码......并且您不能任意更改字节流(通过您的加密例程)并期望它在以下情况下产生有效的字符串被转换回来。

我已经修改了您的代码以显示一些字符串到字节 [] 的转换步骤...您可以根据需要调整这些。

在此处输入图像描述

static void Main(string[] args)
{
    bool Worked = false;
    string Password = "testing";
    Console.WriteLine("Password: " + Password);

    // == Encode then decode 64 test. DecPass64 should equal password == //

    // Encodes to Base64 using ToBase64Transform
    string EncPass64 = Base64Helper.EncodeString(Password);

    // Decodes a Base64 string using FromBase64Transform
    string DecPass64 = Base64Helper.DecodeString(EncPass64);

    // Test if base 64 ecoding / decoding works
    Worked = (Password == DecPass64);
    Console.WriteLine();
    Console.WriteLine("Base64 Pass Encoded: " + EncPass64);
    Console.WriteLine("Base64 Pass Decoded: " + DecPass64);
    Console.WriteLine("Base64 Encode to Base64 Decode Worked? : " + Worked); // True

    // gspassenc uses XOR to switch passwords back and forth between encrypted and decrypted
    byte [] passwordbytes = Encoding.UTF8.GetBytes(Password);
    byte [] bytes_GsEncodedPass = gspassenc(passwordbytes);
    string GsEncodedPass = Encoding.UTF8.GetString(bytes_GsEncodedPass);
    byte[] bytes_GsDecodedPass = gspassenc(bytes_GsEncodedPass);
    string GsDecodedPass = Encoding.UTF8.GetString(bytes_GsDecodedPass);
    Worked = (Password == GsDecodedPass);

    // GsDecodedPass should equal the original Password
    Console.WriteLine();
    Console.WriteLine("GsPass Encoded: " + GsEncodedPass);
    Console.WriteLine("GsPass Decoded: " + GsDecodedPass);
    Console.WriteLine("GsEncode to GsDecode Worked? : " + Worked); // True

    // Bas64 encode the encrypted password. Then decode the base64. B64_GsDecodedPass should equal
    // the GsEncoded Password... But it doesn't for some reason!
    string B64_GsEncodedPass = Convert.ToBase64String(bytes_GsEncodedPass);
    byte []bytes_B64_GsDecodedPass = Convert.FromBase64String(B64_GsEncodedPass);
    string B64_GsDecodedPass = Encoding.UTF8.GetString(bytes_B64_GsDecodedPass);
    Worked = (B64_GsDecodedPass == GsEncodedPass);

    // Print results
    Console.WriteLine();
    Console.WriteLine("Base64 Encoded GsPass: " + B64_GsEncodedPass);
    Console.WriteLine("Base64 Decoded GsPass: " + B64_GsDecodedPass);
    Console.WriteLine("Decoded == GS Encoded Pass? : " + Worked); // False

    // Stop console from closing till we say so
    Console.Read();
}

private static int gslame(int num)
{
    int c = (num >> 16) & 0xffff;
    int a = num & 0xffff;

    c *= 0x41a7;
    a *= 0x41a7;
    a += ((c & 0x7fff) << 16);

    if (a < 0)
    {
        a &= 0x7fffffff;
        a++;
    }

    a += (c >> 15);

    if (a < 0)
    {
        a &= 0x7fffffff;
        a++;
    }

    return a;
}

private static byte[] gspassenc(byte [] pass)
{
    int a = 0;
    int num = 0x79707367; // gspy
    int len = pass.Length;
    byte[] newPass = new byte[len];

    for (int i = 0; i < len; ++i)
    {
        num = gslame(num);
        a = num % 0xFF;
        newPass[i] = (byte)(pass[i] ^ a);
    }

    return newPass;
}

}

于 2013-07-20T02:55:08.863 回答