簡體   English   中英

為什么C#加密和解密在Windows Form而不是Web API 2中起作用?

[英]Why C# Encryption and Decryption works Windows Form and not in Web API 2?

在stackoverflow的幾篇文章中,我發現了使用C#進行基本加密和解密的方法。 我建立了一個帖子作為勝利表格,它可以正常工作。

我將特殊密鑰用作: ZWT451 + Route!Ydgp $ 8524 * Aksdsvgh +

我使用一個字符串作為:我的名字溢出了。

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

我使用此代碼並構建了Web API2。然后,在運行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

稍后,我刪除Web API中的加密和解密,並以以下方式發送基本數據:

我的#名稱#是#溢出

並完美地工作。

這是它在C#WinForm中工作的代碼,而不是在Web 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);
        }

@Alex與此一起工作:

字符串傳入= returnValue.Replace('_','/').Replace('-','+')。Replace('','+');

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

您提供了一個鏈接,但它提供了部分支持。

如何在C#中實現Base64 URL安全編碼?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM