2013-07-20 116 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