[转][C#]加密解密类
{
public static class Crypter
{
private static string FDefaultPassword = typeof(Crypter).FullName;
public static string DefaultPassword
{
set
{
Crypter.FDefaultPassword = value;
}
}
public static Stream Encrypt(Stream dest, string password)
{
ICryptoTransform transform = null;
using (PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(password, Encoding.UTF8.GetBytes("Salt")))
{
transform = new RijndaelManaged
{
Padding = PaddingMode.ISO10126
}.CreateEncryptor(passwordDeriveBytes.GetBytes(), passwordDeriveBytes.GetBytes());
}
dest.Write(new byte[]
{
,
,
}, , );
return new CryptoStream(dest, transform, CryptoStreamMode.Write);
}
public static Stream Decrypt(Stream source, string password)
{
ICryptoTransform transform = null;
using (PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(password, Encoding.UTF8.GetBytes("Salt")))
{
transform = new RijndaelManaged
{
Padding = PaddingMode.ISO10126
}.CreateDecryptor(passwordDeriveBytes.GetBytes(), passwordDeriveBytes.GetBytes());
}
int arg_5C_0 = source.ReadByte();
int num = source.ReadByte();
int num2 = source.ReadByte();
if (arg_5C_0 == && num == && num2 == )
{
return new CryptoStream(source, transform, CryptoStreamMode.Read);
}
source.Position -= 3L;
return null;
}
public static bool IsStreamEncrypted(Stream stream)
{
int arg_25_0 = stream.ReadByte();
int num = stream.ReadByte();
int num2 = stream.ReadByte();
stream.Position -= 3L;
return arg_25_0 == && num == && num2 == ;
}
public static string EncryptString(string data)
{
return Crypter.EncryptString(data, Crypter.FDefaultPassword);
}
public static string EncryptString(string data, string password)
{
if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(password))
{
return data;
}
string result;
using (MemoryStream memoryStream = new MemoryStream())
{
using (Stream stream = Crypter.Encrypt(memoryStream, password))
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
stream.Write(bytes, , bytes.Length);
}
result = "rij" + Convert.ToBase64String(memoryStream.ToArray());
}
return result;
}
public static string DecryptString(string data)
{
return Crypter.DecryptString(data, Crypter.FDefaultPassword);
}
public static string DecryptString(string data, string password)
{
if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(password) || !data.StartsWith("rij"))
{
return data;
}
data = data.Substring();
string @string;
using (Stream stream = Converter.FromString(typeof(Stream), data) as Stream)
{
using (Stream stream2 = Crypter.Decrypt(stream, password))
{
byte[] array = new byte[data.Length];
int count = stream2.Read(array, , array.Length);
@string = Encoding.UTF8.GetString(array, , count);
}
}
return @string;
}
public static string ComputeHash(Stream input)
{
byte[] array = new byte[input.Length];
input.Read(array, , array.Length);
return Crypter.ComputeHash(array);
}
public static string ComputeHash(byte[] input)
{
return BitConverter.ToString(new Murmur3().ComputeHash(input)).Replace("-", string.Empty);
}
public static string ComputeHash(string input)
{
return Crypter.ComputeHash(Encoding.UTF8.GetBytes(input));
}
}
}
来自:https://github.com/FastReports/FastReport
遇到一串不知道具体编码的字符串,使用以下代码勉强转中文了:
str = str.Replace("9B25", "");
List<byte> buffer = new List<byte>();
for (int i = ; i < str.Length; i++)
{
if (i % == )
{
string s = str.Substring(i - , );
buffer.Add(Convert.ToByte(s, ));
}
}
Encoding.Default.GetString(buffer.ToArray());
[转][C#]加密解密类的更多相关文章
- [C#] 常用工具类——加密解密类
using System; using System.Configuration; using System.Collections.Generic; using System.Text; using ...
- 对接携程供应商php加密解密类
php加密解密类 <?php class Aes{ private $key = '6b4d63211b4ba869'; private $iv = 'dbbf079b95004f65'; pu ...
- PHP针对数字的加密解密类,可直接使用
<?phpnamespace app;/** * 加密解密类 * 该算法仅支持加密数字.比较适用于数据库中id字段的加密解密,以及根据数字显示url的加密. * @author 深秋的竹子 * ...
- Java常用的加密解密类(对称加密类)
Java常用的加密解密类 原文转载至:http://blog.csdn.net/wyc_cs/article/details/8793198 原创 2013年04月12日 14:33:35 1704 ...
- 生成二维码 加密解密类 TABLE转换成实体、TABLE转换成实体集合(可转换成对象和值类型) COOKIE帮助类 数据类型转换 截取字符串 根据IP获取地点 生成随机字符 UNIX时间转换为DATETIME\DATETIME转换为UNIXTIME 是否包含中文 生成秘钥方式之一 计算某一年 某一周 的起始时间和结束时间
生成二维码 /// <summary>/// 生成二维码/// </summary>public static class QRcodeUtils{private static ...
- java文本文件加密解密类
原文:http://www.open-open.com/code/view/1420031154765 import java.awt.*; import java.awt.event.*; impo ...
- AES对称加密解密类
import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; import javax.crypto.spec.Se ...
- 推荐分享一个牛X的自定义PHP加密解密类
通俗点说,用它来进行加密,同一个字符串,每次进行加密,得出的结果都是不一样的,大大加强了数据安全性.同时还可设定加密后数据的有效期,简直牛掰了 #食用方法 将下面的第二份模块代码保存为 Mcrypt. ...
- 一个java的DES加密解密类转换成C#
一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //import java.util.regex.P ...
- PHP加密解密类
<?php class Mypass { static function encrypt($data, $key){ $key = md5($key); $x = 0; $len = strle ...
随机推荐
- jquery获取选中值
1.获取一组radio被选中项的值:var item = $('input[name=items][checked]').val(); 2.获取select被选中项的文本 :var item = $( ...
- IO流的分类
按内容分:字节流和字符流 按流向分:输入流和输出流 字节流: 输入流:InputStream 输出流:OutputStream 字符流: 输入流:FileReader 输出流:FileWriter
- AngularJS +Kendo UI Grid template
var dataSource = new kendo.data.DataSource({ transport: { dataType: "json", read: inputUri ...
- 朋友给的IE滚动条
scrollbar-arrow-color: #f4ae21; /*图6,三角箭头的颜色*/scrollbar-face-color: #333; /*图5,立体滚动条的颜色*/scrollbar ...
- Java容器解析系列(3) List AbstractList ListIterator RandomAccess fail-fast机制 详解
做为数据结构学习的常规,肯定是先学习线性表,也就是Java中的List,开始 Java中List相关的类关系图如下: 此篇作为对Java中相关类的开篇.从上图中可以看出,List和AbstractLi ...
- ubuntu 网络配置
检查网络配置命令:ifconfig 一.通过配置文件配置 新手没怎么用过Ubuntu,所以走了不少弯路,网上找了很多方法,大都没对我起到帮助作用,所以把自己的配置方法写一写. Ubuntu上连了两块网 ...
- 2019-04-17-day034-线程与数据共享
内容回顾 锁 互斥锁 能够保护数据的安全性 保证对于数据的修改操作同一时刻多个进程只有一个进程执行 进程数据不安全 : 同时修改文件/数据库/其他共享资源的数据 ###队列 -- 实现了进程之间的通信 ...
- windows10 vs2017 C++连接MySQL
安装mysql8.0 x64 创建test数据库,user表,插入数据如下: +----+------+----------+-----------------+ | id | name | pass ...
- cocos2dx开发之util类&方法——取当前系统时间
返回time_t,即从1970年1月1日至今的秒数 time_t getSysTime(){ time_t currentTime = time(NULL); return currentTime; ...
- Python语法教程总结规范
Python语法易错点记录 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...