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#安全加密类的更多相关文章

  1. php加密类

    1.需求 了解php加密类的使用 2.例子 参考ci的3.1.2的新版加密类,一个不传参,用默认加密算法,加密模式的例子 //0.加载加密类 $this->load->library('e ...

  2. C#加密类

    var es= EncryptSugar.GetInstance(); string word = "abc"; var wordEncrypt = es.Encrypto(wor ...

  3. PHP的AES加密类

    PHP的AES加密类 aes.php <?php /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...

  4. C# DES加密类,16位的加密。

    这个加密类是与java写的DES加密不同时,自己写的,最后与Java的加密相同了,解决了加密后不同的问题. 可以直接调用里面的加密和解密的方法. using System; using System. ...

  5. php实现aes加密类

    php实现的aes加密类,代码中有使用方法. <?php //php aes加密类 class AESMcrypt { public $iv = null; public $key = null ...

  6. Java 自带的加密类MessageDigest类(加密MD5和SHA)

    Java 自带的加密类MessageDigest类(加密MD5和SHA) - X-rapido的专栏 - CSDN博客 https://blog.csdn.net/xiaokui_wingfly/ar ...

  7. C#简单的加密类

    1.加密 public class EncryptHepler { // 验值 static string saltValue = "XXXX"; // 密码值 static st ...

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

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

  9. JAVA中简单的MD5加密类(MD5Utils)

    MD5加密分析:   JDK API:   获取对象的API:   加密的API:   package cn.utils; import java.security.MessageDigest; im ...

  10. C#使用SHA1加密类(RSAFromPkcs8)支持1024位和2048位私钥

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

随机推荐

  1. iconfont 批量把图标加入购物车的方法

    在浏览器中按 f12 打开[开发人员工具],找到[console(控制台)],输入以下代码,再按回车,稍等片刻即可把全部图标加入购物车 var ll = document.getElementsByC ...

  2. 结构体访问成员变量什么时候该用“->”或者是"."呢?的困惑

    煎蛋栗子: typedef struct Node{int data;struct Node *next;}LinkList; LinkList *p=(LinkList *)malloc(sizeo ...

  3. JAVA与DOM解析器提高(DOM/SAX/JDOM/DOM4j/XPath) 学习笔记二

    要求 必备知识 JAVA基础知识.XML基础知识. 开发环境 MyEclipse10 资料下载 源码下载   sax.dom是两种对xml文档进行解析的方法(没有具体实现,只是接口),所以只有它们是无 ...

  4. 第三方登录:微信扫码登录(OAuth2.0)

    1.OAuth2.0 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. 允许用户提供 ...

  5. centos7-使用nginx做ftp站

    关于nginx的安装可见: http://www.cnblogs.com/wenbronk/p/6557482.html 然后最简单的方式, 修改nginx的配置文件: server { listen ...

  6. Python高级特性: 12步轻松搞定Python装饰器

    12步轻松搞定Python装饰器 通过 Python 装饰器实现DRY(不重复代码)原则:  http://python.jobbole.com/84151/   基本上一开始很难搞定python的装 ...

  7. Python高级特性: 函数编程 lambda, filter,map,reduce

    一.概述 Python是一门多范式的编程语言,它同时支持过程式.面向对象和函数式的编程范式.因此,在Python中提供了很多符合 函数式编程 风格的特性和工具. 以下是对 Python中的函数式编程 ...

  8. mysql索引总结(3)-MySQL聚簇索引和非聚簇索引

    mysql索引总结(1)-mysql 索引类型以及创建 mysql索引总结(2)-MySQL聚簇索引和非聚簇索引 mysql索引总结(3)-MySQL聚簇索引和非聚簇索引 mysql索引总结(4)-M ...

  9. 版本管理(二)之Git和GitHub的连接和使用

    首先需要注册登录GitHub:https://github.com 然后 ①:下载Git 先从Git官网,由于我的系统是64位的所以选择64-bit Git for Windows Setup htt ...

  10. [Node.js] 3、搭建hexo博客

      一.安装新版本的nodejs和npm 安装n模块: npm install -g n 升级node.js到最新稳定版 n stable   二.安装hexo note: 参考github,不要去其 ...