Sql版加密解密函数
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System;
using System.Data;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Security.Cryptography;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static string Encrypt_ASE(string source, string key)
{
string Rst;
try
{
Rijndael ri = Rijndael.Create();
ri.Key = Encoding.Default.GetBytes(key);
ri.IV = Encoding.Default.GetBytes(key.Substring(0, 16));
byte[] ridata = Encoding.Default.GetBytes(source);
MemoryStream memo = new MemoryStream();
CryptoStream Cry = new CryptoStream(memo, ri.CreateEncryptor(), CryptoStreamMode.Write);
Cry.Write(ridata, 0, ridata.Length);
Cry.FlushFinalBlock();
Rst = Convert.ToBase64String(memo.ToArray());
}
catch
{
Rst = null;
}
return Rst;
}
[Microsoft.SqlServer.Server.SqlFunction]
public static string Dencrypt_ASE(string source, string key)
{
string Rst;
try
{
Rijndael ri = Rijndael.Create();
ri.Key = Encoding.Default.GetBytes(key);
ri.IV = Encoding.Default.GetBytes(key.Substring(0, 16));
byte[] ridata = Convert.FromBase64String(source);
MemoryStream memo = new MemoryStream();
CryptoStream Cry = new CryptoStream(memo, ri.CreateDecryptor(), CryptoStreamMode.Write);
Cry.Write(ridata, 0, ridata.Length);
Cry.FlushFinalBlock();
Rst = Encoding.Default.GetString(memo.ToArray());
}
catch
{
Rst = null;
}
return Rst;
}
[Microsoft.SqlServer.Server.SqlFunction]
public static string Encrypt_MD5(string source)
{
string Rst = "";
MD5CryptoServiceProvider MD = new MD5CryptoServiceProvider();
byte[] mddata = Encoding.Default.GetBytes(source);
byte[] mdbuff = puteHash(mddata);
foreach (byte b in mdbuff)
Rst += b.ToString("X").PadLeft(2, '0');
return Rst;
}
[Microsoft.SqlServer.Server.SqlFunction]
public static string Encrypt_DES(string source, string key)
{
string Rst;
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Key = Encoding.Default.GetBytes(key);
des.IV = Encoding.Default.GetBytes(key);
byte[] desdata = Encoding.Default.GetBytes(source);
MemoryStream memo = new MemoryStream();
CryptoStream Cry = new CryptoStream(memo, des.CreateEncryptor(), CryptoStreamMode.Write);
Cry.Write(desdata, 0, desdata.Length);
Cry.FlushFinalBlock();
Rst = Convert.ToBase64String(memo.ToArray());
}
catch
{
Rst = null;