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. c# winform as3相互调用

    C#主要代码: 首先要添加COM组件-Shockwave Flash Object //接收flash发送过来的信息        private void axShockwaveFlash1_Fla ...

  2. ecshop 安装出错gd_version

    678: static function gd_version()

  3. php -- 文件读写

    ----- 024-file.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv="c ...

  4. JavaScript -- FileSystemObject-文件

    -----058-FileSystemObject-文件.html----- <!DOCTYPE html> <html> <head> <meta http ...

  5. scala-04-set操作

    Scala Set(集合)是没有重复的对象集合,所有的元素都是唯一的. Scala 集合分为可变的和不可变的集合. 默认情况下,Scala 使用的是不可变集合,如果你想使用可变集合,需要引用 scal ...

  6. Java队列——线程池创建的例子

    线程池为线程生命周期开销问题和资源不足问题提供了解决方案.通过对多个任务重用线程,线程创建的开销被分摊到了多个任务上.其好处是,因为在请求到达时线程已经存在,所以无意中也消除了线程创建所带来的延迟.这 ...

  7. 面试:用快排实现数组中的第K大的数

    #include <iostream> #include <cassert> using namespace std; int selectKth(int a[],int st ...

  8. mysql LAST_INSERT_ID详解

    http://blog.sina.com.cn/s/blog_5b5460eb0100nwvo.html LAST_INSERT_ID() LAST_INSERT_ID(expr) 自动返回最后一个I ...

  9. Git学习笔记4

    现在,远程库已经准备好了,下一步是用命令git clone克隆一个本地库: $ git clone git@github.com:michaelliao/gitskills.git 要克隆一个仓库,首 ...

  10. iphone safari浏览器CSS兼容性的解决方案集合

    1.iphone safari不兼容CSS的active的解决方案如下: <body ontouchstart="" onmouseover=""> ...