2016-03-07 26 views
0

在幾篇文章中,我發現了一個基本的C#加密和解密。我建立了一個作爲勝利形式的帖子,它的工作是正確的。爲什麼C#加密和解密工作在Windows Form而不是在Web API 2中?

我使用一個特殊的鍵: ZWT451 +路由Ydgp $ 8524 * Aksdsvgh +

我使用一個字符串爲:我的名字是溢出。

這是我得到的回報: MEPtmZFj3YaToiTT5V3xO/LwYo1U9YAt

我用這個代碼,並BUIL我的Web API 2.比我跑我的Web API 2我用下面的鏈接後,對數據進行加密發送到web api 2如下所示。

http://localhost:49407/api/MyCntRDS/MyCntRoute?fromacreli= MEPtmZFj3YaToiTT5V3xO/LwYo1U9YAt 

以下是錯誤我得到:

{"Message":"An error has occurred.","ExceptionMessage":"Invalid length for a Base-64 char array or string.","ExceptionType":"System.FormatException","StackTrace":" at System.Convert.FromBase64_Decode(Char* startInputPtr, Int32 inputLength, Byte* startDestPtr, Int32 destLength)\r\n at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)\r\n at System.Convert.FromBase64String(String s)\r\n at ELITACR890LTKB.Controllers.Acr89Ltkb1939Controller.DecryptionACR890String 

後來我刪除我的加密和解密的網絡API和發送基本數據:

我#名字#是#溢出

並且完美地工作。

這裏是它在C#WinForm的工作的代碼,而不是在網絡API 2.

任何幫助,請。

public Form1() 
{ 
    InitializeComponent(); 

} 

    // BTN ENCRYPT +++++++++++++++++++++++++++++++++++++++++++++++ 
    private void Encrypt_Click(object sender, EventArgs e) 
    { 
     // Text String to be Encrypt  
     byte[] buffer = Encryption(textBox1.Text, txtKey.Text); 
     string b = Convert.ToBase64String(buffer); 

     // Password 
     textBox2.Text = b; 
    } 

    // BTN DECRYPT +++++++++++++++++++++++++++++++++++++++++++++++ 
    private void Decrypt_Click(object sender, EventArgs e) 
    { 
     // Decrypt Text String 
     textBox3.Text = Decryption(textBox2.Text, txtKey.Text); 
    } 


    // ENCRYPTION 
    public static byte[] Encryption(string PlainText, string key) 
    { 
     TripleDES des = CreateDES(key); 
     ICryptoTransform ct = des.CreateEncryptor(); 
     byte[] input = Encoding.UTF8.GetBytes(PlainText); 
     return ct.TransformFinalBlock(input, 0, input.Length); 
    } 


    // DECRYPTION 
    public static string Decryption(string CypherText, string key) 
    { 
     byte[] b = Convert.FromBase64String(CypherText); 
     TripleDES des = CreateDES(key); 
     ICryptoTransform ct = des.CreateDecryptor(); 
     byte[] output = ct.TransformFinalBlock(b, 0, b.Length); 
     return Encoding.UTF8.GetString(output); 
    } 

    // TRIPLEDES/3DES 
    static TripleDES CreateDES(string key) 
    { 
     MD5 md5 = new MD5CryptoServiceProvider(); 
     TripleDES des = new TripleDESCryptoServiceProvider(); 
     des.Key = md5.ComputeHash(Encoding.UTF8.GetBytes(key)); 
     des.IV = new byte[des.BlockSize/8]; 
     return des; 
    } 
} 

這是我得到一個錯誤的web api代碼。 。

[RoutePrefix("api/MyCntRoute")] 
public class MyCntRDS: ApiController 
{ 
     string _SpecialKey = "ZWT451+Route!Ydgp$8524*Aksdsvgh+// 32 Character 

     [Route("MyCntRoute")] 
     [HttpGet] 
     public List<ACR1939ELITE> Get(string fromterminal) 
     { 

      getDataFromACR890 = DecryptionACR890String(fromterminal, _SpecialKey); 

      .............. 
     .............. 
     .............. 
     } 
} 


     // DECRYPTION 
     private static string DecryptionACR890String (string fromterminal, string _SpecialKey) 
     { 
      byte[] b = Convert.FromBase64String(fromterminal); 
      TripleDES des = CreateDes(_SpecialKey); 
      ICryptoTransform ct = des.CreateDecryptor(); 
      byte[] output = ct.TransformFinalBlock(b, 0, b.Length); 
      return Encoding.UTF8.GetString(output); 
     } 
+1

看起來像工作,你應該張貼沒有工作的一個代碼。但無論如何,看起來像「fromacreli = MEPtmZFj3YaToiTT5V3xO/LwYo1U9YAt」應該是「fromacreli = MEPtmZFj3YaToiTT5V3xO/LwYo1U9YAt」 –

+0

@NTMS在您的解密方法中放置一個斷點,看看它試圖解密什麼.BASE64應該是4的倍數。正如user2033402所說,你一開始就有空間。 – CathalMF

+0

我盡力了,但沒有工作。我嘗試在Web API中使用Enable System.Diagnostics Tracing,但這也沒有奏效。 (h ** p://w*w.asp.net/web-api/overview/testing-and-debugging/tracing-in-aspnet-web-api) – NTMS

回答

0

@Alex它與這個工作:

串傳入= returnValue.Replace( '_', '/').Replace('-', '+')替換(」」, '+');

我添加了.Replace('','+');部分用空白空間代替原來的+代碼。

您提供了一個鏈接,並且它工作得有限。

How to achieve Base64 URL safe encoding in C#?

相關問題