在当今的数字世界中,保护敏感信息至关重要。确保数据保密性的一种方法是通过加密。在本文中,我们将探讨如何使用C#和AES加密算法对数据进行加密和解密。
在C#中进行加密非常容易,下面为你展示:
using System;
using System.IO;
using System.Security.Cryptography;
class Program
{
private static readonly byte[] Key = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16
};
static async Task Main()
{
string filePath = "TestData.txt";
string message = "Here text you want cipher";
try
{
await EncryptToFileAsync(filePath, message);
Console.WriteLine("The file was encrypted.");
}
catch (Exception ex)
{
Console.WriteLine($"The encryption failed. {ex}");
}
}
private static async Task EncryptToFileAsync(string filePath, string message)
{
using FileStream fileStream = new(filePath, FileMode.OpenOrCreate);
using Aes aes = Aes.Create();
aes.Key = Key;
byte[] iv = aes.IV;
await fileStream.WriteAsync(iv, 0, iv.Length);
using CryptoStream cryptoStream = new(fileStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
using StreamWriter encryptWriter = new(cryptoStream);
await encryptWriter.WriteLineAsync(message);
}
}
但在C#中,这意味着要使用一种一致的加密算法(比如AES),在发送数据前对其进行加密,并在接收后对其进行解密。加密密钥必须在发送方和接收方之间安全地共享,而且必须实现正确的解密逻辑来还原原始消息。如果无法解密数据,加密就毫无意义,授权用户也就无法访问敏感信息。因此,在通过URL传输敏感数据时,加密和解密对于确保安全通信都是不可或缺的。
下面为你展示用C#进行解密的代码:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class Program
{
private static readonly byte[] Key = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16
};
static async Task Main()
{
string filePath = "TestData.txt";
string message = "Here text you want cipher";
try
{
await EncryptToFileAsync(filePath, message);
Console.WriteLine("The file was encrypted.");
}
catch (Exception ex)
{
Console.WriteLine($"The encryption failed. {ex}");
}
try
{
string decryptedMessage = await DecryptFromFileAsync(filePath);
Console.WriteLine($"The decrypted original message: {decryptedMessage}");
}
catch (Exception ex)
{
Console.WriteLine($"The decryption failed. {ex}");
}
}
private static async Task EncryptToFileAsync(string filePath, string message)
{
using FileStream fileStream = new(filePath, FileMode.OpenOrCreate);
using Aes aes = Aes.Create();
aes.Key = Key;
byte[] iv = aes.IV;
await fileStream.WriteAsync(iv, 0, iv.Length);
using CryptoStream cryptoStream = new(fileStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
using StreamWriter encryptWriter = new(cryptoStream);
await encryptWriter.WriteLineAsync(message);
}
private static async Task<string> DecryptFromFileAsync(string filePath)
{
using FileStream fileStream = new(filePath, FileMode.Open);
using Aes aes = Aes.Create();
byte[] iv = new byte[aes.IV.Length];
await fileStream.ReadAsync(iv, 0, iv.Length);
using CryptoStream cryptoStream = new(fileStream, aes.CreateDecryptor(Key, iv), CryptoStreamMode.Read);
using StreamReader decryptReader = new(cryptoStream);
return await decryptReader.ReadToEndAsync();
}
}
当通过GET方法经由URL发送敏感数据时,确保数据在传输过程中保持安全是很重要的。URL通常会被记录在服务器日志、浏览器历史记录中,如果没有适当的保护,在传输过程中还可能被拦截。为了降低这些风险,建议在将数据作为查询参数附加到URL之前对其进行加密。这样一来,即使URL被拦截或记录,数据的实际内容仍然是保密的。
在C#中,我们可以使用诸如AES之类的加密技术,在将数据编码为URL安全格式(比如Base64)之前对其进行安全加密。通过加密数据,你可以确保只有拥有解密密钥的授权方才能访问敏感信息,即便URL被暴露也是如此。这种方法增加了一层额外的安全保障,使得攻击者更难以访问或滥用数据。
下面为你展示创建用于发送数据的URL功能的代码:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class Program
{
private static readonly byte[] Key = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16
};
static async Task Main()
{
string filePath = "TestData.txt";
string message = "Here text you want cipher";
try
{
await EncryptToFileAsync(filePath, message);
Console.WriteLine("The file was encrypted.");
string encryptedBase64 = EncryptToBase64(message);
Console.WriteLine($"Encrypted message in Base64 Raw: {encryptedBase64}");
// 这会显示类似这样的内容 -> "rd0nGXhrpjSrb1kViOT1kLEp3WObOVle1BwKkrDJySQ="
}
catch (Exception ex)
{
Console.WriteLine($"The encryption failed. {ex}");
}
try
{
string decryptedMessage = await DecryptFromFileAsync(filePath);
string encryptedBase64 = EncryptToBase64(decryptedMessage);
Console.WriteLine($"Encrypted message in Base64 Cipher: {encryptedBase64}");
// 这会显示类似这样的内容 -> "iQ1WHZAGcCTm3Y+HMDH4oaxZJSozQj/QBew9IdwXKO8="
Console.WriteLine($"The decrypted original message: {decryptedMessage}");
}
catch (Exception ex)
{
Console.WriteLine($"The decryption failed. {ex}");
}
}
private static async Task EncryptToFileAsync(string filePath, string message)
{
using FileStream fileStream = new(filePath, FileMode.OpenOrCreate);
using Aes aes = Aes.Create();
aes.Key = Key;
byte[] iv = aes.IV;
await fileStream.WriteAsync(iv, 0, iv.Length);
using CryptoStream cryptoStream = new(fileStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
using StreamWriter encryptWriter = new(cryptoStream);
await encryptWriter.WriteLineAsync(message);
}
private static async Task<string> DecryptFromFileAsync(string filePath)
{
using FileStream fileStream = new(filePath, FileMode.Open);
using Aes aes = Aes.Create();
byte[] iv = new byte[aes.IV.Length];
await fileStream.ReadAsync(iv, 0, iv.Length);
using CryptoStream cryptoStream = new(fileStream, aes.CreateDecryptor(Key, iv), CryptoStreamMode.Read);
using StreamReader decryptReader = new(cryptoStream);
return await decryptReader.ReadToEndAsync();
}
private static string EncryptToBase64(string message)
{
using (Aes aes = Aes.Create())
{
aes.Key = Key;
byte[] iv = aes.IV;
using (MemoryStream ms = new())
{
using (CryptoStream cryptoStream = new(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
using (StreamWriter writer = new(cryptoStream))
{
writer.Write(message);
}
}
byte[] encryptedBytes = ms.ToArray();
string encryptedBase64 = Convert.ToBase64String(iv.Concat(encryptedBytes).ToArray());
return encryptedBase64;
}
}
}
}
在C#中,对通过URL传输的敏感数据进行加密不仅易于实现,而且对安全性至关重要。借助像System.Security.Cryptography这样的内置库,你可以快速应用诸如AES之类的加密算法,在将数据附加到URL之前对其进行保护。这确保了即使URL被拦截或记录,敏感信息仍然不可读。尽管这个过程很简单,但它在保护隐私和防止未经授权访问方面起着至关重要的作用,对于任何处理通过URL传输机密数据的应用程序来说,这都是一种必不可少的做法。
如果你喜欢我的文章,请给我一个赞!谢谢