C# 加解密工具类
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text; namespace CloverNet.SecurityTool
{
public partial class SecurityTool
{
public static class AES
{
//默认密钥向量
private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; public static string Encrypt(string originalStr, string secretKey)
{
//分组加密算法
SymmetricAlgorithm des = Rijndael.Create();
byte[] inputByteArray = Encoding.UTF8.GetBytes(originalStr);//得到需要加密的字节数组
//设置密钥及密钥向量
des.Key = Encoding.UTF8.GetBytes(secretKey);
des.IV = _key1;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组
cs.Close();
ms.Close();
return Convert.ToBase64String(cipherBytes);
} public static string Decrypt(string secretStr, string secretKey)
{
var secretbytes = Convert.FromBase64String(secretStr); SymmetricAlgorithm des = Rijndael.Create();
des.Key = Encoding.UTF8.GetBytes(secretKey);
des.IV = _key1;
byte[] decryptBytes = new byte[secretbytes.Length];
MemoryStream ms = new MemoryStream(secretbytes);
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
cs.Read(decryptBytes, , decryptBytes.Length);
cs.Close();
ms.Close();
return Encoding.UTF8.GetString(decryptBytes);
} }
}
}
using System; namespace CloverNet.SecurityTool
{
public partial class SecurityTool
{
public static class Base64
{
public static string Encrypt(string originalStr)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(originalStr);
return Convert.ToBase64String(bytes);
} public static string Decrypt(string originalStr)
{
var bytes = Convert.FromBase64String(originalStr);
return System.Text.Encoding.UTF8.GetString(bytes);
}
}
}
}
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text; namespace CloverNet.SecurityTool
{
public partial class SecurityTool
{
public static class DES
{
public static string Encrypt(string originalStr, string secretKey)
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(originalStr);
des.Key = ASCIIEncoding.ASCII.GetBytes(MD5.Encrypt(secretKey).Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(MD5.Encrypt(secretKey).Substring(, ));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Convert.ToBase64String(ms.ToArray());
ms.Close();
return str;
}
} public static string Decrypt(string secretStr, string secretKey)
{
byte[] inputByteArray = Convert.FromBase64String(secretStr);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = ASCIIEncoding.ASCII.GetBytes(MD5.Encrypt(secretKey).Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(MD5.Encrypt(secretKey).Substring(, ));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
}
}
}
using System.Security.Cryptography;
using System.Text; namespace CloverNet.SecurityTool
{
public partial class SecurityTool
{
public static class MD5
{
public static string Encrypt(string originalStr)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes;
hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(originalStr));
StringBuilder byte2String = new StringBuilder();
foreach (byte i in hashedDataBytes)
{
byte2String.Append(i.ToString("x2"));
}
return byte2String.ToString();
}
}
} }
namespace CloverNet.SecurityTool
{
public partial class SecurityTool
{
public static class SHA256
{
public static string Encrypt(string originalStr)
{
//使用 SHA256 加密算法:
System.Security.Cryptography.SHA256 sha256 = new System.Security.Cryptography.SHA256Managed();
byte[] sha256Bytes = System.Text.Encoding.Default.GetBytes(originalStr);
byte[] cryString = sha256.ComputeHash(sha256Bytes);
string sha256Str = string.Empty;
for (int i = ; i < cryString.Length; i++)
{
sha256Str += cryString[i].ToString("X2");
}
return sha256Str;
}
}
}
}
C# 加解密工具类的更多相关文章
- RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密
package com.geostar.gfstack.cas.util; import org.apache.commons.codec.binary.Base64; import javax.cr ...
- Java中的AES加解密工具类:AESUtils
本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.constants.SysConsta ...
- Java中的RSA加解密工具类:RSAUtils
本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.utils.log.LogUtils; ...
- DES加解密工具类
这两天在跟友商对接接口,在对外暴露接口的时候,因为友商不需要登录即可访问对于系统来说存在安全隐患,所以需要友商在调用接口的时候需要将数据加密,系统解密验证后才执行业务.所有的加密方式并不是万能的,只是 ...
- vue 核心加解密工具类 方法
1 /* base64 加解密 2 */ 3 export let Base64 = require('js-base64').Base64 4 5 /* md5 加解密 6 */ 7 export ...
- 加解密工具类(含keystore导出pfx)
java代码如下: package sign; import java.io.FileInputStream; import java.io.FileOutputStream; import java ...
- Base64加密解密工具类
使用Apache commons codec类Base64进行加密解密 maven依赖 <dependency> <groupId>commons-codec</grou ...
- XJar: Spring-Boot JAR 包加/解密工具,避免源码泄露以及反编译
XJar: Spring-Boot JAR 包加/解密工具,避免源码泄露以及反编译 <?xml version="1.0" encoding="UTF-8" ...
- AES加密、解密工具类
AES加密.解密工具类代码如下: package com.util; import java.io.IOException; import java.io.UnsupportedEncodingExc ...
随机推荐
- Introduction to Go Modules
转:https://roberto.selbach.ca/intro-to-go-modules/ git init git add * git commit -am "First comm ...
- DX12 开debuggerlayer
https://social.technet.microsoft.com/Forums/azure/en-US/ef10f8eb-fee0-4cde-bb01-52d1db2ea347/win10-1 ...
- 巧用 Img / JavaScript 采集页面数据
摘要: 当我们有一个新内容时(例如新功能.新活动.新游戏.新文章),作为运营人员总是迫不及待地希望能尽快传达到用户,因为这是获取用户的第一步.也是最重要的一步. 点此查看原文:http://click ...
- 18-SQLServer中给视图创建索引
一.注意点 1.索引视图所引用的基表必须在同一个数据库中,不是用union all引用多个数据库的表: 2.创建索引视图时要加上with schemabinding: 3.创建索引视图时要指定表所属的 ...
- 理解SqlMapConfig.xml文件
SqlMapConfig.xml mybatis的全局配置文件SqlMapConfig.xml,配置内容如下: properties(属性) settings(全局配置参数) typeAliases( ...
- [Spring Boot] Set Context path for application in application.properties
If you were using Microservice with Spring Boot to build different REST API endpoints, context path ...
- Remove Mapping
http://social.msdn.microsoft.com/forums/vstudio/en-US/ffa3aa15-1d61-4464-ac4a-7a812d073a67/remove-ma ...
- 51 Nod 1475 建设国家 (优先队列+贪心)
1475 建设国家 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 小C现在想建设一个国家.这个国家中有一个首都,然后有若干个中间站,还有若干个城 ...
- Java的23种设计模式 <二>
1.单例模式(Singleton Pattern) 定义:Ensure a class has only one instance, and provide a global point of acc ...
- layer提示带文字
直接撸代码: //加载层-风格4 layer.msg('加载中', { icon: 16 ,shade: 0.01 });