C#安全加密类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Data;
using System.Data.SQLite;
using System.Web;
using System.Runtime.InteropServices; namespace AiXinTang.User
{
public static class DbSafe
{
public static DataTable GetNewDataTable(DataTable dt, string condition, string sortstr)
{
DataTable newdt = new DataTable();
newdt = dt.Clone();
DataRow[] dr = dt.Select(condition, sortstr);
for (int i = ; i < dr.Length; i++)
{
newdt.ImportRow((DataRow)dr[i]);
}
return newdt;//返回的查询结果
} public static string md5(string str, int code)
{
string mymd5 = string.Empty;
if (code == ) //16位MD5加密(取32位加密的9~25字符)
{
mymd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(, );
}
if (code == ) //32位加密
{
mymd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
}
return mymd5;
} public static string myChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_+,./;'|:?-={}"; /// <summary>
/// 根据制定位数获取不同随机字符串
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static string GetStr(int num)
{
int n = myChar.Length;
string str = string.Empty;
if (num < n)
{
Random _currenRandom = new Random(Guid.NewGuid().GetHashCode());
for (int i = ; i < num; i++)
{
int _n = _currenRandom.Next(, n - );
str += myChar.Substring(_n, );
}
}
return str;
} /// <summary>
/// 生成AES密钥32位
/// </summary>
public static string GetAESKey
{
get
{
return GetStr();
}
} /// <summary>
/// 生成AES常量16位
/// </summary>
public static string GetAESIV
{
get
{
return GetStr();
}
} /// <summary>
/// 生成DES密钥32位
/// </summary>
public static string GetDESKey
{
get
{
return GetStr();
}
} /// <summary>
/// 生成DES常量16位
/// </summary>
public static string GetDESIV
{
get
{
return GetStr();
}
} } /// <summary>
/// DES加密解密 KEY-8 IV-16
/// </summary>
public static class DES
{ /// <summary>
/// DES加密 KEY-8 IV-16
/// </summary>
/// <param name="plainStr">明文字符串</param>
/// <returns>密文</returns>
public static string DESEncrypt(string Key, string IV, string plainStr)
{
byte[] bKey = Encoding.UTF8.GetBytes(Key);
byte[] bIV = Encoding.UTF8.GetBytes(IV);
byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, , byteArray.Length);
cStream.FlushFinalBlock();
encrypt = Convert.ToBase64String(mStream.ToArray());
}
}
}
catch { }
des.Clear(); return encrypt;
} /// <summary>
/// DES解密 KEY-8 IV-16
/// </summary>
/// <param name="encryptStr">密文字符串</param>
/// <returns>明文</returns>
public static string DESDecrypt(string Key, string IV, string encryptStr)
{
byte[] bKey = Encoding.UTF8.GetBytes(Key);
byte[] bIV = Encoding.UTF8.GetBytes(IV);
byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, , byteArray.Length);
cStream.FlushFinalBlock();
decrypt = Encoding.UTF8.GetString(mStream.ToArray());
}
}
}
catch { }
des.Clear(); return decrypt;
}
} /// <summary>
/// AES加密解密 KEY-32 IV-16
/// </summary>
public static class AES
{ /// <summary>
/// AES加密 KEY-32 IV-16
/// </summary>
/// <param name="plainStr">明文字符串</param>
/// <returns>密文</returns>
public static string AESEncrypt(string Key, string IV, string plainStr)
{
byte[] bKey = Encoding.UTF8.GetBytes(Key);
byte[] bIV = Encoding.UTF8.GetBytes(IV);
byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null;
Rijndael aes = Rijndael.Create();
try
{
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, , byteArray.Length);
cStream.FlushFinalBlock();
encrypt = Convert.ToBase64String(mStream.ToArray());
}
}
}
catch { }
aes.Clear(); return encrypt;
} /// <summary>
/// AES解密 KEY-32 VI-IV
/// </summary>
/// <param name="encryptStr">密文字符串</param>
/// <returns>明文</returns>
public static string AESDecrypt(string Key, string IV, string encryptStr)
{
byte[] bKey = Encoding.UTF8.GetBytes(Key);
byte[] bIV = Encoding.UTF8.GetBytes(IV);
byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null;
Rijndael aes = Rijndael.Create();
try
{
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, , byteArray.Length);
cStream.FlushFinalBlock();
decrypt = Encoding.UTF8.GetString(mStream.ToArray());
}
}
}
catch { }
aes.Clear(); return decrypt;
} } /// <summary>
/// SQLITE操作
/// </summary>
public static class Sqlite
{
/// <summary>
/// 是否链接成功
/// </summary>
/// <param name="path"></param>
/// <param name="pwd"></param>
/// <returns></returns>
public static bool IfConn(string path, string pwd)
{
bool r = false;
if (!string.IsNullOrEmpty(path))
{
var con = new SQLiteConnection();
if (!string.IsNullOrEmpty(pwd))
{
con.ConnectionString = String.Format("Data Source={0};Version=3;Password={1}", path.Trim(), pwd.Trim());
}
else
{
con.ConnectionString = String.Format("Data Source={0};Version=3;", path.Trim());
}
try
{
con.Open();
r = true; }
catch
{
r = false;
}
finally
{ con.Close();
}
}
return r;
} /// <summary>
/// 重置数据库链接密码
/// </summary>
/// <param name="path"></param>
/// <param name="pwdold"></param>
/// <param name="pwdnew"></param>
/// <returns></returns>
public static bool SetPwd(string path, string pwdold, string pwdnew)
{
bool r = false;
var con = new SQLiteConnection();
if (!string.IsNullOrEmpty(pwdold))
{
con.ConnectionString = String.Format("Data Source={0};Version=3;Password={1}", path.Trim(), pwdold.Trim());
}
else
{
con.ConnectionString = String.Format("Data Source={0};Version=3;", path.Trim());
}
try
{
con.Open();
if (!string.IsNullOrEmpty(pwdnew.Trim()))
{
con.ChangePassword(pwdnew.Trim());
}
r = true; }
catch
{
r = false;
}
finally
{ con.Close();
}
return r;
} [DllImport("kernel32.dll")]
public static extern IntPtr _lopen(string lpPathName, int iReadWrite); [DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);
/// <summary>
/// 创建新的用户数据库
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool DbNew(string name,string pwd)
{
bool r = false;
string path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.TrimEnd('\\')+@"\app_data\");
if (File.Exists(path + "AXT.User.Demo.config") && !File.Exists(path + name + ".config"))
{
File.Copy(path + "AXT.User.Demo.config", path + name+".config");
if (File.Exists(path + name + ".config"))
{
const int OF_READWRITE = ;
const int OF_SHARE_DENY_NONE = 0x40;
IntPtr HFILE_ERROR = new IntPtr(-);
IntPtr vHandle = _lopen(path + name + ".config", OF_READWRITE | OF_SHARE_DENY_NONE);
CloseHandle(vHandle);
if (SetPwd(path + name + ".config", "", pwd))
{
r = true;
}
}
}
return r;
} public static bool IsFileInUse(string fileName)
{
bool inUse = true; FileStream fs = null;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None); inUse = false;
}
catch
{ }
finally
{
if (fs != null) fs.Close();
}
return inUse;
} }
}
C#安全加密类的更多相关文章
- php加密类
1.需求 了解php加密类的使用 2.例子 参考ci的3.1.2的新版加密类,一个不传参,用默认加密算法,加密模式的例子 //0.加载加密类 $this->load->library('e ...
- C#加密类
var es= EncryptSugar.GetInstance(); string word = "abc"; var wordEncrypt = es.Encrypto(wor ...
- PHP的AES加密类
PHP的AES加密类 aes.php <?php /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...
- C# DES加密类,16位的加密。
这个加密类是与java写的DES加密不同时,自己写的,最后与Java的加密相同了,解决了加密后不同的问题. 可以直接调用里面的加密和解密的方法. using System; using System. ...
- php实现aes加密类
php实现的aes加密类,代码中有使用方法. <?php //php aes加密类 class AESMcrypt { public $iv = null; public $key = null ...
- Java 自带的加密类MessageDigest类(加密MD5和SHA)
Java 自带的加密类MessageDigest类(加密MD5和SHA) - X-rapido的专栏 - CSDN博客 https://blog.csdn.net/xiaokui_wingfly/ar ...
- C#简单的加密类
1.加密 public class EncryptHepler { // 验值 static string saltValue = "XXXX"; // 密码值 static st ...
- Java常用的加密解密类(对称加密类)
Java常用的加密解密类 原文转载至:http://blog.csdn.net/wyc_cs/article/details/8793198 原创 2013年04月12日 14:33:35 1704 ...
- JAVA中简单的MD5加密类(MD5Utils)
MD5加密分析: JDK API: 获取对象的API: 加密的API: package cn.utils; import java.security.MessageDigest; im ...
- C#使用SHA1加密类(RSAFromPkcs8)支持1024位和2048位私钥
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
随机推荐
- 哨兵/sentinel:在算法设计中的应用
哨兵(sentinel)昨天看算法导论里对哨兵的描述后,觉得这是一种很有意思的编程思想.哨兵是一个哑对象.一般哨兵不存放任何数据,但其结构体与其他有用的元素一致.正如其字面意思,哨兵是在边界保卫祖国的 ...
- POJ 2083 Fractal 分形题目
这两天自学了一线算法导论里分治策略的内容,秉着只有真正投入投入编程,才能更好的理解一种算法的思想的想法,兴致勃勃地找一些入门的题来学习. 搜了一下最后把目光锁定在了Poj fractal这一个题上.以 ...
- Linux安装codis
codis用go语言开发的,安装前记得先安装go开发环境,Linux安装go语言开发包 前提条件:记得安装git,否则无法下载go语言开发包 1.建立一个go语言的工作目录创建目录:sudo mkdi ...
- 进程间通信 IPC(Inter-Process Communication)
目录 一.管道 二.FIFO 三.消息队列 四.信号量 五.共享存储 六.网络IPC:套接字 一.管道 管道是进程间通信中最古老的方式,所有UNIX都提供此种通信机制.管道有以下两种局限性: 历史 ...
- android app启动过程
Native进程的运行过程 一般程序的启动步骤,可以用下图描述.程序由内核加载分析,使用linker链接需要的共享库,然后从c运行库的入口开始执行. 通常,native进程是由shell或者init启 ...
- mysqldump 用法
mysqldump 是文本备份还是二进制备份 它是文本备份,如果你打开备份文件你将看到所有的语句,可以用于重新创建表和对象.它也有 insert 语句来使用数据构成表. mysqldump 的语法是什 ...
- B 树、B+ 树、B* 树
B 树.B+ 树.B* 树 作者:July.weedge.Frankie.编程艺术室出品. 说明:本文从B树开始谈起,然后论述B+树.B*树,最后谈到R 树.其中B树.B+树及B*树部分由weedge ...
- elasticSearch6源码分析(4)indices模块
1.indices概述 The indices module controls index-related settings that are globally managed for all ind ...
- git第四节----git commit message
@git commit message 什么是git commit message :git commit -m '每次提交时编辑的内容' git commit message的好处: 1 ...
- php和mysql学习问题笔记
1.Undefined index: pwd in E:\xampp\htdocs\phpbase2elite\12\source\register.php on line 6 这是一个警告,表示数组 ...