MSSQL数据库连接密码加密

MSSQL数据库连接密码加密,第1张

#region DES加密字符串

/// <summary>

/// 加密字符串

/// 注意:密钥必须为8位

/// </summary>

/// <param name="strText">字符串</param>

public string DesEncrypt(string strText)

{

byte[] byKey = null

byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }

try

{

string encryptKey = "XX_XX_XX" //密钥

byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey)

DESCryptoServiceProvider des = new DESCryptoServiceProvider()

byte[] inputByteArray = Encoding.UTF8.GetBytes(strText)

MemoryStream ms = new MemoryStream()

CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)

cs.Write(inputByteArray, 0, inputByteArray.Length)

cs.FlushFinalBlock()

return Convert.ToBase64String(ms.ToArray())

}

catch

{

}

return strText

}

#endregion

#region DES解密字符串

/// <summary>

/// 解密字符串

/// </summary>

/// <param name="inputString">加了密的字符串</param>

public string DesDecrypt(string inputString)

{

byte[] byKey = null

byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }

byte[] inputByteArray = new Byte[inputString.Length]

try

{

string decryptKey = "XX_XX_XX"

byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8))

DESCryptoServiceProvider des = new DESCryptoServiceProvider()

inputByteArray = Convert.FromBase64String(inputString)

MemoryStream ms = new MemoryStream()

CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)

cs.Write(inputByteArray, 0, inputByteArray.Length)

cs.FlushFinalBlock()

System.Text.Encoding encoding = new System.Text.UTF8Encoding()

return encoding.GetString(ms.ToArray())

}

catch

{

}

return inputString

}

#endregion

===这个是DES方式加密解密字符串的

System.Security.Cryptography.MD5CryptoServiceProvider md5=new System.Security.Cryptography.MD5CryptoServiceProvider()

return Convert.ToBase64String(md5.ComputeHash(System.Text.Encoding.Unicode.GetBytes(yourstring)))

===这个是MD5的

实现代码也给你写好了,希望能对你有帮助

看情况,逻辑上加密是好一点,因为存在安全性问题,数据库常见的安全问题就是注入攻击,建议还是做一下加密,而加密分为对称加密和非对称加密。不管哪种加密方式,都或多或少对其他不良目的起到一定防御作用。


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/478784.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-06-08
下一篇2023-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存