C# 加密总结 一些常见的加密方法

 散列数据 代码如下:

 原始散列对于彩虹表来说也存在漏洞,在彩虹表中,表内的每一条记录都是一串明文对应一种加密算法生成的一串密文。加盐就是指在密码中加入一个盐,这样可以提高密码散列的安全性。修改后的代码如下:

 二 对称加密

 调用方式:
            byte[] salt = Convert.FromBase64String(GetSalt(9, 18));
            string password = "Password";
            byte[] key=new byte[0];
            byte[] iv = new byte[0];
            GetKeyAndIVFromPasswordAndSalt(password,salt, new RijndaelManaged(), ref key, ref iv);
            string input = "Wrox Press";
            string encrytText = Encrypt(input, key, iv);
            Console.WriteLine(encrytText);
            string decryptText=Decrypt(encrytText,key,iv);
            Console.WriteLine(decryptText);

只是简单的加密和解密数据是不够的,我们还需要确保数据不被改变,我们可以创建一个消息认证代码来生成一个加密的散列。

private static string GenerateMac(string input, byte[] key)
      {
          HMACSHA512 hmac = new HMACSHA512(key);
          byte[] data= hmac.ComputeHash(Convert.FromBase64String(input));
          return Convert.ToBase64String(data);
      }
      static bool IsMacValid(string input, byte[] key, string savedMac)
      {
          string recalculateMac = GenerateMac(input, key);
          return recalculateMac.Equals(savedMac);
      }

 例如我们对数据库中LicenseNumber加密,那么我们就必须修改表结构,修改后如图:

非对称加密

 证书加密

 调用方式:

 string input = "Wrox Press";
            byte[] clearTextAsBytes = Encoding.UTF8.GetBytes(input);
            X509Certificate2 serverPublicKeyCertificate = LoadCertificateFromFile("IISExpress.cer");
            X509Certificate2 signingCertificate = GetCertificateBySubjectName("test");
            byte[] signedClearText = SignData(clearTextAsBytes, signingCertificate);
            byte[] encryptedAndSignedData = EncryptWithCertificate(signedClearText, serverPublicKeyCertificate);

            byte[] encodedUnencryptedCms = DecryptWithCertificate(encryptedAndSignedData);
            List<string> signingSubjects = new List<string>();
            byte[] receivedClearText = ValidateSignatureAndExtractContent(encodedUnencryptedCms, signingSubjects);
            string unecnryptedString = Encoding.UTF8.GetString(receivedClearText);
            Console.ReadLine();

我的计算机是win8,这里并没有用什么企业级证书,作为测试,我是用win8中IIS来创建自签名证书,然后用mmc来管理证书(http://softbbs.zol.com.cn/1/20_1370.html),所以以上的GetCertificateBySubjectName方法需要修改如下:

  private static X509Certificate2 GetCertificateBySubjectName(string subjectName)
        {
            X509Store myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            myStore.Open(OpenFlags.ReadOnly);

            X509Certificate2Collection certificateCollection = myStore.Certificates.Find(X509FindType.FindBySubjectName, subjectName, true);
            X509Certificate2 myCertificate;
            if (certificateCollection.Count > 0)
            {
                myCertificate = certificateCollection[0];
            }
            else
            {
                X509Certificate2[] array = new X509Certificate2[myStore.Certificates.Count];
                myStore.Certificates.CopyTo(array, 0);
                myCertificate = array.FirstOrDefault(x => x.FriendlyName.Equals(subjectName));
            }
            if (myStore != null)
                myStore.Close();
            return myCertificate;
        }

posted on   dz45693  阅读(4549)  评论(1编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2012-07-05 web优化之-js 异步加载 js延迟执行 js插件

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示