这是他人论文中的一个加密程序,谁能帮忙调试下!!谢谢using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {            DSACryptoServiceProvider dsaCode = new DSACryptoServiceProvider();
            string publicKey = dsaEncode.ToXmlString(false);
            string privateKey = dsaEncode.ToXmlString(true);
            //将公钥及私钥分别保存至publicKey.bin及privateKey.bin文件中
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new
            FileStream("publicKey.bin", FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, publicKey);
            //使用私钥对提案内容数据的哈希值进行加密并生成数字签名         
            objdsa.FromXmlString(privateKey);
            byte[] sourceContent = System.Text.UTF8Encoding.UTF8.GetBytes(caseContent);
            //使用私钥对提案内容数据生成的哈希值进行加密并生成数字签名    
            string signDataVal = BitConverter.ToString(objdsa.SignData(sourceContent));
            //获取接收到的数据的Hash值
            byte[] fileHashValue = new SHA1CryptoServiceProvider().ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(caseContent));
            //获取接收到的数字签名
            string[] strTemp = signDataVal.Split('-');
            byte[] signedDataHash = new byte[strTemp.Length];
            for (int i = 0; i < strTemp.Length; i++)
                signedDataHash[i] = byte.Parse(strTemp[i], System.Globalization.NumberStyles.AllowHexSpecifier);
            dsaCode.FromXmlString(publicKey);
            //验证数据签名是否正确
            bool signIsValid = dsaCode.VerifySignature(fileHashValue, signedDataHash);
        }
    }
}下面是说明:
.Net Framework框架中的System. Security.
Cryptography 命名空间下提供了一系列加密解密相关函数,可以非常方便迅速地完成加密解密及数字签名等功能,因此我们可以用.Net Framework框架中的C#语言,通过一个实例来说明数字签名创建与验证过程。主要代码步骤实现如下(对于相似功能代码不重复给出):
(1) 生成用户的公开密钥和私人密钥对
//使用DSA 算法生生成公钥publicKey及私钥privateKey
DSACryptoServiceProvider dsaCode = new DSACryptoServiceProvider();
string publicKey = dsaEncode.ToXmlString(false);
string privateKey= = dsaEncode.ToXmlString(true);
//将公钥及私钥分别保存至publicKey.bin及privateKey.bin文件中
IFormatter formatter = new BinaryFormatter();
Stream stream = new 
FileStream("publicKey.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, publicKey);
//保存私钥密码同上,略…
上述代码运行之后将会在当前目录生成两个
.bin二进制文件
(2) 使用私钥(privateKey)生成数字签名。
//使用私钥对提案内容数据的哈希值进行加密并生成数字签名         objdsa.FromXmlString(privateKey);
byte[] sourceContent = System.Text.UTF8Encoding.UTF8.GetBytes(caseContent); 
//使用私钥对提案内容数据生成的哈希值进行加密并生成数字签名    
string signDataVal = BitConverter.ToString(objdsa.SignData(sourceContent));
(3)  使用公钥验证数字签名是否正确:
//获取接收到的数据的Hash值
byte[] fileHashValue = new SHA1CryptoServiceProvider().ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(caseContent));
//获取接收到的数字签名
string[] strTemp = signDataVal.Split(‘-‘);
     byte[] signedDataHash = new byte[strTemp.Length];
for (int i = 0; i < strTemp.Length; i++)
signedDataHash [i] = byte.Parse(strTemp[i], System.Globalization.NumberStyles.AllowHexSpecifier);
dsaCode.FromXmlString(publicKey);
//验证数据签名是否正确
bool signIsValid = dsaCode.VerifySignature(fileHashValue, signedDataHash);

解决方案 »

  1.   

    调试出下面的几个错误!
    错误 1 当前上下文中不存在名称“dsaEncode” D:\我的文档\Visual Studio 2005\Projects\mima\mima\Program.cs 16 32 mima
    错误 2 当前上下文中不存在名称“dsaEncode” D:\我的文档\Visual Studio 2005\Projects\mima\mima\Program.cs 17 33 mima
    错误 3 当前上下文中不存在名称“objdsa” D:\我的文档\Visual Studio 2005\Projects\mima\mima\Program.cs 24 13 mima
    错误 4 当前上下文中不存在名称“caseContent” D:\我的文档\Visual Studio 2005\Projects\mima\mima\Program.cs 25 75 mima
    错误 5 当前上下文中不存在名称“objdsa” D:\我的文档\Visual Studio 2005\Projects\mima\mima\Program.cs 27 56 mima
    错误 6 当前上下文中不存在名称“caseContent” D:\我的文档\Visual Studio 2005\Projects\mima\mima\Program.cs 29 119 mima