EncryptHelper加密对象-工具类

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace Common.Utility
{
    /// <summary>
    /// Author:Kt
    /// Date Created:2011-04-01
    /// Description:加密对象-工具类
    /// </summary>
    public class EncryptHelper
    {
        /// <summary>
        /// AES 解密
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="secretKey">私钥(长度: 16、24、32字节)</param>
        /// <returns></returns>
        public static string AESDecrypt(string content, string secretKey)
        {
            if (string.IsNullOrEmpty(content)) return null;
            Byte[] toEncryptArray = Convert.FromBase64String(content);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(secretKey),
                Mode = System.Security.Cryptography.CipherMode.ECB,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }

        /// <summary>
        /// AES 加密
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="secretKey">私钥(长度: 16、24、32字节)</param>
        /// <returns></returns>
        public static string AESEncrypt(string content, string secretKey)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(secretKey);
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(content);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public static string Md5(string content)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(content, "MD5");
        }

        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="encode">编码</param>
        /// <returns>签名字符串</returns>
        public static string Md5(string content, Encoding encode)
        {
            var result = new StringBuilder();
            var md5 = new MD5CryptoServiceProvider();
            var bytes = md5.ComputeHash(encode.GetBytes(content));
            foreach (var item in bytes)
                result.Append(item.ToString("x").PadLeft(2, ‘0‘));

            return result.ToString();
        }

        /// <summary>
        /// 3DES 解密
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="secretKey">私钥</param>
        /// <param name="ivs">向量</param>
        /// <returns></returns>
        public static string DESDecrypt(string content, string secretKey, string ivs)
        {
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
            mCSP.Mode = CipherMode.CBC;
            mCSP.Padding = PaddingMode.PKCS7;
            mCSP.Key = Encoding.UTF8.GetBytes(secretKey);
            mCSP.IV = Encoding.UTF8.GetBytes(ivs);
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
            byt = Convert.FromBase64String(content);
            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return Encoding.UTF8.GetString(ms.ToArray());
        }

        /// <summary>
        /// 3DES 加密
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="secretKey">私钥</param>
        /// <param name="ivs">向量</param>
        /// <returns></returns>
        public static string DESEncrypt(string content, string secretKey, string ivs)
        {
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
            mCSP.Mode = CipherMode.CBC;
            mCSP.Padding = PaddingMode.PKCS7;
            mCSP.Key = Encoding.UTF8.GetBytes(secretKey);
            mCSP.IV = Encoding.UTF8.GetBytes(ivs);
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
            byt = Encoding.UTF8.GetBytes(content);
            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return Convert.ToBase64String(ms.ToArray());
        }
    }
}
using System;
using System.Text;

namespace Common.Utility.Tests
{
    /// <summary>
    /// 运行示例
    /// </summary>
    internal partial class Program
    {
        private static void Main_EncryptHelper(string[] args)
        {
            string str = "需要加密的字符串12345678";
            string md5_1 = EncryptHelper.Md5(str);
            string md5_2 = EncryptHelper.Md5(str, Encoding.UTF8);

            str = "10086";
            string secretKey_AES = Guid.NewGuid().ToString("N");
            string JiaMi = EncryptHelper.AESEncrypt(str, secretKey_AES);
            string JieMi = EncryptHelper.AESDecrypt(JiaMi, secretKey_AES);

            string secretKey_DES = "fsT7ObM1nEnrRAGO1djI2YBi";
            string ivs_DES = "GUGlYE1g";
            JiaMi = EncryptHelper.DESEncrypt(str, secretKey_DES, ivs_DES);
            JieMi = EncryptHelper.DESDecrypt(JiaMi, secretKey_DES, ivs_DES);
        }
    }
}

原文地址:https://www.cnblogs.com/Jeely/p/11718995.html

时间: 2024-10-06 20:32:44

EncryptHelper加密对象-工具类的相关文章

对象工具类 - ObjectUtils.java

对象工具类,提供对象克隆.获取对象属性.类型判断.Map转换对象.对象转Map.设置对象属性等. 源码如下:(点击下载 -  ObjectUtils.java .JsonUtils.java .gson-2.2.4.jar .commons-lang-2.6.jar) 1 import java.io.Serializable; 2 import java.lang.reflect.Field; 3 import java.lang.reflect.Method; 4 import java.u

加密解密工具类(Java,DES)

一个Java版的DES加密工具类,可以用来进行网络数据传输加密,保存密码的时候进行加密. import java.security.Key; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.I

AES加密解密工具类封装(AESUtil)

import org.springframework.util.Base64Utils; 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.KeyGenerat

Spring获取ApplicationContext对象工具类

(1)实现的工具类: package com.util; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; final public class ApplicationContextUtil { private static ApplicationContext ac=null; priv

获取Spring容器Bean对象工具类

在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅.废话不多说,直接上代码. 一.代码 package com.zxy.demo.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext;

本地加密解密工具类

import java.security.Key; import javax.crypto.Cipher; public class EncryptDecodeUtil { /** * 字符串默认键值 */ private static String strDefaultKey = "national"; /** * 加密工具 */ private Cipher encryptCipher = null; /** * 解密工具 */ private Cipher decryptCiph

自写AES加密解密工具类

此类主要用于加密与解密,采用128位ECB模式,PKCS5Padding填充补位. 可使用方法为加密返回二进制encryptBin(content, key).加密返回十六进制encryptHex(content, key).二进制内容解密decryptBin(content, key).十六进制内容解密decryptHex(content, key). content是需要加密的字符串,key是密钥,随意一个字符串. 1 package com.test; 2 3 import java.io

DES加密解密工具类

import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto

MD5加密Java工具类

原文:http://www.open-open.com/code/view/1421764946296 import java.security.MessageDigest; public class MD5 { //公盐 private static final String PUBLIC_SALT = "demo" ; //十六进制下数字到字符的映射数组 private final static String[] hexDigits = {"0", "