C#中 加密 解密问题,高手快来帮忙啊!!

jk52018859 2009-12-26 03:32:27
具体的要求这样的:加密器A 和 解密器B分开的,也就是说是两个exe运行程序~ (两个程序之间没有依赖性,单独运行的那种),我做的程序之间存在依赖,必须先运行A,才能运行B;
加密程序A:用户名和密码录入并加密
解密程序B:读取A加密后的文件key.data,并显示用户名和密码;
最好有详细的代码,谢谢了!!
---用C#语言吧,我是新手,不太懂其它语言的!!
...全文
179 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
open666111 2009-12-29
  • 打赏
  • 举报
回复
这个是很简单的方法啊 调用下就可以用了哦
public String md5(String s)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
bytes = md5.ComputeHash(bytes);
md5.Clear();

string ret = "";
for (int i = 0; i < bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}

return ret.PadLeft(32, '0');
}
bdx808 2009-12-28
  • 打赏
  • 举报
回复
好好学习
yulinlover 2009-12-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 floud4 的回复:]
不会连文件读取和写入都不会吧?

楼主貌似完全的懒人一个。
[/Quote]
绝对是,这随便一查都能找到,非得散点分,咱们就笑纳了。呵呵。
a569673493 2009-12-27
  • 打赏
  • 举报
回复
学习
Teng_s2000 2009-12-27
  • 打赏
  • 举报
回复
周末晚上上来看看热闹吧
Jave.Lin 2009-12-27
  • 打赏
  • 举报
回复
Passing
Mark
Study
Up
Jave.Lin 2009-12-27
  • 打赏
  • 举报
回复
Passing
Mark
Study
Up
南山猛士 2009-12-27
  • 打赏
  • 举报
回复
厉害呀,学习了,我也该好好学了。
floud4 2009-12-27
  • 打赏
  • 举报
回复
不会连文件读取和写入都不会吧?

楼主貌似完全的懒人一个。
nixiang12 2009-12-27
  • 打赏
  • 举报
回复
围观你们的
风之影子 2009-12-27
  • 打赏
  • 举报
回复
对XML或是TXT进行加解密

#region 对文件进行加密解密
static string iv = "password";
static string key = "password";

/// <summary>
/// DES加密偏移量,必须是>=8位长的字符串
/// </summary>
public static string IV
{
get { return iv; }
set { iv = value; }
}

/// <summary>
/// DES加密的私钥,必须是8位长的字符串
/// </summary>
public static string Key
{
get { return key; }
set { key = value; }
}

/// <summary>
/// 对文件内容进行DES加密
/// </summary>
/// <param name="sourceFile">待加密的文件绝对路径</param>
/// <param name="destFile">加密后的文件保存的绝对路径</param>
public static void EncryptFile(string sourceFile, string destFile)
{

if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);

byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(sourceFile);


using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}
}
catch
{
throw;
}
finally
{
fs.Close();
}
}
}

/// <summary>
/// 对文件内容进行DES加密,加密后覆盖掉原来的文件
/// </summary>
/// <param name="sourceFile">待加密的文件的绝对路径</param>
public void EncryptFile(string sourceFile)
{
EncryptFile(sourceFile, sourceFile);
}

/// <summary>
/// 对文件内容进行DES解密
/// </summary>
/// <param name="sourceFile">待解密的文件绝对路径</param>
/// <param name="destFile">解密后的文件保存的绝对路径</param>
public static void DecryptFile(string sourceFile, string destFile)
{

if (!File.Exists(sourceFile))
throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);

byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(sourceFile);


using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}
}
catch
{
throw;
}
finally
{
fs.Close();
}
}
}

/// <summary>
/// 对文件内容进行DES解密,解密后覆盖掉原来的文件
/// </summary>
/// <param name="sourceFile">待解密的文件的绝对路径</param>
public static void DecryptFile(string sourceFile)
{
DecryptFile(sourceFile, sourceFile);
}

#endregion


读写加密的XML或TXT

#region 读取及操作assembly(XML文件)
public static void SaveXml(string ConnenctionString, string strKey)//写入动态的数据库配置信息
{
CommonClass.DecryptFile(FilePath);
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Application.StartupPath+"\\assembly.xml";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == strKey)
{
//对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
att.Value = ConnenctionString;
break;
}
}
doc.Save(strFileName);
CommonClass.EncryptFile(FilePath, FilePath);
}

public static string ReadXml(string strKey)//写入动态的数据库配置信息
{
CommonClass.DecryptFile(FilePath);
string TempValues = "";
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Application.StartupPath + "\\assembly.xml";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == strKey)
{
att = nodes[i].Attributes["value"];
TempValues = att.Value;
break;
}
}
doc.Save(strFileName);
CommonClass.EncryptFile(FilePath, FilePath);
return TempValues;
}

public static SqlConnection GetXmlConn()//写入动态的数据库配置信息
{
CommonClass.DecryptFile(FilePath);
SqlConnection Conn;
string tempDataBase = "", tempServerIP = "", tempUser = "", tempPassword = "", tempStr = "" ;
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Application.StartupPath + "\\assembly.xml";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == "ServerIP")
{
tempServerIP = nodes[i].Attributes["value"].Value;
}
else if (att.Value == "DataBase")
{
tempDataBase = nodes[i].Attributes["value"].Value;
}
else if (att.Value == "User")
{
tempUser = nodes[i].Attributes["value"].Value;
}
else if (att.Value == "Password")
{
tempPassword = nodes[i].Attributes["value"].Value;
}
}
doc.Save(strFileName);
tempStr = "uid=" + tempUser + ";pwd=" + tempPassword;
tempStr += ";initial catalog=" + tempDataBase + ";Server=" + tempServerIP + ";";
tempStr += "Connect Timeout=30";
Conn = new SqlConnection(tempStr);
CommonClass.EncryptFile(FilePath, FilePath);
return Conn;
}

#endregion
jk52018859 2009-12-27
  • 打赏
  • 举报
回复
大哥们,加密后是要保存到一个文件中去的(Key.data),然后在另一个exe运行程序中读取这个Key.data文件,解密出来,一楼说的这些加密解密方法,好像只是对字符串的加解密,并没有从Key.data文件中读取...
floud4 2009-12-26
  • 打赏
  • 举报
回复
我做过一个基本的加解密算法,当然是非可逆的加解密算法。
思路如下:

加密:
1:根据密码key作为种子,生成伪随机序列md5字符串,与正文做数学运算。
2:对这个字符串做一个固定的加密操作
3:保存2进制文件

解密:
1:读取2进制文件
2:对文件进行相反的解密操作
3:根据输入的key作为种子,生成伪随机序列md5字符串,与正文做相反数学运算。
Comforter 2009-12-26
  • 打赏
  • 举报
回复
一楼方法不错,学习了
ProjectDD 2009-12-26
  • 打赏
  • 举报
回复
路过学习。。
hhhh63 2009-12-26
  • 打赏
  • 举报
回复
不知道你是不会加密,还是不会读写文件。
shibinysy 2009-12-26
  • 打赏
  • 举报
回复
加密有很多种方法.同时C#也提供了很多加密方法.你需要加密和解密在加密算法上需要使用对称加密.我给你提供一个简单的算法.在保存的时候可以使用二进制的方式来保存到文件.
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace Tools
{
/// <summary>
/// 加密
/// </summary>
public class AES
{
//默认密钥向量
private static byte[] Keys = { 0x18, 0x72, 0x65, 0x79, 0x6F, 0x57, 0x6D, 0x79, 0x35, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
/// <summary>
/// 设置加密密匙向量
/// </summary>
public static byte[] IV
{
set { Keys = value; }
get { return Keys; }
}
/// <summary>
/// AES加密数据
/// </summary>
/// <param name="encryptString">需要加密的数据</param>
/// <param name="encryptKey">加密密匙</param>
/// <returns>加密后的数据</returns>
public static string Encode(string encryptString, string encryptKey)
{
encryptKey = Utils.GetSubString(encryptKey, 32, "");
encryptKey = encryptKey.PadRight(32, ' ');

RijndaelManaged rijndaelProvider = new RijndaelManaged();
rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
rijndaelProvider.IV = IV;
ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();

byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);

return Convert.ToBase64String(encryptedData);
}

/// <summary>
/// AES解密
/// </summary>
/// <param name="decryptString">需要解密的数据</param>
/// <param name="decryptKey">加密时用的加密密匙</param>
/// <returns>解密后的数据</returns>
public static string Decode(string decryptString, string decryptKey)
{
try
{
decryptKey = Utils.GetSubString(decryptKey, 32, "");
decryptKey = decryptKey.PadRight(32, ' ');

RijndaelManaged rijndaelProvider = new RijndaelManaged();
rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey);
rijndaelProvider.IV = IV;
ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();

byte[] inputData = Convert.FromBase64String(decryptString);
byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);

return Encoding.UTF8.GetString(decryptedData);
}
catch
{
return "";
}

}

}

/// <summary>
/// 加密
/// </summary>
public class DES
{
//默认密钥向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

/// <summary>
/// 设置加密密匙向量
/// </summary>
public static byte[] IV
{
set { Keys = value; }
get { return Keys; }
}

/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string Encode(string encryptString, string encryptKey)
{
encryptKey = Utils.GetSubString(encryptKey, 8, "");
encryptKey = encryptKey.PadRight(8, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = IV;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());

}

/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string Decode(string decryptString, string decryptKey)
{
try
{
decryptKey = Utils.GetSubString(decryptKey, 8, "");//"";//
decryptKey = decryptKey.PadRight(8, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = IV;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();

MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return "";
}
}
}

/// <summary>
/// MD5加密
/// </summary>
public class MD5EnCode
{
public static string Encode(string Pwd)
{

MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = System.Text.Encoding.Default.GetBytes(Pwd );
byte[] result = md5.ComputeHash(data);
String ret = "";
for (int i = 0; i < result.Length; i++)
ret += result[i].ToString("x").PadLeft(2, '0');
return ret;

}
}
}


上面是一个完整的加密和解密算法.有多种加密方式.楼主可以自己看.保存到文件的那就很简单了.我就不写了.

111,120

社区成员

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

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

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