c#如何优雅实现数据加密存储、模糊匹配和脱敏

C#可以使用以下方式优雅实现数据加密存储、模糊匹配和脱敏:

数据加密存储:可以使用C#内置的加密类库,如System.Security.Cryptography命名空间下的类。可以使用对称加密算法(如AES)或非对称加密算法(如RSA)对数据进行加密,再存储到数据库或文件中。在读取数据时,需要先进行解密操作。

模糊匹配:可以使用C#内置的字符串匹配类库,如System.Text.RegularExpressions命名空间下的类。可以使用正则表达式进行模糊匹配。例如,可以使用Regex.Match方法匹配字符串中的某个子串,或使用Regex.Replace方法替换字符串中的某些字符。

脱敏:可以使用C#内置的字符串处理类库,如System.String命名空间下的类。可以使用Substring方法截取字符串的部分内容,或使用Replace方法替换字符串中的某些字符。例如,可以将身份证号码的前几位和后几位进行脱敏处理,只显示中间部分的几个字符。

数据加密存储

using System;
using System.Security.Cryptography;
using System.Text;
public static class EncryptionHelper
{
    public static string Encrypt(string plaintext)
    {
        byte[] salt = new byte[] { 0x26, 0x19, 0x7E, 0x7A, 0x9C, 0x7D, 0x62, 0x96 };
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes("mysecretpassword", salt, 1000);
        Aes aes = Aes.Create();
        aes.Key = key.GetBytes(aes.KeySize / 8);
        aes.IV = key.GetBytes(aes.BlockSize / 8);
        byte[] encryptedBytes;
        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
            {
                byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext);
                cs.Write(plaintextBytes, 0, plaintextBytes.Length);
            }
            encryptedBytes = ms.ToArray();
        }
        return Convert.ToBase64String(encryptedBytes);
    }
    public static string Decrypt(string ciphertext)
    {
        byte[] salt = new byte[] { 0x26, 0x19, 0x7E, 0x7A, 0x9C, 0x7D, 0x62, 0x96 };
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes("mysecretpassword", salt, 1000);
        Aes aes = Aes.Create();
        aes.Key = key.GetBytes(aes.KeySize / 8);
        aes.IV = key.GetBytes(aes.BlockSize / 8);
        byte[] encryptedBytes = Convert.FromBase64String(ciphertext);
        byte[] plaintextBytes;
        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(encryptedBytes, 0, encryptedBytes.Length);
            }
            plaintextBytes = ms.ToArray();
        }
        return Encoding.UTF8.GetString(plaintextBytes);
    }
}

模糊匹配

using System;
public static class FuzzyMatchHelper
{
    public static bool FuzzyMatch(string pattern, string text)
    {
        if (string.IsNullOrEmpty(pattern) || string.IsNullOrEmpty(text))
        {
            return false;
        }
        int patternIndex = 0;
        int textIndex = 0;
        while (patternIndex < pattern.Length && textIndex < text.Length)
        {
            if (pattern[patternIndex] == text[textIndex])
            {
                patternIndex++;
                textIndex++;
            }
            else if (patternIndex == 0)
            {
                textIndex++;
            }
            else
            {
                patternIndex = 0;
            }
        }
        return patternIndex == pattern.Length;
    }
}

脱敏

using System;
using System.Text.RegularExpressions;
public static class DataMaskingHelper
{
    public static string Mask(string data)
    {
        if (string.IsNullOrEmpty(data))
        {
            return data;
        }
        string pattern = @"(?<=\d{3})\d(?=\d{4})";
        return Regex.Replace(data, pattern, "*");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要引入System.Security.Cryptography命名空间。 以下是C#窗体实现AES加密和解密的示例代码: ``` using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.Windows.Forms; namespace AesDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnEncrypt_Click(object sender, EventArgs e) { try { // 获取密钥和向量 string key = txtKey.Text.Trim(); string iv = txtIV.Text.Trim(); byte[] keyBytes = Encoding.UTF8.GetBytes(key); byte[] ivBytes = Encoding.UTF8.GetBytes(iv); // 获取明文 string plaintext = txtPlaintext.Text.Trim(); byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); // 创建AES加密器 using (Aes aes = Aes.Create()) { aes.Key = keyBytes; aes.IV = ivBytes; // 创建加密流 using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, aes.CreateEncryptor(), CryptoStreamMode.Write)) { // 将明文写入加密流 csEncrypt.Write(plaintextBytes, 0, plaintextBytes.Length); csEncrypt.FlushFinalBlock(); // 获取加密结果 byte[] ciphertextBytes = msEncrypt.ToArray(); string ciphertext = Convert.ToBase64String(ciphertextBytes); // 显示加密结果 txtCiphertext.Text = ciphertext; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnDecrypt_Click(object sender, EventArgs e) { try { // 获取密钥和向量 string key = txtKey.Text.Trim(); string iv = txtIV.Text.Trim(); byte[] keyBytes = Encoding.UTF8.GetBytes(key); byte[] ivBytes = Encoding.UTF8.GetBytes(iv); // 获取密文 string ciphertext = txtCiphertext.Text.Trim(); byte[] ciphertextBytes = Convert.FromBase64String(ciphertext); // 创建AES解密器 using (Aes aes = Aes.Create()) { aes.Key = keyBytes; aes.IV = ivBytes; // 创建解密流 using (MemoryStream msDecrypt = new MemoryStream(ciphertextBytes)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, aes.CreateDecryptor(), CryptoStreamMode.Read)) { // 读取解密结果 byte[] plaintextBytes = new byte[ciphertextBytes.Length]; int bytesRead = csDecrypt.Read(plaintextBytes, 0, plaintextBytes.Length); // 显示解密结果 string plaintext = Encoding.UTF8.GetString(plaintextBytes, 0, bytesRead); txtPlaintext.Text = plaintext; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } ``` 在窗体上添加三个TextBox控件和两个Button控件,分别命名为txtKey、txtIV、txtPlaintext、txtCiphertext、btnEncrypt和btnDecrypt。用户输入密钥、向量和明文,点击“加密”按钮进行加密,点击“解密”按钮进行解密。加密和解密过程中,采用AES算法进行加密和解密,使用Base64编码将密文转换为字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值