using System;using System.IO;using System.Text;using System.Security.Cryptography;public partial class UserdefinedFunctions{ //加密 [Microsoft.sqlServer.Server.sqlFunction] public static string f_encrypt(string str) { string output = ""; String Key = @")[FDf0_gefcaj-+oEfgD8<Kl'e9lju"; //密钥 Byte[] bKey = new Byte[32]; byte[] byteArray = EnCoding.UTF8.GetBytes(str); Array.copy(EnCoding.UTF8.GetBytes(Key.padright(bKey.Length)),bKey,bKey.Length); RijndaelManaged aes = new RijndaelManaged(); aes.Mode = CipherMode.ECB; aes.padding = paddingMode.PKCS7; aes.KeySize = 128; aes.Key = bKey; MemoryStream MenStream = new MemoryStream(); CryptoStream CryStream = new CryptoStream(MenStream,aes.CreateEncryptor(),CryptoStreamMode.Write); try { CryStream.Write(byteArray,byteArray.Length); CryStream.FlushFinalBlock(); output = Convert.ToBase64String(MenStream.ToArray()); } catch { } finally { CryStream.Close(); MenStream.Close(); aes.Clear(); } return output; } //解密 [Microsoft.sqlServer.Server.sqlFunction] public static string f_decrypt(string str) { string output = ""; String Key = @")[FDf0_gefcaj-+oEfgD8<Kl'e9lju"; //密钥 Byte[] bKey = new Byte[32]; Byte[] bStr = Convert.FromBase64String(str); Array.copy(EnCoding.UTF8.GetBytes(Key.padright(bKey.Length)),bKey.Length); RijndaelManaged aes = new RijndaelManaged(); aes.Mode = CipherMode.ECB; aes.padding = paddingMode.PKCS7; aes.KeySize = 128; aes.Key = bKey; MemoryStream MenStream = new MemoryStream(bStr); CryptoStream CryStream = new CryptoStream(MenStream,aes.CreateDecryptor(),CryptoStreamMode.Read); try { byte[] tmp = new byte[bStr.Length + 32]; int len = CryStream.Read(tmp,bStr.Length + 32); byte[] ret = new byte[len]; Array.copy(tmp,ret,len); output = EnCoding.UTF8.GetString(ret); } catch { } finally { CryStream.Close(); MenStream.Close(); aes.Clear(); } return output; }};总结
以上是内存溢出为你收集整理的SQLServer CLR 函数AES加密解密代码全部内容,希望文章能够帮你解决SQLServer CLR 函数AES加密解密代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)