簡體   English   中英

使用自定義加密密碼時C#Base64編碼/解碼失敗

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

我目前正在編寫一個程序(正在使用自定義方法)對密碼進行加密,然后使用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));
    }
}

這是因為您使用編碼算法干擾字符串的Unicode“字符”,然后使用那些可能無法形成有效Unicode流的“字符”構造字符串的方式。

從String轉換為Byte數組並再次返回時,您需要確定要使用哪種編碼....並且您不能隨意更改字節流(通過加密例程),並且期望它在生成時產生有效的字符串被轉換回來。

我已經修改了您的代碼以顯示一些字符串到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