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 ...
随机推荐
- Java通过腾讯邮箱发送邮件
private static void sendEmaill() { Properties prop = new Properties(); //协议 prop.setProperty("m ...
- 差分约束详解&&洛谷SCOI2011糖果题解
差分约束系统: 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统(system of difference const ...
- (十一)zabbix监控mysql
1)配置脚本获取mysql的各种参数 注意:脚本中定义host #vim /etc/zabbix/zabbix_agentd.d/mysql_status.sh #!/bin/bash MySQlBi ...
- Python3之文件读写操作详解
文件操作的步骤: 打开文件 -> 操作文件 -> 关闭文件 切记:最后要关闭文件(否则可能会有意想不到的结果) 打开文件 文件句柄 = open('文件路径', '模式') 指定文件编码 ...
- [Algorithm] Area of polygon
How to calculate the area of polygon. For a triangle like: We can calculate the area: function cross ...
- 题解 【NOIP2011】计算系数
[NOIP2011]计算系数 Description 给定一个多项式 (ax+by)^k ,请求出多项式展开后 x^n * y^m 项的系数. Input 共一行,包含 5 个整数,分别为 a,b,k ...
- codevs 3185-3187 队列练习x
三联水题…… 3185x 题目描述 Description 给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请输出最终的队头元素. 操作解 ...
- TensorFlow使用记录 (八): 梯度修剪 和 Max-Norm Regularization
梯度修剪 梯度修剪主要避免训练梯度爆炸的问题,一般来说使用了 Batch Normalization 就不必要使用梯度修剪了,但还是有必要理解下实现的 In TensorFlow, the optim ...
- 无法连接虚拟设备 ide1:0
问题: 启动vmware之后,发现出现无法连接 ide 1:0. 网络查找之后,发现是之前挂载的iso镜像找不到了. 原因: 我把iso镜像放到其他位置. 解决: 指定iso文件的位置. 参考:htt ...
- eclipse外部导入Javaweb项目时,项目上出现红叉的一个可能的解决办法
解决办法:http://blog.csdn.net/qq_32671287/article/details/52467885 进入项目包下的.settings目录,找到org.eclipse.wst. ...