• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# 加密术

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

 

本文是利用一个简单的小例子,简述C#中和加密术有关的内容,仅供学习参考用。

概述

随着信息技术的发展,计算机网络为信息的获取、传输、处理、利用与共享提供了一个高效、快捷、安全的通信环境和传输通道,网络信息安全也变得越来越重要。信息安全主要包括两个方面的内容:信息存储安全和信息传输安全。保证网络中信息安全的主要技术是数据的加密与解密。如下图示,说明了加密与解密的过程。

 

 

 

公式算法表示如下:

加密公式:c=Eke(m)  (11.1) 解密公式:m=Dkd(c)  (11.2)其中m代表明文,c代表密文,E是加密算法,D是解密算法,参数ke称为加密密钥,参数kd称为解密密钥

涉及知识点

  • HASH算法 :散列函数是现代密码系统的基础。这些函数将任意长度的二进制字符串映射为固定长度的二进制字符串(称为散列值)
  • 对称加密算法:指对信息的加密和解密都使用相同的密钥。
  • 非对称加密算法:非对称加密算法对信息的加密与解密使用不同的密钥,用来加密的密钥是可以公开的,用来解密的密钥需要保密
  • 数字签名术:数字签名技术结合散列算法和非对称加密技术来进行篡改检测和解决相关的身份验证问题。这就像在现实生活中用亲笔签名来保证文件或资料的真实性一样。

 散列算法

散列算法主要有MD5【Message Digest Algorithm】,RIPEMD160【RACE Integrity Primitives Evaluation Message Digest】和SHA【Secure Hash Algorithm】,SHA又分为SHA1,SHA256,SHA384,SHA512,【散列算法不可逆,是单向操作】如下如所示:

 

 

 

关于散列算法,效果图如下:

 

 

 

HASH算法核心代码如下:

 
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Security.Cryptography;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace DemoCrtpto
  9 {
 10     /// <summary>
 11     /// 信息摘要帮助类
 12     /// </summary>
 13     public class HashHelper
 14     {
 15         #region 信息摘要
 16 
 17         /// <summary>
 18         /// MD5信息摘要
 19         /// </summary>
 20         /// <param name="source"></param>
 21         /// <returns></returns>
 22         public static string GetInfoByMd5(string source)
 23         {
 24             HashAlgorithm hash = getHashAlgorithm(HashType.MD5);
 25             return Encrypto(hash, source);
 26         }
 27 
 28         public static string GetInfoBySHA1(string source)
 29         {
 30             HashAlgorithm hash = getHashAlgorithm(HashType.SHA1);
 31             return Encrypto(hash, source);
 32         }
 33 
 34         public static string GetInfoBySHA256(string source)
 35         {
 36             HashAlgorithm hash = getHashAlgorithm(HashType.SHA256);
 37             return Encrypto(hash, source);
 38         }
 39 
 40         public static string GetInfoBySHA384(string source)
 41         {
 42             HashAlgorithm hash = getHashAlgorithm(HashType.SHA384);
 43             return Encrypto(hash, source);
 44         }
 45 
 46         public static string GetInfoBySHA512(string source)
 47         {
 48             HashAlgorithm hash = getHashAlgorithm(HashType.SHA512);
 49             return Encrypto(hash, source);
 50         }
 51 
 52         public static string GetInfoByRipeMD(string source)
 53         {
 54             HashAlgorithm hash = getHashAlgorithm(HashType.RIPEMD);
 55             return Encrypto(hash, source);
 56         }
 57 
 58         #endregion
 59 
 60         /// <summary>
 61         /// 根据类型获取摘要算法名称
 62         /// </summary>
 63         /// <param name="t"></param>
 64         /// <returns></returns>
 65         private static HashAlgorithm getHashAlgorithm(HashType t) {
 66             HashAlgorithm hash;
 67             switch (t) {
 68                 case HashType.MD5:
 69                     hash = new MD5CryptoServiceProvider();
 70                     break;
 71                 case HashType.SHA1:
 72                     hash = new SHA1Managed();
 73                     break;
 74                 case HashType.SHA256:
 75                     hash = new SHA256Managed();
 76                     break;
 77                 case HashType.SHA384:
 78                     hash = new SHA384Managed();
 79                     break;
 80                 case HashType.SHA512:
 81                     hash = new SHA512Managed();
 82                     break;
 83                 case HashType.RIPEMD:
 84                     hash = new RIPEMD160Managed();
 85                     break;
 86                 default:
 87                     hash = new MD5CryptoServiceProvider();
 88                     break;
 89             }
 90             return hash;
 91         }
 92 
 93         /// <summary>
 94         /// 加密
 95         /// </summary>
 96         /// <param name="hash"></param>
 97         /// <param name="source"></param>
 98         /// <returns></returns>
 99         private static string Encrypto(HashAlgorithm hash, string source) {
100             string dest = string.Empty;
101             try
102             {
103                 byte[] btSource = Encoding.Default.GetBytes(source);
104                 byte[] btDest = hash.ComputeHash(btSource);
105                 dest = Convert.ToBase64String(btDest, 0, btDest.Length);
106             }
107             catch (Exception ex)
108             {
109                 throw ex;
110             }
111             return dest;
112         }
113     }
114 
115     /// <summary>
116     /// 信息摘要类型
117     /// </summary>
118     public enum HashType {
119         MD5=0,
120         SHA1=1,
121         SHA256=2,
122         SHA384=3,
123         SHA512=4,
124         RIPEMD=5
125     }
126 }
 
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Security.Cryptography;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace DemoCrtpto
  9 {
 10     /// <summary>
 11     /// 信息摘要帮助类
 12     /// </summary>
 13     public class HashHelper
 14     {
 15         #region 信息摘要
 16 
 17         /// <summary>
 18         /// MD5信息摘要
 19         /// </summary>
 20         /// <param name="source"></param>
 21         /// <returns></returns>
 22         public static string GetInfoByMd5(string source)
 23         {
 24             HashAlgorithm hash = getHashAlgorithm(HashType.MD5);
 25             return Encrypto(hash, source);
 26         }
 27 
 28         public static string GetInfoBySHA1(string source)
 29         {
 30             HashAlgorithm hash = getHashAlgorithm(HashType.SHA1);
 31             return Encrypto(hash, source);
 32         }
 33 
 34         public static string GetInfoBySHA256(string source)
 35         {
 36             HashAlgorithm hash = getHashAlgorithm(HashType.SHA256);
 37             return Encrypto(hash, source);
 38         }
 39 
 40         public static string GetInfoBySHA384(string source)
 41         {
 42             HashAlgorithm hash = getHashAlgorithm(HashType.SHA384);
 43             return Encrypto(hash, source);
 44         }
 45 
 46         public static string GetInfoBySHA512(string source)
 47         {
 48             HashAlgorithm hash = getHashAlgorithm(HashType.SHA512);
 49             return Encrypto(hash, source);
 50         }
 51 
 52         public static string GetInfoByRipeMD(string source)
 53         {
 54             HashAlgorithm hash = getHashAlgorithm(HashType.RIPEMD);
 55             return Encrypto(hash, source);
 56         }
 57 
 58         #endregion
 59 
 60         /// <summary>
 61         /// 根据类型获取摘要算法名称
 62         /// </summary>
 63         /// <param name="t"></param>
 64         /// <returns></returns>
 65         private static HashAlgorithm getHashAlgorithm(HashType t) {
 66             HashAlgorithm hash;
 67             switch (t) {
 68                 case HashType.MD5:
 69                     hash = new MD5CryptoServiceProvider();
 70                     break;
 71                 case HashType.SHA1:
 72                     hash = new SHA1Managed();
 73                     break;
 74                 case HashType.SHA256:
 75                     hash = new SHA256Managed();
 76                     break;
 77                 case HashType.SHA384:
 78                     hash = new SHA384Managed();
 79                     break;
 80                 case HashType.SHA512:
 81                     hash = new SHA512Managed();
 82                     break;
 83                 case HashType.RIPEMD:
 84                     hash = new RIPEMD160Managed();
 85                     break;
 86                 default:
 87                     hash = new MD5CryptoServiceProvider();
 88                     break;
 89             }
 90             return hash;
 91 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#串口通信发布时间:2022-07-13
下一篇:
C#取真实IP地址--多个代理背后的ip地址发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap