简体   繁体   English

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

[英]C# Base64 encoding / decoding fails when using custom encrypted password

Im currently writing a program that is encrypting a password (using a custom method), and then encoding the password to Base64 using the To/FromBase64Transform classes. 我目前正在编写一个程序(正在使用自定义方法)对密码进行加密,然后使用To / FromBase64Transform类将密码编码为Base64。 The problem is, when i encode my encrypted password, I am unable to decode it back to its proper encrypted state. 问题是,当我对我的加密密码进行编码时,我无法将其解码回正确的加密状态。 The Base64Helper class is just a wrapper for the To/FromBase64Transform classes. Base64Helper类只是To / FromBase64Transform类的包装。

My Test Code: 我的测试代码:

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

And the result is: 结果是:

在此处输入图片说明

Any help will be much appreciated! 任何帮助都感激不尽!

UPDATE : Here is my Base64Helper Class: 更新 :这是我的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));
    }
}

It's because of the way you are interfering with the Unicode "Chars" of the string with the encoding algorithm and then constructing a String using those "Chars" which then might not form a valid Unicode stream. 这是因为您使用编码算法干扰字符串的Unicode“字符”,然后使用那些可能无法形成有效Unicode流的“字符”构造字符串的方式。

When converting from your String to a Byte array and back again, you need to decide which encoding to use....and you can't arbitrarily change the byte stream (via your encryption routine) and expect it to produce a valid string when being converted back. 从String转换为Byte数组并再次返回时,您需要确定要使用哪种编码....并且您不能随意更改字节流(通过加密例程),并且期望它在生成时产生有效的字符串被转换回来。

I've modified your code to show some string to byte[] conversion steps...you can adjust these depending on your need. 我已经修改了您的代码以显示一些字符串到byte []的转换步骤...您可以根据需要进行调整。

在此处输入图片说明

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM