C# 常用加密处理
AES
using System;
using System.Security.Cryptography;
using System.Text; namespace Common
{
public class AESProvider
{
public static int KEYSIZE = ; /// <summary>
/// 获取随机的加密密钥
/// </summary>
/// <param name="Key"></param>
/// <param name="IV"></param>
/// <returns></returns>
public static void GenerateKey(out string Key, out string IV)
{
Key = GenerateKey();
IV = GenerateIV();
} /// <summary>
/// 获取随机的加密密钥
/// </summary>
/// <returns></returns>
public static string GenerateKey()
{
int n = KEYSIZE / ;
char[] arrChar = new char[]{
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
'','','','','','','','','','',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
}; StringBuilder num = new StringBuilder(); Random rnd = new Random();
for (int i = ; i < n; i++)
{
num.Append(arrChar[rnd.Next(, arrChar.Length)].ToString());
} return num.ToString();
} /// <summary>
/// 获取随机的初始化向量
/// </summary>
/// <returns></returns>
public static string GenerateIV()
{
int n = KEYSIZE / ;
char[] arrChar = new char[]{
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
'','','','','','','','','','',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
}; StringBuilder num = new StringBuilder(); Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = ; i < n; i++)
{
num.Append(arrChar[rnd.Next(, arrChar.Length)].ToString());
} return num.ToString();
} /// <summary>
/// AES加密
/// </summary>
/// <param name="TextData">待加密字符</param>
/// <param name="Key">加密密钥</param>
/// <param name="IV">初始化向量</param>
/// <param name="CryptText">输出:已加密字符串</param>
/// <returns>0:成功加密 -1:待加密字符串不为能空 -2:加密密钥不能为空 -3:初始化向量字节长度不为KEYSIZE/8 -4:其他错误</returns>
public static int EncryptText(string TextData, string Key, string IV, out string CryptText)
{
CryptText = "";
TextData = TextData.Trim();
if (string.IsNullOrEmpty(TextData)) return -;
if (string.IsNullOrEmpty(Key)) return -;
if (string.IsNullOrEmpty(IV) || IV.Length != KEYSIZE / ) return -; try
{
RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = KEYSIZE;
rijndaelCipher.BlockSize = KEYSIZE; byte[] pwdBytes = Encoding.UTF8.GetBytes(Key);
byte[] ivBytes = Encoding.UTF8.GetBytes(IV); byte[] keyBytes = new byte[KEYSIZE / ];
int len = pwdBytes.Length;
if (len > keyBytes.Length) len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte[] plainText = Encoding.UTF8.GetBytes(TextData);
byte[] cipherBytes = transform.TransformFinalBlock(plainText, , plainText.Length); CryptText = Convert.ToBase64String(cipherBytes); return ;
}
catch
{
return -;
}
} /// <summary>
/// AES解密
/// </summary>
/// <param name="CryptText">待解密字符串</param>
/// <param name="Key">加密密钥</param>
/// <param name="IV">初始化向量</param>
/// <param name="TextData">输出:已解密的字符串</param>
/// <returns>0:成功解密 -1:待解密字符串不为能空 -2:加密密钥不能为空 -3:初始化向量字节长度不为KEYSIZE/8 -4:其他错误</returns>
public static int DecryptText(string CryptText, string Key, string IV, out string TextData)
{
TextData = "";
if (string.IsNullOrEmpty(CryptText)) return -;
if (string.IsNullOrEmpty(Key)) return -;
if (string.IsNullOrEmpty(IV) || IV.Length != KEYSIZE / ) return -; try
{
RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = KEYSIZE;
rijndaelCipher.BlockSize = KEYSIZE; byte[] encryptedData = Convert.FromBase64String(CryptText);
byte[] pwdBytes = Encoding.UTF8.GetBytes(Key);
byte[] ivBytes = Encoding.UTF8.GetBytes(IV); byte[] keyBytes = new byte[KEYSIZE / ];
int len = pwdBytes.Length;
if (len > keyBytes.Length) len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, , encryptedData.Length); TextData = Encoding.UTF8.GetString(plainText).Trim();
return ;
}
catch
{
return -;
}
}
}
}
RSA
using System;
using System.Security.Cryptography;
using System.Text; namespace Common
{
public class RSAProvider
{
public static int KEYSIZE = ;
/// <summary>
/// 自动生成公私密钥
/// </summary>
/// <param name="Modulus">输出:加密公钥</param>
/// <param name="Exponent">输入:公钥指数</param>
/// <param name="PrivateKey">输出:私钥</param>
public static void GenerateKey(out string Modulus, out string Exponent, out string PrivateKey)
{
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(KEYSIZE); RSAParameters pubparam = rsaProvider.ExportParameters(false);
byte[] mod = pubparam.Modulus;
byte[] exp = pubparam.Exponent; byte[] prikey = rsaProvider.ExportCspBlob(true); Modulus = Convert.ToBase64String(mod);
Exponent = Convert.ToBase64String(exp);
PrivateKey = Convert.ToBase64String(prikey);
} /// <summary>
/// RSA加密
/// </summary>
/// <param name="TextData">待加密字符</param>
/// <param name="Modulus">加密公钥</param>
/// <param name="Exponent">加密指数</param>
/// <param name="CryptText">输出:已加密字符串</param>
/// <returns>0:成功加密 -1:待加密字符串不为能空 -2:加密公钥不能为空 -3:待加密字符串超长 -4:其他错误</returns>
public static int EncryptText(string TextData, string Modulus, string Exponent, out string CryptText)
{
CryptText = "";
if (string.IsNullOrEmpty(TextData)) return -;
if (string.IsNullOrEmpty(Modulus)) return -;
if (TextData.Length > KEYSIZE / - ) return -;
try
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(KEYSIZE);
byte[] modBytes = Convert.FromBase64String(Modulus);
byte[] expBytes = Convert.FromBase64String(Exponent); RSAParameters p = new RSAParameters();
p.Modulus = modBytes;
p.Exponent = expBytes; rsa.ImportParameters(p);
byte[] plainText = Encoding.UTF8.GetBytes(TextData);
byte[] cipherBytes = rsa.Encrypt(plainText, false); CryptText = Convert.ToBase64String(cipherBytes); return ;
}
catch { return -; }
} /// <summary>
/// RSA解密
/// </summary>
/// <param name="CryptText">待解密字符串</param>
/// <param name="Key">解密私钥</param>
/// <param name="TextData">输出:已解密的字符串</param>
/// <returns>0:成功解密 -1:待解密字符串不为能空 -2:解密私钥不能为空 -4:其他错误</returns>
public static int DecryptText(string CryptText, string PrivateKey, out string TextData)
{
TextData = ""; if (string.IsNullOrEmpty(CryptText)) return -;
if (string.IsNullOrEmpty(PrivateKey)) return -; try
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(KEYSIZE); byte[] prikey = Convert.FromBase64String(PrivateKey); ;
rsa.ImportCspBlob(prikey); byte[] cipherBytes = Convert.FromBase64String(CryptText);
byte[] plainText = rsa.Decrypt(cipherBytes, false); TextData = Encoding.UTF8.GetString(plainText); return ;
}
catch { return -; }
}
}
}
MD5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Common
{
public class EncryptMD5
{ public static string GetMd5(string s, Encoding encoding)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();//实例化一个MD5
byte[] t = md5.ComputeHash(encoding.GetBytes(s));//生成MD5哈希值
StringBuilder sb = new StringBuilder();
for (int i = ; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(, ''));//将哈希值转换为字符串
}
return sb.ToString().ToUpper();
}
}
}
C# 常用加密处理的更多相关文章
- iOS代码加密常用加密方式
iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...
- PHP的几个常用加密函数(转载 https://jellybool.com/post/php-encrypt-functions)
PHP的几个常用加密函数 在网站的开发过程中,常常需要对部分数据(如用户密码)进行加密,本文主要介绍PHP的几个常见的加密函数 MD5加密: string md5 ( string $str [, b ...
- 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 ...
- php实现的三个常用加密解密功能函数示例
目录 算法一: 算法二: 算法三(改进第一个加密之后的算法) 本文实例讲述了php实现的三个常用加密解密功能函数.分享给大家供大家参考,具体如下: 算法一: //加密函数 function lock_ ...
- iOS常用加密方法(aes、md5、base64)
1.代码 iOS常用加密方法(aes.md5.base64) .AES加密 NSData+AES.h文件 // // NSData-AES.h // Smile // // Created by 周 ...
- PHP的几个常用加密函数
在php的开发过程中,常常需要对部分数据(如用户密码)进行加密 一.加密类型: 1.单向散列加密 就是把任意长度的信息进行散列计算,得到固定长度的输出,这个散列计算过程是单向的,即不能对固定长度的输出 ...
- JAVA常用加密解密算法Encryption and decryption
加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...
- .NET Core 常用加密和Hash工具NETCore.Encrypt
前言 在日常开发过程中,不可避免的涉及到数据加密解密(Hash)操作,所以就有想法开发通用工具,NETCore.Encrypt就诞生了.目前NETCore.Encrypt只支持.NET Core ,工 ...
- 关于Java中常用加密/解密方法的实现
安全问题已经成为一个越来越重要的问题,在Java中如何对重要数据进行加密解密是本文的主要内容. 一.常用的加密/解密算法 1.Base64 严格来说Base64并不是一种加密/解密算法,而是一种编码方 ...
随机推荐
- php大力力 [017节]来来来,庆祝一下🎁大力力第一个数据库录入程序完成!
庆祝一下
- Koch曲线
Koch曲线是一种分形,完整的Koch曲线像雪花,维基百科上记录Koch曲线最早出现在海里格·冯·科赫的论文<关于一条连续而无切线,可由初等几何构作的曲线>中,它的定义如下,给定线段AB, ...
- yii2 数据验证
控制器层 <?php namespace frontend\controllers; use Yii; use frontend\models\FormsModel; use yii\web\U ...
- 用云存储和CDN轻松搞定网站图片
http://www.kuqin.com/web/20120207/317912.html
- The Separator in Grid_BFS
Description Given a connected, undirected graph G = (V, E), where V is the vertex set consisting a c ...
- C# 封装一个钩子类
利用C#设置钩子函数,并封装成类.如果想要实现全局钩子的话,必须将实现的代码封装到类库里. using System; using System.Collections.Generic; using ...
- JQuery blockUI
1 $.blockUI({//界面锁定之后 ,显示样式和提示消息 css: { width: 'auto', left: '20px', right: '20px' }, message: '< ...
- ie6下png背景显示问题?
针对ie6下png背景显示问题,CSS中可以这样解决:_background:none;_filter:progid:DXImageTransform.Microsoft.AlphaImageLoad ...
- Unity3d Shader
Unity3d Shader 预览Surface Shader主要用来实现光照相关处理,可能更简洁. Vertex and Fragment Shader 如果不与光照交互, 则可以用这个shader ...
- 本地日志数据实时接入到hadoop集群的数据接入方案
1. 概述 本手册主要介绍了,一个将传统数据接入到Hadoop集群的数据接入方案和实施方法.供数据接入和集群运维人员参考. 1.1. 整体方案 Flume作为日志收集工具,监控一个文件目录或者一个文 ...