求高手给出C#加密字符串的源代码

icefreezing 2010-12-16 09:40:21
主要实现为:C#如何加密字符串来存到数据库,然后将数据库加密的字符串解密后显示出来,这时就显示成未加密前的对应字符?请给出程序实现源代码,谢谢
...全文
65 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
机器人 2010-12-16
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;


namespace ConnStrEncry
{
public class Utils
{
public static void ProtectConnectionString()
{
ToggleConnectionStringProtection(System.Windows.Forms.Application.ExecutablePath, true);
}

public static void UnprotectConnectionString()
{
ToggleConnectionStringProtection(System.Windows.Forms.Application.ExecutablePath, false);
}

private static void ToggleConnectionStringProtection(string exePath, bool protect)
{
// Define the Dpapi provider name.
string strProvider = "DataProtectionConfigurationProvider";
// string strProvider = "RSAProtectedConfigurationProvider";

Configuration configuration = null;
ConnectionStringsSection section = null;

// Open the configuration file and retrieve
// the connectionStrings section.

// For Web!
// oConfiguration = System.Web.Configuration.
// WebConfigurationManager.OpenWebConfiguration("~");

// For Windows!
// Takes the executable file name without the config extension.
configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(exePath);

if (configuration != null)
{
bool blnChanged = false;
section = configuration.GetSection("connectionStrings") as ConnectionStringsSection;

if (section != null)
{
if ((!(section.ElementInformation.IsLocked)) && (!(section.SectionInformation.IsLocked)))
{
if (protect)
{
if (!(section.SectionInformation.IsProtected))
{
blnChanged = true;
// Encrypt the section.
section.SectionInformation.ProtectSection(strProvider);
}
}
else
{
if (section.SectionInformation.IsProtected)
{
blnChanged = true;
// Remove encryption.
section.SectionInformation.UnprotectSection();
}
}
}

if (blnChanged)
{
// Indicates whether the associated configuration section
// will be saved even if it has not been modified.
section.SectionInformation.ForceSave = true;

// Save the current configuration.
configuration.Save();
}
}
}

}


}
}


使用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConnStrEncry
{
class Program
{
static void Main(string[] args)
{
Utils.ProtectConnectionString();
string connstr = System.Configuration.ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
Console.WriteLine(connstr);
Console.Read();
}
}
}
wuyq11 2010-12-16
  • 打赏
  • 举报
回复
md5,des,sha等加密解密
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
http://topic.csdn.net/u/20100629/16/9bffaa4e-aada-4ab6-a161-b6800235380a.html
DataBox-MDX 2010-12-16
  • 打赏
  • 举报
回复
LZ用要实现具体的什么功能呢。
一般情况下(我说的一般情况下)我们加密完字符串就要它不能进行解密了,如果能解密,那么假设某天哪个人看到你的数据库,那么看到数据后也能进行解密。比如用户登入的密码,我们加密完就要使得该密码不能解密。那么每次在验证密码的时候都是应用程序重新进行加密然后和数据库的加密字符串匹配。相同则表示成功。

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧