前言
为了帮助同学们完成痛苦的实验课程设计,本作者将其作出的实验结果及代码贴至CSDN中,供同学们学习参考。如有不足或描述不完善之处,敬请各位指出,欢迎各位的斧正!
一、实验目的
1、掌握RSA签名的工作原理。
2、熟悉.NET Framework 4 数字签名编程
二、实验环境
Microsoft Visual Studio 2019
三、实验内容及步骤
1.编程实现找素数的算法。
2.编程实现找RSA参数的计算程序。
3.编写一个RSA算法;
4.对2个字符加、解密,改变最低1位明文观察并记录RSA的输出。
5.对2个字符加、解密,改变最高1位明文观察并记录RSA的输出。
6.使用VS平台,以framework为基础,编写RSA加解密程序,观测密钥容器、密钥的产生、导出和导入
四、实验基本方法
1.用C#编写文件的RSA签名计算;
2.用RSA签名进行文件完整性校验;
五、实验及实验结果分析
1.分析程序关键原代码。
2.分析用RSA签名进行文件完整性校验。
六、实验程序清单
源代码修改如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace SecTest
{
public partial class RSASV : Form
{
public string str1;
public string MFileNamestr1;
public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
{
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder strB = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
strB.Append(bytes[i].ToString("X2"));
}
hexString = strB.ToString();
}
return hexString;
}
public RSASV()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var cspPas = new CspParameters();
cspPas.KeyContainerName = "rsa_key";
RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider(cspPas);
RSA1.PersistKeyInCsp = false;
RSA1.Clear();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspPas);
string str_Public_Key;
string str_Private_Key;
str_Public_Key = "";
str_Private_Key = "";
str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
textBox1.Text = str_Public_Key;
textBox2.Text = str_Private_Key;
FileStream fs1 = new FileStream("E:\\学习\\学习\\密码学\\密码学实验\\第五次\\SecVer\\RsaKey1.dat", FileMode.Create, FileAccess.Write);
string key = RSA.ToXmlString(false);
fs1.Write(Encoding.UTF8.GetBytes(key), 0, key.Length);
fs1.Close();
fs1.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
var cspPas = new CspParameters();
cspPas.KeyContainerName = "rsa_key";
RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider(cspPas);
string str_Public_Key;
string str_Private_Key;
str_Public_Key = "";
str_Private_Key = "";
str_Public_Key = Convert.ToBase64String(RSA1.ExportCspBlob(false));
str_Private_Key = Convert.ToBase64String(RSA1.ExportCspBlob(true));
textBox1.Text = str_Public_Key;
textBox2.Text = str_Private_Key;
}
private void button3_Click(object sender, EventArgs e)
{
var cspPas = new CspParameters();
cspPas.KeyContainerName = "rsa_key";
RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider(cspPas);
SHA1 sh = new SHA1CryptoServiceProvider();
if (textBox3.Text == "")
return;
FileStream fin = new FileStream(textBox3.Text, FileMode.Open, FileAccess.Read);
byte[] Data = new byte[fin.Length];
try
{
fin.Read(Data, 0, Data.Length);
fin.Seek(0, SeekOrigin.Begin);
}
catch
{
}
finally
{
if (fin != null)
fin.Close();
}
byte[] signData = RSA1.SignData(Data, sh);
textBox4.Text =Convert.ToBase64String(signData);
}
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox3.Text = openFileDialog.FileName;
MFileNamestr1 = textBox3.Text;
}
}
private void button5_Click(object sender, EventArgs e)
{
bool result;
FileStream fin = new FileStream(textBox3.Text, FileMode.Open, FileAccess.Read);
byte[] Data = new byte[fin.Length];
try
{
fin.Read(Data, 0, Data.Length);
fin.Seek(0, SeekOrigin.Begin);
}
catch
{
}
finally
{
if (fin != null)
fin.Close();
}
FileStream fkeyin = new FileStream("E:\\学习\\学习\\密码学\\密码学实验\\第五次\\SecVer\\RsaKey1.dat", FileMode.Open, FileAccess.Read);
long keyL=fkeyin.Length;
byte[] key_buf = new byte[keyL];
fkeyin.Read(key_buf, 0, (int)keyL);
string strPublicKey;
strPublicKey = System.Text.Encoding.Default.GetString(key_buf);
RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider();
RSA1.FromXmlString(strPublicKey);
byte[] data = Convert.FromBase64String(textBox4.Text);
SHA1 sh = new SHA1CryptoServiceProvider();
result = RSA1.VerifyData(Data, sh, data);
if (result)
textBox5.Text = "验证通过。";
else
textBox5.Text = "验证失败。";
}
}
}
七、实验结果
生成并保存密钥:
读取并显示密钥:
更新密钥
读取并显示密钥
选择被签的文件:
签字:
验证签字:
扫描二维码关注公众号,回复:
17285801 查看本文章