91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

c# httputility怎樣實現數據加密

c#
小樊
81
2024-12-01 09:31:15
欄目: 編程語言

在C#中,使用HttpClientUtility進行數據加密通常涉及到兩個方面:發送加密數據和接收解密數據。這里我們將使用HTTPS協議來實現數據的加密傳輸。以下是一個簡單的示例,展示了如何使用HttpClientUtility發送加密數據并接收解密數據。

首先,確保你的項目已經引用了System.Net.HttpSystem.Security.Cryptography命名空間。

  1. 發送加密數據:
using System;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace HttpClientEncryptionExample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string url = "https://your-secure-server.com/api/endpoint";
            string data = "Your sensitive data";

            // 對數據進行加密
            string encryptedData = EncryptData(data);

            // 發送加密數據
            using (HttpClient client = new HttpClient())
            {
                var content = new StringContent(encryptedData, Encoding.UTF8, "application/json");
                HttpResponseMessage response = await client.PostAsync(url, content);

                if (response.IsSuccessStatusCode)
                {
                    string responseData = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("Response data: " + responseData);
                }
                else
                {
                    Console.WriteLine("Error: " + response.StatusCode);
                }
            }
        }

        static string EncryptData(string data)
        {
            byte[] clearBytes = Encoding.Unicode.GetBytes(data);

            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes("YourSalt", new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }

                    return Convert.ToBase64String(ms.ToArray());
                }
            }
        }
    }
}
  1. 接收解密數據:
using System;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace HttpClientDecryptionExample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string url = "https://your-secure-server.com/api/endpoint";

            // 發送加密數據(與上面相同)
            string encryptedData = EncryptData("Your sensitive data");

            // 發送加密數據
            using (HttpClient client = new HttpClient())
            {
                var content = new StringContent(encryptedData, Encoding.UTF8, "application/json");
                HttpResponseMessage response = await client.PostAsync(url, content);

                if (response.IsSuccessStatusCode)
                {
                    string responseData = await response.Content.ReadAsStringAsync();

                    // 對響應數據進行解密
                    string decryptedData = DecryptData(responseData);
                    Console.WriteLine("Decrypted response data: " + decryptedData);
                }
                else
                {
                    Console.WriteLine("Error: " + response.StatusCode);
                }
            }
        }

        static string DecryptData(string encryptedData)
        {
            byte[] encryptedBytes = Convert.FromBase64String(encryptedData);

            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes("YourSalt", new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(encryptedBytes, 0, encryptedBytes.Length);
                        cs.Close();
                    }

                    return Encoding.Unicode.GetString(ms.ToArray());
                }
            }
        }
    }
}

請注意,這個示例使用了AES加密算法和Rfc2898DeriveBytes類來生成密鑰和初始化向量(IV)。你需要根據你的需求選擇合適的加密算法和密鑰派生函數。同時,確保將YourSalt替換為你自己的鹽值。

0
高安市| 靖边县| 池州市| 濉溪县| 洛隆县| 宜兰市| 时尚| 嘉义县| 靖边县| 公安县| 孙吴县| 高邑县| 绥阳县| 呼玛县| 汉源县| 德惠市| 广宁县| 佛坪县| 左云县| 五华县| 高台县| 滦南县| 聊城市| 得荣县| 罗甸县| 湘潭县| 湘潭市| 富锦市| 沂南县| 台安县| 桃园市| 巴塘县| 汾西县| 比如县| 砀山县| 阜南县| 镇安县| 赤壁市| 沭阳县| 荣昌县| 于田县|