使用自定义加密密码时,C#Base64编码/解码失败

时间:2013-07-20 01:55:28

标签: c# encryption base64

我正在编写一个加密密码的程序(使用自定义方法),然后使用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);
    }

结果是:

enter image description here

非常感谢任何帮助!

更新:这是我的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));
    }
}

1 个答案:

答案 0 :(得分:1)

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

当从String转换为Byte数组并再次返回时,您需要决定使用哪种编码....并且您不能随意更改字节流(通过加密例程)并期望它生成转换回来时的有效字符串。

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

enter image description here

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;
}

}