C#常用加密方法
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text; /// <summary>
/// Encrypt 的摘要说明
/// </summary>
public static class Encrypt
{
#region 解密由AES加密的字符串
/// <summary>
/// 解密由AES加密的字符串
/// </summary>
/// <param name="source">加密字符串</param>
/// <param name="key">加密密钥</param>
/// <returns>解密后的字符串</returns>
public static string Dencrypt_AES(string source, string key)
{
try
{
Rijndael rijndael = Rijndael.Create();
rijndael.Key = Encoding.Default.GetBytes(key);
rijndael.IV = Encoding.Default.GetBytes(key.Substring(0, 0x10));
byte[] buffer = Convert.FromBase64String(source);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
return Encoding.Default.GetString(stream.ToArray());
}
catch
{
return null;
}
}
#endregion #region 解密由DES加密的字符串
/// <summary>
/// 解密由DES加密的字符串
/// </summary>
/// <param name="source">加密的字符串</param>
/// <param name="key">加密的密钥</param>
/// <returns>解密后的字符串</returns>
public static string Dencrypt_DES(string source, string key)
{
try
{
DESCryptoServiceProvider provider2 = new DESCryptoServiceProvider
{
Key = Encoding.Default.GetBytes(key),
IV = Encoding.Default.GetBytes(key)
};
DESCryptoServiceProvider provider = provider2;
byte[] buffer = Convert.FromBase64String(source);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
return Encoding.Default.GetString(stream.ToArray());
}
catch
{
return null;
}
}
#endregion #region 解密由RC2加密的字符串
/// <summary>
/// 解密由RC2加密的字符串
/// </summary>
/// <param name="source">加密的字符串</param>
/// <param name="key">加密的密钥</param>
/// <returns>解密后的字符串</returns>
public static string DEncrypt_RC2(string source, string key)
{
try
{
RC2CryptoServiceProvider provider2 = new RC2CryptoServiceProvider
{
Key = Encoding.Default.GetBytes(key),
IV = Encoding.Default.GetBytes(key)
};
RC2CryptoServiceProvider provider = provider2;
byte[] buffer = Convert.FromBase64String(source);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
return Encoding.Default.GetString(stream.ToArray());
}
catch
{
return null;
}
}
#endregion #region 解密由TripleDES加密的字符串
/// <summary>
/// 解密由TripleDES加密的字符串
/// </summary>
/// <param name="source">加密的字符串</param>
/// <param name="key">加密的密钥</param>
/// <returns>解密后的字符串</returns>
public static string Dencrypt_TripleDES(string source, string key)
{
try
{
TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider
{
Key = Encoding.Default.GetBytes(key),
IV = Encoding.Default.GetBytes(key.Substring(0, 8))
};
TripleDESCryptoServiceProvider provider = provider2;
byte[] buffer = Convert.FromBase64String(source);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
return Encoding.Default.GetString(stream.ToArray());
}
catch
{
return null;
}
}
#endregion #region 使用AES方式进行加密
/// <summary>
/// 使用AES方式进行加密
/// </summary>
/// <param name="source">需要加密的字符串</param>
/// <param name="key">加密用的密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt_AES(string source, string key)
{
try
{
Rijndael rijndael = Rijndael.Create();
rijndael.Key = Encoding.Default.GetBytes(key);
rijndael.IV = Encoding.Default.GetBytes(key.Substring(0, 0x10));
byte[] bytes = Encoding.Default.GetBytes(source);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
return Convert.ToBase64String(stream.ToArray());
}
catch
{
return null;
}
}
#endregion #region 使用DES方式进行加密
/// <summary>
/// 使用DES方式进行加密
/// </summary>
/// <param name="source">需要加密的字符串</param>
/// <param name="key">加密用的密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt_DES(string source, string key)
{
try
{
DESCryptoServiceProvider provider2 = new DESCryptoServiceProvider
{
Key = Encoding.Default.GetBytes(key),
IV = Encoding.Default.GetBytes(key)
};
DESCryptoServiceProvider provider = provider2;
byte[] bytes = Encoding.Default.GetBytes(source);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
return Convert.ToBase64String(stream.ToArray());
}
catch
{
return null;
}
}
#endregion #region 使用MD5方式进行加密
/// <summary>
/// 使用MD5方式进行加密
/// </summary>
/// <param name="source">需要加密的字符串</param>
/// <param name="key">加密用的密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt_MD5(string source)
{
string str = "";
MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
byte[] bytes = Encoding.Default.GetBytes(source);
byte[] buffer2 = provider.ComputeHash(bytes);
foreach (byte num in buffer2)
{
str = str + num.ToString("X").PadLeft(2, '0');
}
return str;
}
#endregion #region 使用RC2方式进行加密
/// <summary>
/// 使用RC2方式进行加密
/// </summary>
/// <param name="source">需要加密的字符串</param>
/// <param name="key">加密用的密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt_RC2(string source, string key)
{
try
{
RC2CryptoServiceProvider provider2 = new RC2CryptoServiceProvider
{
Key = Encoding.Default.GetBytes(key),
IV = Encoding.Default.GetBytes(key)
};
RC2CryptoServiceProvider provider = provider2;
byte[] bytes = Encoding.Default.GetBytes(source);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
return Convert.ToBase64String(stream.ToArray());
}
catch
{
return null;
}
}
#endregion #region 使用TripleDES方式进行加密
/// <summary>
/// 使用TripleDES方式进行加密
/// </summary>
/// <param name="source">需要加密的字符串</param>
/// <param name="key">加密用的密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt_TripleDES(string source, string key)
{
try
{
TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider
{
Key = Encoding.Default.GetBytes(key),
IV = Encoding.Default.GetBytes(key.Substring(0, 8))
};
TripleDESCryptoServiceProvider provider = provider2;
byte[] bytes = Encoding.Default.GetBytes(source);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
return Convert.ToBase64String(stream.ToArray());
}
catch
{
return null;
}
}
#endregion #region 获取加密的密钥
/// <summary>
/// 获取加密的密钥
/// </summary>
/// <param name="EncryptType">加密方式</param>
/// <returns>密钥</returns>
public static string GetKey(EncryptType encryptType)
{
switch (encryptType)
{
case EncryptType.AES:
return "1zx2fqw3eas4dtg8hui9opl6dfh5aok0"; case EncryptType.DES:
return "zxc52mxb"; case EncryptType.TDES:
return "zxc52mxbqwdftghu24dfhji0"; case EncryptType.RC2:
return "qwedfgbd";
}
return "";
}
#endregion public enum EncryptType:byte
{
AES,
DES,
TDES,
RC2
}
}
C#常用加密方法的更多相关文章
- iOS常用加密方法(aes、md5、base64)
1.代码 iOS常用加密方法(aes.md5.base64) .AES加密 NSData+AES.h文件 // // NSData-AES.h // Smile // // Created by 周 ...
- IOS开发之----常用加密方法
本文转载至 http://blog.csdn.net/wildfireli/article/details/23191983 (AES.MD5.Base64) 分类: iPhone 2014-04-0 ...
- Java中常用的加密方法(JDK)
加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...
- ASP.NET常用加密解密方法
ASP.NET常用加密解密方法 一.MD5加密解密 1.加密 C# 代码 public static string ToMd5(string clearString) ...
- [转载] Java中常用的加密方法
转载自http://www.iteye.com/topic/1122076/ 加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的 ...
- IOS常见的加密方法,常用的MD5和Base64
iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...
- 2019-2-20C#开发中常用加密解密方法解析
C#开发中常用加密解密方法解析 一.MD5加密算法 我想这是大家都常听过的算法,可能也用的比较多.那么什么是MD5算法呢?MD5全称是 message-digest algorithm 5[|ˈmes ...
- iOS里常见的几种信息编码、加密方法简单总结
一.MD5 MD5编码是最常用的编码方法之一,是从一段字符串中通过相应特征生成一段32位的数字字母混合码. MD5主要特点是 不可逆,相同数据的MD5值肯定一样,不同数据的MD5值不一样(也不是绝对的 ...
- iOS代码加密常用加密方式
iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...
随机推荐
- 【移动端 Web】怎么循序渐进地开发一个移动端页面
1. 移动页面开发基础 1.1 像素——什么是像素 像素是 Web 页面布局的基础,那么到底什么才是一个像素呢? 像素:一个像素就是计算机屏幕所能显示一种特定颜色的最小区域.这是像素的概念,实际上,W ...
- 每天一个linux命令:vmstat
1.命令简介 vmstat(Virtual Memory Statistics 虚拟内存统计) 命令用来显示Linux系统虚拟内存状态,也可以报告关于进程.内存.I/O等系统整体运行状态. 2.用法 ...
- 几种序列化协议(protobuf,xstream,jackjson,jdk,hessian)相关数据对比
测试结果 序列化数据对比 bytes字节数对比 具体的数字: protobuf jackson xstream Serializable hessian2 hessian2压缩 hessian1 ...
- Jexus 5.4.6 on CentOS 6.6
Mono 通过脚本安装 https://github.com/cjy37/linux-asp.net-installScript 版本 3.10 MongoDB 也可通过以上脚本安装 默认端口 270 ...
- linux驱动(续)
网络通信 --> IO多路复用之select.poll.epoll详解 IO多路复用之select.poll.epoll详解 目前支持I/O多路复用的系统调用有 select,psel ...
- react-native init的时候出现问题:npm WARN React-native@0.35.0 requires a peer of react@~15.3.1 but none was
react-native init的时候出现问题:npm WARN React-native@0.35.0 requires a peer of react@~15.3.1 but none was ...
- SQL Server 2016新特性:In-Memory OLTP
存储格式修改 在2014,2016中修改了内存优化表的存储格式,新的格式是序列的并且the database is restarted once during database recovery. ...
- 根据IP查地理位置信息
IP地址库下载地址: https://www.ipip.net/product/ip.html 使用方式(Python): https://github.com/ipipdotnet/datx-pyt ...
- windows安装centos7子系统
Windows的Linux子系统(wsl ) 自从有了wsl后,基本上虚拟机都省了,对于离不开Windows的便捷,Linux对开发的友好的人来说,真是一大利器.可惜默认的系统没有centos,幸运的 ...
- JavaWeb过滤器.监听器.拦截器-原理&区别(转)
1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而过滤器则可以对几乎所有的 ...