MD5加密解密类

public class CrytMd5
    {
        private static string symmetricKey = "2fSA4Gd3czxzmsel5kgEs6dXj6h4vA6y";    //对称加密密钥
        private static string symmetricIV = "he86rTcIvr12CiwN";    //对称加密初始向量

/// <summary>
        /// 设置密钥,只写
        /// </summary>
        public static string Key
        {
            set
            {
                symmetricKey = value;
            }
        }

/// <summary>
        /// 设置初始向量,只写
        /// </summary>
        public static string IV
        {
            set
            {
                symmetricIV = value;

}
        }

/// <summary>
        /// 将字节数组buff进行加密,使用该方法之前需设置密钥(Key)和初始向量(IV)
        /// </summary>
        /// <param name = "buff" > 需要加密的字节数组 </ param >
        /// < returns > 返回加密后的字节数组 </ returns >
        public static string EncryptTo(string ordiStr)
        {
            string ret = ordiStr;
            try
            {
                RijndaelManaged myRijndael = new RijndaelManaged();

byte[] key = System.Text.Encoding.UTF8.GetBytes(symmetricKey);
                byte[] IV = System.Text.Encoding.UTF8.GetBytes(symmetricIV);

byte[] buff = System.Text.Encoding.UTF8.GetBytes(ordiStr);
                //取得加密器
                ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
                //加密数据
                MemoryStream msEncrypt = new MemoryStream();
                CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

//将数据写入流csEncrypt中
                csEncrypt.Write(buff, 0, buff.Length);
                csEncrypt.FlushFinalBlock();

//读取加密后的数据
                buff = msEncrypt.ToArray();
                ret = ConvertUtil.Byte2HEX(buff);

//关闭流
                csEncrypt.Close();
                msEncrypt.Close();
                return ret;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //新的MD5算法
        public static string EncryptCode(string password)
        {
            //明文密码由字符串转换为byte数组
            byte[] clearBytes = new System.Text.UnicodeEncoding().GetBytes(password);
            //由明文的byte数组计算出MD5密文byte数组
            byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);

//把byte数组转换为字符串后返回,BitConverter用于将基础数据类型与字节数组相互转换
            string Encrystr = BitConverter.ToString(hashedBytes);
            return Encrystr.Replace("-", "");
        }

/// <summary>
        /// 将字节数组buff进行解密,使用该方法之前需设置密钥(Key)和初始向量(IV)
        /// </summary>
        /// <param name="buff">需要解密的字节数组</param>
        /// <returns>返回解密后的字节数组</returns>
        public static string DecryptTo(string cryptedStr)
        {
            string ret = cryptedStr;
            try
            {
                RijndaelManaged myRijndael = new RijndaelManaged();

byte[] key = System.Text.Encoding.UTF8.GetBytes(symmetricKey);
                byte[] IV = System.Text.Encoding.UTF8.GetBytes(symmetricIV);

//取得解密器
                ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);

//解密数据
                byte[] buff = ConvertUtil.Hex2Byte(cryptedStr);
                MemoryStream msDecrypt = new MemoryStream(buff);
                CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

//读取已解密的数据
                buff = new byte[buff.Length];
                csDecrypt.Read(buff, 0, buff.Length);
                msDecrypt.Close();
                csDecrypt.Close();

//去掉解密后多余的数据(后面的0,这是加密时为了补齐块大小而填充的)
                ArrayList list = new ArrayList();
                for (int i = 0; i < buff.Length; i++)
                {
                    if (buff[i] != 0)
                    {
                        list.Add(buff[i]);
                    }
                    else
                    {
                        break;
                    }
                }
                //读取解密后的正确数据
                buff = new byte[list.Count];
                list.CopyTo(buff, 0);

//去掉解密后多余的数据(后面的0,这是加密时为了补齐块大小而填充的)
                //ret = System.Text.Encoding.UTF8.GetString(buff);                
                ret = System.Text.Encoding.UTF8.GetString(buff, 0, buff.Length);
                return ret;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static string GetMd5Str(string ConvertString)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
            t2 = t2.Replace("-", "");

t2 = t2.ToLower();

return t2;
        }

时间: 2024-10-13 01:53:54

MD5加密解密类的相关文章

MD5加密解密类(asp.net)&amp;使用MD5过时处理

加密类 #region ========加密======== /// <summary> /// 加密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Encrypt(string Text) { return Encrypt(Text, "cong"); }

MD5加密解密帮助类

using System; using System.Security.Cryptography; using System.Text; namespace Maticsoft.DBUtility { /// <summary> /// DES加密/解密类. /// </summary> public class DESEncrypt { public DESEncrypt() { } #region ========加密======== /// <summary> /

[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

DES加密/解密类。

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

C#实现基于Base64的加密解密类实例

本文实例讲述了C#实现基于Base64的加密解密类.分享给大家供大家参考.具体如下: 这个C#类是一个基于Base64的加密和解密类,用户可以可以使用默认的秘钥进行加密.解密,也可以自己设定秘钥进行加密和解密,非常实用 using System; using System.Security.Cryptography; using System.Text; namespace DotNet.Utilities { /// <summary> /// Encrypt 的摘要说明. /// <

java 采用MD5加密解密

MD5加密解密 package endecrypt; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * 采用MD5加密解密 * @author tfq * @datetime 2011-10-13 */ public class MD5Util { /*** * MD5加码 生成3

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) {