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, , 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, , toEncryptArray.Length); return Convert.ToBase64String(resultArray, , 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(, '')); 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, , 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, , 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 = "";
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);
}
}
}
EncryptHelper加密对象-工具类的更多相关文章
- 对象工具类 - ObjectUtils.java
对象工具类,提供对象克隆.获取对象属性.类型判断.Map转换对象.对象转Map.设置对象属性等. 源码如下:(点击下载 - ObjectUtils.java .JsonUtils.java .gso ...
- Base64加密解密工具类
使用Apache commons codec类Base64进行加密解密 maven依赖 <dependency> <groupId>commons-codec</grou ...
- java MD5加密的工具类
import java.security.MessageDigest; /** * MD5加密工具类 * @author zwq */ public class MD5Util { /** * MD5 ...
- AES加密解密工具类封装(AESUtil)
package club.codeapes.common.utils; import org.springframework.util.Base64Utils; import javax.crypto ...
- 加密解密工具类(Java,DES)
一个Java版的DES加密工具类,能够用来进行网络传输数据加密,保存password的时候进行加密. import java.security.Key; import java.security.sp ...
- 获取Spring容器Bean对象工具类
在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅. ...
- 019-MD5加密模板工具类
1 MD5加密工具类 package ${enclosing_package}; import java.math.BigInteger; import java.security.MessageDi ...
- Java 序列化对象工具类
SerializationUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.Byt ...
- 根据值获取枚举类对象工具类EnumUtils
枚举类 public enum Sex { man("M","男"),woman("W","女"); private S ...
随机推荐
- python+Appium自动化:记录遇到的坑
1.打开 uiautomatorviewer同步的的时候突然报错 Error while obtaining UI hierarchy XML file: com.android.ddmlib.Syn ...
- 观察数组 -vue
1.vue中的被包装的观察数组能够触发视图更新 2.有push(),pop(),shift(),unshift(),splice(),sort(),reverse() 3.不能检测到下面数组变化: 1 ...
- Python之windows锁屏
简单粗暴,三行代码搞定 from ctypes import * user32 = windll.LoadLibrary('user32.dll') user32.LockWorkStation() ...
- Python之multiprocessing模块的使用
作用:Python多进程处理模块,解决threading模块不能使用多个CPU内核,避免Python GIL(全局解释器)带来的计算瓶颈. 1.开启多进程的简单示例,处理函数无带参数 #!/usr/b ...
- npoi 导出
npoi 导出 public void Output(DataTable table, string SheetName, string reportName) { string result = s ...
- BZOJ 4004: [JLOI2015]装备购买 高斯消元解线性基
BZOJ严重卡精,要加 $long$ $double$ 才能过. 题意:求权和最小的极大线性无关组. 之前那个方法解的线性基都是基于二进制拆位的,这次不行,现在要求一个适用范围更广的方法. 考虑贪心 ...
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- echarts的渲染问题
问题:柱状图的图例和柱状图颜色在IE10和IE11中拖动时间轴的时候图例和柱状图颜色会发生改变 echarts的渲染是从setOption中开始的,不是从echarts.init()开始 initPa ...
- CentOS7 安装JumpServer
环境: CentOS Linux release 7.6.1810 (Core) JumpServer 1.4.8 Python 3.6.X MariaDB 编译安装Python3.6 首先,下载Py ...
- php " ",0,'0',false ==判断
今天项目中遇到的一个问题,举个栗子: if($_GET['is_has_idcard']==0 || $_GET['is_has_idcard']==1){ echo '这次我要上传身份证'; } i ...