c#多种加解密示例(md5加密解密)
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
///
/// 加解密
///
public static class CryptHelper
{
#region RSA加密
///
/// 用指定公钥加密文本
///
/// 公钥
/// 文本
///
public static string RsaEncrypt(string publicKey, string input)
{
const string sFormat =
"
var provider = new RSACryptoServiceProvider();
var arrPubKey = Hex2Bytes(publicKey);
var pkB64 = Convert.ToBase64String(arrPubKey);
var xmlStr = string.Format(sFormat, pkB64);
var bytesInput = Encoding.UTF8.GetBytes(input);
provider.FromXmlString(xmlStr);
var encryptArray = provider.Encrypt(bytesInput, false);
var hexStr = BitConverter.ToString(encryptArray).Replace("-", "");
return hexStr.ToLower();
}
#endregion
#region 公用函数
///
/// 将字节转换成16进制数字
///
/// 字节数组
///
public static string Bytes2Hex(byte[] bytes)
{
var buffer = new StringBuilder();
foreach (var t in bytes)
{
buffer.AppendFormat("{0:x2}", t);
}
return buffer.ToString();
}
///
/// 将16进制数组转换成字节
///
/// 16进制数组
///
///
public static byte[] Hex2Bytes(string input)
{
if (string.IsNullOrEmpty(input)) return null;
var offset = input.Length%2;
if (offset == 1) input = "0" + input;
int i;
var list = new List
for (i = 0; i < input.Length; i += 2)
{
var temp = input.Substring(i, 2);
byte bv;
var success = byte.TryParse(temp, NumberStyles.HexNumber, null, out bv);
if (!success) throw new ArgumentOutOfRangeException();
list.Add(bv);
}
return list.ToArray();
}
#endregion
#region SHA1加密
///
/// SHA1加密 使用缺省密钥给字符串加密
///
///
///
public static string Sha1Encrypt(string sourceString)
{
var data = Encoding.Default.GetBytes(sourceString);
HashAlgorithm sha = new SHA1CryptoServiceProvider();
var bytes = sha.ComputeHash(data);
return BitConverter.ToString(bytes).Replace("-", "");
}
#endregion
#region DES加解密
///
/// DES加密
///
///
///
///
public static string DesEncrypt(string pubKey, string input)
{
var des = new DESCryptoServiceProvider();
var bytes = Encoding.UTF8.GetBytes(input);
des.Key = Encoding.ASCII.GetBytes(pubKey);
des.IV = Encoding.ASCII.GetBytes(pubKey);
using (var ms = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock();
}
var data = ms.ToArray();
return BitConverter.ToString(data).Replace("-", "");
}
}
///
/// DES解密
///
///
///
///
public static string DesDecrypt(string pubKey, string input)
{
var des = new DESCryptoServiceProvider();
var bytes = Hex2Bytes(input);
des.Key = Encoding.ASCII.GetBytes(pubKey);
des.IV = Encoding.ASCII.GetBytes(pubKey);
using (var ms = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock();
}
var data = ms.ToArray();
return Encoding.UTF8.GetString(data);
}
}
#endregion
#region MD5加密
///
/// MD5加密
///
/// 输入
///
public static string Md5Encrypt(string input)
{
MD5 md5 = new MD5CryptoServiceProvider();
var data = Encoding.UTF8.GetBytes(input);
var encs = md5.ComputeHash(data);
return BitConverter.ToString(encs).Replace("-", "");
}
#endregion
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播