2013-07-20 120 views
0

進出口目前寫入被加密的密碼(使用自定義的方法),然後編碼使用To/FromBase64Transform類密碼爲Base64的程序。問題是,當我編碼我的加密密碼時,我無法將其解碼回適當的加密狀態。 Base64Helper類只是To/FromBase64Transform類的包裝器。C#Base64編碼/解碼失敗

我的測試代碼:

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

任何幫助將非常感激!

UPDATE:這是我的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)); 
    } 
} 
+0

你能告訴你的Base64Helper.EncodeString和.DecodeString,謝謝(我想象你正在做字符串字節有[]轉換) –

+0

更新和補充 – Wilson212

回答

1

這是因爲你用的編碼算法的字符串的Unicode「字符數」干擾,然後構建使用這些「字符串的方法字符「,然後可能不會形成一個有效的Unicode流。

當從字符串轉換爲字節數組,然後再返回,你需要決定使用哪種編碼....你不能隨意更改的字節流(通過你的加密例程),並期望它產生轉換回來時的有效字符串。

我修改你的代碼,以顯示某些字符串爲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; 
} 

}

+0

測試,將brb:D – Wilson212

+0

你是一個真正的!非常感謝! – Wilson212