博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]C# 常用字符串加密解密方法
阅读量:5054 次
发布时间:2019-06-12

本文共 4000 字,大约阅读时间需要 13 分钟。

 

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;
        
}
 
 
    
}
}

转载于:https://www.cnblogs.com/zhangxin4477/p/6751798.html

你可能感兴趣的文章
[数学][dp] Jzoj P4236 登山
查看>>
C++继承方式
查看>>
Page2
查看>>
FIT2096 Assignment 2 2019
查看>>
软件工程实验一 复利计算——单元测试
查看>>
Python多进程并发(multiprocessing)
查看>>
读取配置文件参数和文件路径
查看>>
2017 UESTC Training for Graph Theory
查看>>
oracle实用的sqlplus命令
查看>>
Selenium上机实验
查看>>
BZOJ1369/BZOJ2865 【后缀数组+线段树】
查看>>
微软ASP.NET站点部署指南(8):部署Code-Only更新
查看>>
FreeModbus移植实例(转)
查看>>
筛素数 [高效]
查看>>
正則表達式(轉)
查看>>
Java并发编程:线程池的使用
查看>>
Python 的xlutils模块
查看>>
springMVC笔记(四)- 不配置HandlerMapping
查看>>
解决zabbix可用性为灰色状态
查看>>
lemon详细使用方法
查看>>