VS2013
using System;using System.IO;
using System.Security.Cryptography;
namespace oozinoz1
{
/**//// <summary>
/// Class5 的摘要说明。
/// </summary>
public class Class5
{
static void Main()
{
// 生成电子文件
string filePath = "C:\\公文.txt";
StreamWriter sw = File.CreateText(filePath);
sw.Write("测试公文");
sw.Close();
// 对电子文件进行哈希
byte[] fileHash = GetFileHash(filePath);
// 取得公钥
string publicKey = GetKeyFromContainer("公文",false);
// 取得私钥
string privateKey = GetKeyFromContainer("公文",true);
Console.WriteLine("发送方:A");
Console.WriteLine("电子文件地址:");
Console.WriteLine(filePath);
Console.WriteLine("哈希:");
Console.WriteLine(ConvertBytesToString(fileHash));
Console.WriteLine("使用私钥进行数字签名:");
// 取得电子签名
byte[] ElectronicSignature = EncryptHash(privateKey,fileHash);
Console.WriteLine(ConvertBytesToString(ElectronicSignature));
Console.WriteLine("传送给接收方。");
Console.WriteLine("");
string fileCopyPath = "C:\\公文接收.txt";
File.Copy(filePath,fileCopyPath,true);
Console.WriteLine("是否篡改文件?(Y/N)");
string sRe = string.Empty;
do
{
sRe = Console.ReadLine();
}
while(sRe != "Y" && sRe != "N");
byte[] fileCopyHash;
if(sRe == "N")
{
Console.WriteLine("接收方收到电子文件。");
Console.WriteLine("接收方:B");
Console.WriteLine("接收文件地址:");
Console.WriteLine(fileCopyPath);
fileCopyHash = GetFileHash(fileCopyPath);
Console.WriteLine("哈希:");
Console.WriteLine(ConvertBytesToString(fileCopyHash));
}
else
{
Console.WriteLine("正在篡改文件。");
sw = new StreamWriter(fileCopyPath);
sw.WriteLine("公文已被篡改。");
sw.Close();
Console.WriteLine("接收方收到电子文件。");
Console.WriteLine("接收方:");
Console.WriteLine("接收文件地址:");
Console.WriteLine(fileCopyPath);
fileCopyHash = GetFileHash(fileCopyPath);
Console.WriteLine("哈希:");
Console.WriteLine(ConvertBytesToString(fileCopyHash));
}
Console.WriteLine("公钥:");
Console.WriteLine(publicKey);
//使用公钥进行验证
Console.WriteLine("使用公钥进行验证:");
if(DecryptHash(publicKey,fileCopyHash,ElectronicSignature))
{
Console.WriteLine("通过验证,电子文件合法有效。");
}
else
{
Console.WriteLine("未通过验证,电子文件非法或被人篡改过。");
}
Console.Read();
}
/**//// <summary>
/// 将Byte[]转换成十六进制字符串
/// </summary>
/// <param name="bytes">要转换的Byte[]</param>
/// <returns>十六进制字符串</returns>
public static string ConvertBytesToString(byte[] bytes)
{
string bytestring = string.Empty;
if(bytes != null && bytes.Length > 0)
{
for(int i = 0;i<bytes.Length;i++)
{
bytestring += bytes.ToString("X") + " ";
}
}
return bytestring;
}
/**//// <summary>
/// 得到指定电子文件的哈希
/// </summary>
/// <param name="filePath">电子文件地址</param>
/// <returns>哈希值</returns>
public static byte[] GetFileHash(string filePath)
{
try
{
FileStream objFile = File.OpenRead(filePath);
HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
byte[] Hashbyte = MD5.ComputeHash(objFile);
objFile.Close();
return Hashbyte;
}
catch
{
return null;
}
}
/**//// <summary>
/// 得到公钥与私钥
/// </summary>
/// <param name="ContainerName">私钥容器名</param>
/// <param name="privatekey">真为得到私钥,假为得到公钥</param>
/// <returns>公钥或私钥</returns>
public static string GetKeyFromContainer(string ContainerName,bool privatekey)
{
CspParameters cp = new CspParameters();
cp.KeyContainerName = ContainerName;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
return rsa.ToXmlString(privatekey);
}
/**//// <summary>
/// 对哈希进行数字签名
/// </summary>
/// <param name="privateKey">私钥</param>
/// <param name="fileHash">电子文件哈希</param>
/// <returns></returns>
public static byte[] EncryptHash(string privateKey,byte[] fileHash)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(privateKey);
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
RSAFormatter.SetHashAlgorithm("MD5");
return RSAFormatter.CreateSignature(fileHash);
}
/**//// <summary>
/// 对数字签名用公钥进行验证
/// </summary>
/// <param name="publicKey">公钥</param>
/// <param name="fileHash">接收到的电子文件的哈希</param>
/// <param name="electronicSignature">数字签名</param>
/// <returns>数字签名有效为真,数字签名无效为假</returns>
public static bool DecryptHash(string publicKey,byte[] fileHash,byte[] electronicSignature)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(publicKey);
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
RSADeformatter.SetHashAlgorithm("MD5");
return RSADeformatter.VerifySignature(fileHash,electronicSignature);
}
}
谁能 帮我运行运行 看看这段代码的 实现了个啥 你的题目和内容还有问题{:10_277:}{:10_277:}
看着像是C# Hello. 发表于 2020-5-4 11:56
你的题目和内容还有问题
看着像是C#
我不知道VS2013里面的 咋用 litianyang 发表于 2020-5-4 12:05
我不知道VS2013里面的 咋用
我没懂你想说啥{:10_277:} 不明白你在问什么 永恒的蓝色梦想 发表于 2020-5-4 12:21
不明白你在问什么
能帮我运行一遍吗{:5_109:}我不会用 软件 Hello. 发表于 2020-5-4 12:18
我没懂你想说啥
用VS怎么运行 我不会 用VS的c#
页:
[1]