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 = MD.ComputeHash(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;
}
return Rst;
}
[Microsoft.SqlServer.Server.SqlFunction]
public static string Dencrypt_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 = Convert.FromBase64String(source);
MemoryStream memo = new MemoryStream();
CryptoStream Cry = new CryptoStream(memo, des.CreateDecryptor(), CryptoStreamMode.Write);
Cry.Write(desdata, 0, desdata.Length);
Cry.FlushFinalBlock();
Rst = Encoding.Default.GetString(memo.ToArray());
}
catch
{
Rst = null;
}
return Rst;
}
[Microsoft.SqlServer.Server.SqlFunction]
public static string Encrypt_TripleDES(string source, string key)
{
string Rst;
try
{
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = Encoding.Default.GetBytes(key);
tdes.IV = Encoding.Default.GetBytes(key.Substring(0, 8));
byte[] tdesdata = Encoding.Default.GetBytes(source);
MemoryStream memo = new MemoryStream();
CryptoStream Cry = new CryptoStream(memo, tdes.CreateEncryptor(), CryptoStreamMode.Write);
Cry.Write(tdesdata, 0, tdesdata.Length);
Cry.FlushFinalBlock();
Rst = Convert.ToBase64String(memo.ToArray());
}
catch
{
Rst = null;
}
return Rst;
}
[Microsoft.SqlServer.Server.SqlFunction]
public static string Dencrypt_TripleDES(string source, string key)
{
string Rst;
try
{
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = Encoding.Default.GetBytes(key);
tdes.IV = Encoding.Default.GetBytes(key.Substring(0, 8));
byte[] tdesdata = Convert.FromBase64String(source);
MemoryStream memo = new MemoryStream();
CryptoStream Cry = new CryptoStream(memo, tdes.CreateDecryptor(), CryptoStreamMode.Write);
Cry.Write(tdesdata, 0, tdesdata.Length);
Cry.FlushFinalBlock();
Rst = Encoding.Default.GetString(memo.ToArray());
}
catch
{
Rst = null;
}
return Rst;
}
[Microsoft.SqlServer.Server.SqlFunction]
public static string Encrypt_RC2(string source, string key)
{
string Rst;
try
{
RC2CryptoServiceProvider rc = new RC2CryptoServiceProvider();
rc.Key = Encoding.Default.GetBytes(key);
rc.IV = Encoding.Default.GetBytes(key);
byte[] rcdata = Encoding.Default.GetBytes(source);
MemoryStream memo = new MemoryStream();
CryptoStream Cry = new CryptoStream(memo, rc.CreateEncryptor(), CryptoStreamMode.Write);
Cry.Write(rcdata, 0, rcdata.Length);
Cry.FlushFinalBlock();
Rst = Convert.ToBase64String(memo.ToArray());
}
catch
{
Rst = null;
}
return Rst;
}
[Microsoft.SqlServer.Server.SqlFunction]
public static string dEncrypt_RC2(string source, string key)
{
string Rst;
try
{
RC2CryptoServiceProvider rc = new RC2CryptoServiceProvider();
rc.Key = Encoding.Default.GetBytes(key);
rc.IV = Encoding.Default.GetBytes(key);
byte[] rcdata = Convert.FromBase64String(source);
MemoryStream memo = new MemoryStream();
CryptoStream Cry = new CryptoStream(memo, rc.CreateDecryptor(), CryptoStreamMode.Write);
Cry.Write(rcdata, 0, rcdata.Length);
Cry.FlushFinalBlock();
Rst = Encoding.Default.GetString(memo.ToArray());
}
catch
{
Rst = null;
}
return Rst;
}
};