C#加密帮助类EncryptHelper

///之前这篇加密帮助类觉得不够严谨,不够强,所以特意修改,也对自己负责一点,如果对你有帮助可以看看,
///呵呵!~这个类都经过本人测试过都没问题.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using log4net;

namespace Utility
{
    /// <summary>
    /// @Author:梁继龙
    /// @Date:2012/7/30
    /// @Descripte:EncryptHelper加密帮助类.
    /// </summary>
    public class EncryptHelper
    {
        /// <summary>
        ///方法一:
        ///此种加密之后的字符串是三十二位的(字母加数据)字符串
        /// Example: password是admin 加密变成后21232f297a57a5a743894a0e4a801fc3
        /// </summary>
        /// <param name="beforeStr"></param>
        /// <returns></returns>
        public string MD5Encrypt(string beforeStr)
        {
            string afterString = "";
            try
            {
                MD5 md5 = MD5.Create();
                byte[] hashs = md5.ComputeHash(Encoding.UTF8.GetBytes(beforeStr));

                foreach (byte by in hashs)
                    //这里是字母加上数据进行加密.//3y 可以,y3不可以或 x3j等应该是超过32位不可以
                    afterString += by.ToString("x2");
            }
            catch (Exception ex)
            {
                ILog log = log4net.LogManager.GetLogger(this.GetType());
                log.Error("==============你引起了一个错误是==============" + ex.Message.ToString());
            }
            return afterString;
        }

        /// <summary>
        /// 方法二
        /// HashAlgorithm加密
        /// 这种加密是  字母加-加字符
        /// Example: password是admin 加密变成后19-A2-85-41-44-B6-3A-8F-76-17-A6-F2-25-01-9B-12
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public String HashEncrypt(string password)
        {
            Byte[] hashedBytes = null;
            try
            {
                Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
                hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
            }
            catch (Exception ex)
            {
                ILog log = log4net.LogManager.GetLogger(this.GetType());
                log.Error("==============你引起了一个错误是==============" + ex.Message.ToString());
            }
            return BitConverter.ToString(hashedBytes);//MD5加密
        }

        /// <summary>
        /// 方法三:
        /// MD5  +   HashCode加密
        /// Example: password是admin 加密变成后 895b7da64943134be17b825ce118456c
        /// </summary>
        /// <returns></returns>
        public String MD5HashCodeEncrypt(string EncryptPwd)
        {
            return MD5Encrypt(HashEncrypt(EncryptPwd)); //在HashEncrypt基础上再MD5
        }

        /// <summary>
        /// 方法四:
        /// HashCode +MD5 加密
        /// Example: password是admin 加密变成后EB-1D-6D-E2-FC-F1-CD-94-4D-75-78-E6-3D-7A-12-32
        /// </summary>
        /// <param name="EncryptPwd"></param>
        /// <returns></returns>
        public String HashCodeMD5Encrypt(string EncryptPwd)
        {
            return HashEncrypt(MD5Encrypt(EncryptPwd)); //在MD5基础再HashCode
        }
        /// <summary>
        /// 方法五
        /// </summary>
        /// <param name="EncryptPwd"></param>
        /// <returns></returns>

        public String HashMD5Encrypt(string EncryptPwd)
        {
            return HashCodeMD5Encrypt(HashCodeMD5Encrypt(EncryptPwd)); //在HashCodeMD5Encrypt基础再HashCode
        }
        /// <summary>
        /// 方法六
        /// 哈哈是不是有点晕呢?
        /// 大家伙可以继续写.
        /// </summary>
        /// <param name="EncryptPwd"></param>
        /// <returns></returns>
        public String MD5HashEncrypt(string EncryptPwd)
        {
            return MD5HashCodeEncrypt(MD5HashCodeEncrypt(EncryptPwd)); //在MD5基础再HashCode
        }
        /// <summary>
        /// 64位双重MD5小写
        /// </summary>
        /// <returns></returns>
        public static string Last64(string s)
        {
            if (s.Length != 32)
                return "";
            string s1 = s.Substring(0, 16);
            string s2 = s.Substring(16, 16);
            return Lower32(s1) + Lower32(s2);
        }
        /// <summary>
        /// 32位大写
        /// </summary>
        /// <returns></returns>
        public static string Upper32(string s)
        {
            s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
            return s.ToUpper();
        }
        /// <summary>
        /// 32位小写
        /// </summary>
        /// <returns></returns>
        public static string Lower32(string s)
        {
            s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
            return s.ToLower();
        }
        /// <summary>
        /// 16位大写
        /// </summary>
        /// <returns></returns>
        public static string Upper16(string s)
        {
            s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
            return s.ToUpper().Substring(8, 16);
        }
        /// <summary>
        /// 16位小写
        /// </summary>
        /// <returns></returns>
        public static string Lower16(string s)
        {
            s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
            return s.ToLower().Substring(8, 16);
        }
    }

}

时间: 2024-11-07 10:56:31

C#加密帮助类EncryptHelper的相关文章

EncryptHelper加密帮助类

///之前这篇加密帮助类觉得不够严谨,不够强,所以特意修改,也对自己负责一点,如果对你有帮助可以看看, ///呵呵!~这个类都经过本人测试过都没问题. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Security.Cryptography;

Java AES 加密工具类

package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import jav

[C#] 常用工具类——加密解密类

using System; using System.Configuration; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using S

MD5加密帮助类

using System; using System.Collections.Generic; using System.Text; namespace AIMSCommon { /// <summary> /// MD5加密帮助类 /// </summary> public class Md5Helper { #region "MD5加密" /// <summary> /// MD5加密 /// </summary> /// <p

MD5加密实现类不是Windows平台下联邦信息处理标准验证过的加密算法的一部分

在.NET应用程序中,MD5CryptoServiceProvider实例化时,造成This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms异常.意思是MD5CryptoServiceProvider这个加密实现类不是Windows平台下联邦信息处理标准验证过的加密算法的一部分. 在大多机器上都没有遇到这个问题,今天在一台机器上突然遇到. 这个问题的解决办法是,修

android开发MD5加密工具类(一)

MD5加密工具类整理: 1 package com.gzcivil.utils; 2 3 import java.io.UnsupportedEncodingException; 4 import java.security.MessageDigest; 5 import java.security.NoSuchAlgorithmException; 6 7 public class MD5Tool { 8 9 public static String md5(String string) {

DES加密/解密类。

/// <summary> /// DES加密/解密类. /// </summary> public class DESEncrypt { #region ========加密======== /// <summary> /// 加密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public s

加密工具类 - CryptoUtils.java

加密工具类,包含MD5,BASE64,SHA,CRC32的加密与解密方法. 源码如下:(点击下载  - CryptoUtils.java.commons-io-2.4.jar.commons-codec-1.9.jar ) import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java

android加密工具类

import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * 加密工具类 * Created by Administrator on 2015/10/21 0021. */ public class EncryptUtils { /** * 字符串加密使用MD5算法 */ public final static String encryptMD5(String source) {