在C#中,使用HttpClientUtility進行數據加密通常涉及到兩個方面:發送加密數據和接收解密數據。這里我們將使用HTTPS協議來實現數據的加密傳輸。以下是一個簡單的示例,展示了如何使用HttpClientUtility發送加密數據并接收解密數據。
首先,確保你的項目已經引用了System.Net.Http
和System.Security.Cryptography
命名空間。
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());
}
}
}
}
}
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
替換為你自己的鹽值。