C# 常用字符串加密解密方法
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Security;
namespace HuaTong.General.Utility
{
/// <summary>
/// 加密工具类
/// </summary>
public
class EncryptHelper
{
//默认密钥
private
static
string AESKey =
"[45/*YUIdse..e;]"
;
private
static
string DESKey =
"[&HdN72]"
;
/// <summary>
/// AES加密
/// </summary>
public
static
string AESEncrypt(string value, string _aeskey =
null
)
{
if (string.IsNullOrEmpty(_aeskey))
{
_aeskey = AESKey;
}
byte[] keyArray = Encoding.UTF8.GetBytes(_aeskey);
byte[] toEncryptArray = Encoding.UTF8.GetBytes(value);
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>
/// AES解密
/// </summary>
public
static
string AESDecrypt(string value, string _aeskey =
null
)
{
try
{
if (string.IsNullOrEmpty(_aeskey))
{
_aeskey = AESKey;
}
byte[] keyArray = Encoding.UTF8.GetBytes(_aeskey);
byte[] toEncryptArray =
Convert
.FromBase64String(value);
RijndaelManaged rDel = new RijndaelManaged();
rDel.
Key
= keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return
Encoding.UTF8.GetString(resultArray);
}
catch
{
return
string.Empty;
}
}
/// <summary>
/// DES加密
/// </summary>
public
static
string DESEncrypt(string value, string _deskey =
null
)
{
if (string.IsNullOrEmpty(_deskey))
{
_deskey = DESKey;
}
byte[] keyArray = Encoding.UTF8.GetBytes(_deskey);
byte[] toEncryptArray = Encoding.UTF8.GetBytes(value);
DESCryptoServiceProvider rDel = new DESCryptoServiceProvider();
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>
/// DES解密
/// </summary>
public
static
string DESDecrypt(string value, string _deskey =
null
)
{
try
{
if (string.IsNullOrEmpty(_deskey))
{
_deskey = DESKey;
}
byte[] keyArray = Encoding.UTF8.GetBytes(_deskey);
byte[] toEncryptArray =
Convert
.FromBase64String(value);
DESCryptoServiceProvider rDel = new DESCryptoServiceProvider();
rDel.
Key
= keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return
Encoding.UTF8.GetString(resultArray);
}
catch
{
return
string.Empty;
}
}
public
static
string MD5(string value)
{
byte[] result = Encoding.UTF8.GetBytes(value);
MD5 md5 = new MD5CryptoServiceProvider();
byte[]
output
= md5.ComputeHash(result);
return
BitConverter.ToString(
output
).
Replace
(
"-"
,
""
);
}
public
static
string HMACMD5(string value, string hmacKey)
{
HMACSHA1 hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(hmacKey));
byte[] result = System.Text.Encoding.UTF8.GetBytes(value);
byte[]
output
= hmacsha1.ComputeHash(result);
return
BitConverter.ToString(
output
).
Replace
(
"-"
,
""
);
}
/// <summary>
/// base64编码
/// </summary>
/// <
returns
></
returns
>
public
static
string Base64Encode(string value)
{
string result =
Convert
.ToBase64String(Encoding.
Default
.GetBytes(value));
return
result;
}
/// <summary>
/// base64解码
/// </summary>
/// <
returns
></
returns
>
public
static
string Base64Decode(string value)
{
string result = Encoding.
Default
.GetString(
Convert
.FromBase64String(value));
return
result;
}
}
}
C# 常用字符串加密解密方法的更多相关文章
- C#开发中常用的加密解密方法
转载自:https://www.cnblogs.com/bj981/p/11203711.html C#开发中常用的加密解密方法 相信很多人在开发过程中经常会遇到需要对一些重要的信息进行加密处理,今天 ...
- C#常用字符串加解密方法封装
C#中常用的字符串加密.解密方法封装,包含只加密但不解密的方法.收藏起来备用. //方法一 //须添加对System.Web的引用 //using System.Web.Security; /// & ...
- C# 字符串加密解密方法
这个是加密的算法的命名空间,使用加密算法前要引用该程序集 System.Security.Cryptography using System;using System.Data;using Syst ...
- 从网上整理的一些delphi字符串加密解密方法
function Encode(Str: string): string; var //加密 TmpChr: AnsiChar; i, Len: integer; begin Result := St ...
- java字符串加密解密
java字符串加密解密 字符串加密解密的方式很多,每一种加密有着相对的解密方法.下面要说的是java中模拟php的pack和unpack的字符串加密解密方法. java模拟php中pack: /** ...
- ASP.NET常用加密解密方法
ASP.NET常用加密解密方法 一.MD5加密解密 1.加密 C# 代码 public static string ToMd5(string clearString) ...
- 2019-2-20C#开发中常用加密解密方法解析
C#开发中常用加密解密方法解析 一.MD5加密算法 我想这是大家都常听过的算法,可能也用的比较多.那么什么是MD5算法呢?MD5全称是 message-digest algorithm 5[|ˈmes ...
- ASP.NET(C#)常用数据加密和解密方法汇总
一. 数据加密的概念 1. 基本概念 2. 基本功能 3. 加密形式 二. 数据加密的项目应用和学习 1. 媒体加密:DRM 2. 文件加密:文本 ...
- ios常见加密解密方法
在其他平台中经常会计算MD5值,在iOS平台中也提供了该方法,首先需要导入头文件 #import <CommonCrypto/CommonDigest.h> 方法CC_MD5可以获取MD5 ...
随机推荐
- IA32 MMU paging初始化代码
写了一段IA32 paging通用构造代码.有须要的.能够拿去 #define PDE_FLG_RW (1<<1) #define PDE_FLG_US (1<<2) #def ...
- solr实战-(一)
实现用户数据索引及查询 1. 启动solr solr start 2. 创建collection solr create -c user 3. schema中加入field ...
- PbootCMS V1.1.4 正式发布
PbootCMS V1.1.4 正式发布 PbootCMS V1.1.4 build 2018-06-251.修复自定义表单表名重复仍然添加成功问题:2.修复分享到微信导致页面错误的问题:3.修复静态 ...
- csu 1030: 素数槽
素数槽 Description 处于相邻的两个素数p和p + n之间的n - 1个连续的合数所组成的序列我们将其称为长度为n的素数槽.比如,‹24, 25, 26, 27, 28›是处于素数23 ...
- CSS艺术之---负margin之美
CSS中负边距(nagative margin)是布局中常常使用的一个技巧.仅仅要运用得当时常会产生奇异的效果.勘称CSS中的奇淫巧计,非常多CSS布局方法都依赖于负边距.掌握它对于前端童鞋来说还是非 ...
- element-UI中table表格的@row-click事件和@selection-change耦合了
<el-table ref="multipleTable" :data="tableData" tooltip-effect="dark&quo ...
- oc7--内存分析
// // main.m // 第二个OC类 #import <Foundation/Foundation.h> @interface Person : NSObject { @publi ...
- h5-10 canvas 简易祖玛
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Mysql数据库性能
Mysql数据库设计规范 https://www.cnblogs.com/Luke-Me/p/8994432.html 我们在项目一开始的设计中,就要忙着考虑数据库的设计,表.字段.索引.sql等等, ...
- 【Codeforces 258E】 Devu and Flowers
[题目链接] http://codeforces.com/contest/451/problem/E [算法] 容斥原理 [代码] #include<bits/stdc++.h> usin ...