zoukankan      html  css  js  c++  java
  • ORM映射框架总结加密处理

    1.MD5 加密处理

    代码
     1 /**
     2  * 日期:2009-3-15
     3  * 作者:
     4  * 功能:MD5加密及验证
     5  * */
     6 using System;
     7 using System.Collections.Generic;
     8 using System.Linq;
     9 using System.Text;
    10 using System.Security.Cryptography;
    11 using System.IO;
    12 
    13 namespace CommonData.Encrypt
    14 {
    15     public partial class EncryptMD5
    16     {
    17         /// <summary>
    18         /// MD5 加密
    19         /// </summary>
    20         /// <param name="content">加密的内容</param>
    21         /// <returns></returns>
    22         public static string MD5Encrypt(string content)
    23         {
    24             if (string.IsNullOrEmpty(content) == false)
    25             {
    26                 MD5 md5 = MD5.Create();
    27                 string result = "";
    28                 byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(content));
    29                 for (int i = 0; i < data.Length; i++)
    30                 {
    31                     result += data[i].ToString("x2");
    32                 }
    33                 return result;
    34             }
    35             else
    36             {
    37                 Console.WriteLine("您加密的内容为空!");
    38                 return "";
    39             }
    40 
    41         }
    42 
    43 
    44         /// <summary>
    45         /// 加密验证
    46         /// </summary>
    47         /// <param name="content">输入需要验证的内容</param>
    48         /// <param name="data">加密比较的对象</param>
    49         /// <returns></returns>
    50         public static bool GetContentCheck(string content, string data)
    51         {
    52             if (!string.IsNullOrEmpty(content) && !string.IsNullOrEmpty(data))
    53             {
    54                 string hashContent = MD5Encrypt(content);
    55                 if (hashContent.Equals(data))
    56                 {
    57                     return true;
    58                 }
    59                 else
    60                 {
    61                     return false;
    62                 }
    63             }
    64             else
    65             {
    66                 Console.WriteLine("请提供有效的验证信息!");
    67                 return false;
    68             }
    69         }
    70     }
    71 }
    72 

      这个类提供了两个方法。上面的那个是加密,下面的验证。因为MD5加密时不可逆的过程,因此在验证的时候需要先加密在比较

      如果需要了解更多md5加密可以到msdn 上去看看,介绍的比较详细

    2.XOR 加密

    代码
     1 /**
     2  * 日期:2009-3-15
     3  * 作者:
     4  * 功能:XOR加密
     5  * */
     6 using System;
     7 using System.Collections.Generic;
     8 using System.Linq;
     9 using System.Text;
    10 using System.Security.Cryptography;
    11 
    12 namespace CommonData.Encrypt
    13 {
    14     public partial class EncryptXOR
    15     {
    16         /// <summary>
    17         /// XOR加密
    18         /// </summary>
    19         /// <param name="content">加密的文本</param>
    20         /// <param name="password">加密文本密匙</param>
    21         /// <returns></returns>
    22         public static string EncryptString(string content, string password)
    23         {
    24             byte[] bcontent = (new UnicodeEncoding()).GetBytes(content);
    25             byte[] bpassword = (new UnicodeEncoding()).GetBytes(password);
    26 
    27             for (int i = 0; i < bcontent.Length; i += 2)
    28             {
    29                 for (int j = 0; j < bpassword.Length; j += 2)
    30                 {
    31                     bcontent[i] = Convert.ToByte(bcontent[i] ^ bpassword[j]);
    32                 }
    33             }
    34             return (new UnicodeEncoding()).GetString(bcontent).TrimEnd('\0');
    35         }
    36 
    37 
    38         /// <summary>
    39         /// XOR 解密
    40         /// </summary>
    41         /// <param name="content">解密的文本</param>
    42         /// <param name="key">解密文本密匙</param>
    43         /// <returns></returns>
    44         public static string DecryptString(string content, string key)
    45         {
    46             return EncryptString(content, key);
    47         }
    48     }
    49 }
    50 

      这个加密算法是可逆的,不过在解密的时候需要密钥处理.这个加密不难,算法也比较简单

  • 相关阅读:
    Servlet-RequestDispatcher.forward方法
    Servlet---RequestDispatcher.include方法
    解决用了vertical-align: middle,但是文字图片依旧不居中的问题
    vue项目中rem适配问题
    微信小程序之使用ecarts---详细步骤
    微信公众号之input输入框获取焦点后,底部固定定位的按钮顶起问题解决办法
    微信公众号开发之解决IOS点击input 、textarea页面错位的问题
    微信小程序用navigationStyle自定义导航栏做法
    微信小程序 mpvue + picker
    PyCharm怎么整理(格式化)当前代码
  • 原文地址:https://www.cnblogs.com/qingyuan/p/1637837.html
Copyright © 2011-2022 走看看