【加密】C#.NET 各种加密解密
包括:AES/DES/Base64/RSA/MD5/SHA256
http://www.sosuo8.com/article/show.asp?id=3083
http://blog.csdn.net/llwinnner/article/details/4011936
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Data.Common;
using System.Web.Script.Serialization;
using System.IO;
using System.Security.Cryptography;
namespace Pub.Class
{
public static class EncryptExtensions
{
private static byte[] AESKeys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
public static string AESEncode(this string encryptString, string encryptKey)
{
encryptKey = encryptKey.SubString(, "");
encryptKey = encryptKey.PadRight(, ' ');
RijndaelManaged rijndaelProvider = new RijndaelManaged();
rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(, ));
rijndaelProvider.IV = AESKeys;
ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();
byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, , inputData.Length);
return Convert.ToBase64String(encryptedData);
}
public static string AESDecode(this string decryptString, string decryptKey)
{
try
{
decryptKey = decryptKey.SubString(, "");
decryptKey = decryptKey.PadRight(, ' ');
RijndaelManaged rijndaelProvider = new RijndaelManaged();
rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey);
rijndaelProvider.IV = AESKeys;
ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();
byte[] inputData = Convert.FromBase64String(decryptString);
byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, , inputData.Length);
return Encoding.UTF8.GetString(decryptedData);
}
catch { return string.Empty; }
}
private static byte[] DESKeys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
public static string DESEncode(this string encryptString, string encryptKey)
{
encryptKey = encryptKey.SubString(, "");
encryptKey = encryptKey.PadRight(, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(, ));
byte[] rgbIV = DESKeys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, , inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
public static string DESDecode(this string decryptString, string decryptKey)
{
try
{
decryptKey = decryptKey.SubString(, "");
decryptKey = decryptKey.PadRight(, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = DESKeys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, , inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch { return string.Empty; }
}
public static string Base64Encode(this string encryptString)
{
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(encryptString);
return Convert.ToBase64String(encbuff);
}
public static string Base64Decode(this string decryptString)
{
byte[] decbuff = Convert.FromBase64String(decryptString);
return System.Text.Encoding.UTF8.GetString(decbuff);
}
public static string RSADecrypt(this string s, string key)
{
string result = null;
if (string.IsNullOrEmpty(s)) throw new ArgumentException("An empty string value cannot be encrypted.");
if (string.IsNullOrEmpty(key)) throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key.");
CspParameters cspp = new CspParameters();
cspp.KeyContainerName = key;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp);
rsa.PersistKeyInCsp = true;
string[] decryptArray = s.Split(new string[] { "-" }, StringSplitOptions.None);
byte[] decryptByteArray = Array.ConvertAll<string, byte>(decryptArray, (a => Convert.ToByte(byte.Parse(a, System.Globalization.NumberStyles.HexNumber))));
byte[] bytes = rsa.Decrypt(decryptByteArray, true);
result = System.Text.UTF8Encoding.UTF8.GetString(bytes);
return result;
}
public static string RSAEncrypt(this string s, string key)
{
if (string.IsNullOrEmpty(s)) throw new ArgumentException("An empty string value cannot be encrypted.");
if (string.IsNullOrEmpty(key)) throw new ArgumentException("Cannot encrypt using an empty key. Please supply an encryption key.");
CspParameters cspp = new CspParameters();
cspp.KeyContainerName = key;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp);
rsa.PersistKeyInCsp = true;
byte[] bytes = rsa.Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(s), true);
return BitConverter.ToString(bytes);
}
public static string MD5(this string str)
{
string cl1 = str;
string pwd = "";
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
// 加密后是一个字节类型的数组
byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(cl1));
for (int i = ; i < s.Length; i++)
{
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
pwd = pwd + s[i].ToString("x");// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
}
return pwd;
}
public static string MD5CSP(this string encypStr)
{
string retStr;
MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
//创建md5对象
byte[] inputBye;
byte[] outputBye;
//使用GB2312编码方式把字符串转化为字节数组.
inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
outputBye = m5.ComputeHash(inputBye);
retStr = System.BitConverter.ToString(outputBye);
retStr = retStr.Replace("-", "").ToLower();
return retStr;
}
public static string SHA256(this string str)
{
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] Result = Sha256.ComputeHash(SHA256Data);
return Convert.ToBase64String(Result);
//返回长度为44字节的字符串
}
}
}
【加密】C#.NET 各种加密解密的更多相关文章
- JS客户端RSA加密,Java服务端解密
常用语网页客户端对密码加密,在后端java解密还原 java代码依赖 <dependency> <groupId>commons-codec</group ...
- RSA加密前端JS加密,后端asp.net解密,报异常
RSA加密前端JS加密,后端asp.net解密,报异常 参考引用:http://www.ohdave.com/rsa/的JS加密库 前端JS加密代码: function GetChangeStr() ...
- Android DES加密的CBC模式加密解密和ECB模式加密解密
DES加密共有四种模式:电子密码本模式(ECB).加密分组链接模式(CBC).加密反馈模式(CFB)和输出反馈模式(OFB). CBC模式加密: import java.security.Key; i ...
- 前端加密传输 crypto-js AES 加密和解密
配置: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 对称加密----AES和DES加密、解密
目前主流的加密方式有:(对称加密)AES.DES (非对称加密)RSA.DSA 调用AES/DES加密算法包最精要的就是下面两句话: Cipher cipher = Cipher.get ...
- AES加密解密&&SHA1、SHA加密&&MD5加密
AES加密解密 SHA1.SHA加密 MD5加密 二话不说立即附上代码: package com.luo.util; import java.io.UnsupportedEncodingExcepti ...
- Android 中 非对称(RSA)加密和对称(AES)加密
在非对称加密中使用的主要算法有:RSA.Elgamal.背包算法.Rabin.D-H.ECC(椭圆曲线加密算法)等. 优点: 非对称加密与对称加密相比,其安全性更好:对称加密的通信双方使用相同的秘钥, ...
- PHP、Java对称加密中的AES加密方法
PHP AES加密 <?php ini_set('default_charset','utf-8'); class AES{ public $iv = null; public $key = n ...
- DotNet加密方式解析--对称加密
离过年又近了一天,回家已是近在咫尺,有人欢喜有人愁,因为过几天就得经历每年一度的装逼大戏,亲戚朋友加同学的各方显摆,所以得靠一剂年终奖来装饰一个安稳的年,在这里我想起了一个题目“论装逼的技术性和重要性 ...
- DotNet加密方式解析--非对称加密
新年新气象,也希望新年可以挣大钱.不管今年年底会不会跟去年一样,满怀抱负却又壮志未酬.(不过没事,我已为各位卜上一卦,卦象显示各位都能挣钱...).已经上班两天了,公司大部分人还在休假,而我早已上班, ...
随机推荐
- QUiLoader 动态加载.ui文件
动态加载UI文件是指,用 Qt Designer 通过拖拽的方式生产.ui 文件.不用 uic工具把.ui 文件变成等价的 c++代码,而是在程序运行过程中需要用到UI文件时,用 QUiLoader ...
- SQL 编码规范
1. 必须对表起别名,方便调查表用了哪些列 比如 select owner,object_id,name from a,b where a.id=b.id; 如果不对表取别名,我怎么知道你访问的列是哪 ...
- 自定义控件(视图)1期笔记01:View 和 ViewGroup
1.View 和 ViewGroup 图解关系: 2. View 和 ViewGroup 关系和作用: (1) 关系: • 继承关系 • 组合关系 (2) 作用: • View的作用: 提供 ...
- ifndef/define/endif 的作用
转载自百度百科 ,感谢度娘 1 2 3 #ifdef语句1 //程序2 #endif 可翻译为:如果宏定义了语句1则执行程序2. 作用:我们可以用它区隔一些与特定头文件.程序库和其他文件版本有关的代码 ...
- C++的4种编程范型
基于过程procedural-based 基于对象object-based 面向对象object-oriented 泛型技术generics
- Apache配置虚拟主机后,不能访问localhost的问题
今天想试用一下php7,但是发现php7只支持Apache2.4版本,而我电脑上的Apache是2.2版本,为了想尝鲜,就必须去下载新的Apache2.4 php7和apache2.4安装整合以后,l ...
- lvm基础
一.简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写. LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬 ...
- We~ˇsay~~ˇ
拂弹每一个音符 与心相印 行走每一段风景 和路缠绵 花开的声音 只能用心倾听 无论曾经如何艰难 我依然在最初的起点 默念歌唱 等你 携手
- 通过样式调整input 中password text默认长度
原文出处 <input >标签内的type分别为password和text时其默认长度和宽度不一致,而在做登陆框时往往需要将它们的长度和宽度设置一致.如下的方法可以通过css控制使其一致: ...
- http请求之referer头与防盗链
在网页中的占用大流量的信息可以写成这个信息在网络上的url位置,这样就会减少本网站的流量,但是其他网站也 不会随意让你使用人家的资源,因为这样的情对人家的网站没有好处,会增加人家网站的流量,所以要防止 ...