您的位置首页  散文评论

数据加密存储(数据加密存储 国际标准)快来看

C#可以使用以下方式优雅实现数据加密存储、模糊匹配和脱敏:数据加密存储:可以使用C#内置的加密类库,如System.Security.Crypt

数据加密存储(数据加密存储 国际标准)快来看

 

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.IsOrEmpty(pattern) || string.IsOrEmpty(text))

{returnfalse; } int patternIndex = 0; int textIndex = 0;while (patternIndex < pattern.Length && textIndex < text.Length)

{if (pattern[patternIndex] == text[textIndex]) { patternIndex++; textIndex++; }elseif (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.IsOrEmpty(data)) {return data; } string pattern = @"(?<=\d{3})\d(?=\d{4})";return Regex.Replace(data, pattern,

"*"); }}

免责声明:本站所有信息均搜集自互联网,并不代表本站观点,本站不对其真实合法性负责。如有信息侵犯了您的权益,请告知,本站将立刻处理。联系QQ:1640731186