{
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#]加密解密类的更多相关文章

  1. [C#] 常用工具类——加密解密类

    using System; using System.Configuration; using System.Collections.Generic; using System.Text; using ...

  2. 对接携程供应商php加密解密类

    php加密解密类 <?php class Aes{ private $key = '6b4d63211b4ba869'; private $iv = 'dbbf079b95004f65'; pu ...

  3. PHP针对数字的加密解密类,可直接使用

    <?phpnamespace app;/** * 加密解密类 * 该算法仅支持加密数字.比较适用于数据库中id字段的加密解密,以及根据数字显示url的加密. * @author 深秋的竹子 *  ...

  4. Java常用的加密解密类(对称加密类)

    Java常用的加密解密类 原文转载至:http://blog.csdn.net/wyc_cs/article/details/8793198 原创 2013年04月12日 14:33:35 1704 ...

  5. 生成二维码 加密解密类 TABLE转换成实体、TABLE转换成实体集合(可转换成对象和值类型) COOKIE帮助类 数据类型转换 截取字符串 根据IP获取地点 生成随机字符 UNIX时间转换为DATETIME\DATETIME转换为UNIXTIME 是否包含中文 生成秘钥方式之一 计算某一年 某一周 的起始时间和结束时间

    生成二维码 /// <summary>/// 生成二维码/// </summary>public static class QRcodeUtils{private static ...

  6. java文本文件加密解密类

    原文:http://www.open-open.com/code/view/1420031154765 import java.awt.*; import java.awt.event.*; impo ...

  7. AES对称加密解密类

    import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; import javax.crypto.spec.Se ...

  8. 推荐分享一个牛X的自定义PHP加密解密类

    通俗点说,用它来进行加密,同一个字符串,每次进行加密,得出的结果都是不一样的,大大加强了数据安全性.同时还可设定加密后数据的有效期,简直牛掰了 #食用方法 将下面的第二份模块代码保存为 Mcrypt. ...

  9. 一个java的DES加密解密类转换成C#

    一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //import java.util.regex.P ...

  10. PHP加密解密类

    <?php class Mypass { static function encrypt($data, $key){ $key = md5($key); $x = 0; $len = strle ...

随机推荐

  1. Jboss解决只能通过localhost访问而不能使用IP访问项目的问题

    之前项目都是前后端完全分离,很少使用到后端语言的开发工具,最近使用intellij+Jboss进行项目部署开发,初始用发现项目启动后只能使用localhost进行项目访问,IP地址访问则提示页面404 ...

  2. python安装scrapy

    Scrapy基于事件驱动网络框架 Twisted 编写,Twisted是一个异步非阻塞框架. 安装 scrapy 要先安装 Twisted,不然无法安装成功,链接: Python Extension ...

  3. Android 音视频深入 十一 FFmpeg和AudioTrack播放声音(附源码下载)

    项目地址,求starhttps://github.com/979451341/AudioVideoStudyCodeTwo/tree/master/FFmpeg%E6%92%AD%E6%94%BE%E ...

  4. shell脚本结构

    echo $? 代表上一次命令的状态返回值,‘0’则代表为真<执行成功>,‘非零’则代表为假<执行失败>. shell脚本: <判断老男孩的年纪> [root@bo ...

  5. HTML5:表格相关标记及其属性

    表格相关标记及其属性 <table>:表格,包括以下属性 属性 说明 width 宽度(有像素和百分比两种表示方法) height 高度(有像素和百分比两种表示方法) border 边框粗 ...

  6. nginx——优化 Nginx 站点目录

    1. 禁止解析指定目录下的指定程序 location ~ ^/data/.*.(php|php5|sh|pl|py)$ { # 根据实际来禁止哪些目录下的程序,且该配置必须写在 Nginx 解析 PH ...

  7. nodejs --- formidable模块 , post 上传.

    1. 只有一个文件域: var formidable = require('formidable'), http = require('http'), util = require('util'); ...

  8. Mondrian辅助组件----Schema WorkBench(架构平台简介)

    Schema WorkBech 是Pentaho套件的另一个组件,是mondrian中schema文件生成工具.通过Schema WorkBench我们可以快速生成一个schema文件,不再需要手写. ...

  9. Ubuntu16.04 用Nomachine进行远程控制的配置

    本文介绍如何在Ubuntu16.04环境下运用Nomachine进行远程控制. 一. NoMachine介绍 NoMachine是一款基于NX技术进行远程控制的软件,最大的优势是跨平台,简单,可以实现 ...

  10. 新建一个self hosted Owin+ SignalR Project(1)

    OWIN是Open Web Server Interface for .Net 的首字母缩写,他的定义如下: OWIN在.NET Web Server 与Web Application之间定义了一套标 ...