大三的数据安全课程设计。。希望能对你的课设或项目有所帮助
文章目录
- 一、题目要求
- 二、模板规划
- 读取模板:
- 记录模板:
- 三、源码
- 四、演示界面
一、题目要求
程序可以输入日记内容
需要将日记内容进行加密
能够将加密数据写入区块链
能够读取链上加密数据
读取的加密数据进行解密
解密后在程序中显示
二、模板规划
读取模板:
记录模板:
三、源码
using System;
using System.Collections.Generic;
using BsvSimpleLibrary;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NBitcoin;
using NBitcoin.DataEncoders;
using BitcoinSVCryptor;
namespace 日记
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text=="")
{
MessageBox.Show("私钥不能为空");
}
string privateKeyStr = textBox1.Text;
BitcoinSecret privateKey;
try
{
privateKey = new BitcoinSecret(privateKeyStr);
string network = "";
if (privateKey.Network == Network.TestNet)
{
network = bsvConfiguration_class.testNetwork;
}
else if (privateKey.Network == Network.Main)
{
network = bsvConfiguration_class.mainNetwork;
}
else
{
MessageBox.Show("网络错误!");
}
PubKey pubKey = privateKey.PubKey;
string pubkeyStr = pubKey.ToHex();
KeyId pkhash = pubKey.Hash;
string pkhashStr = pkhash.ToString();
BitcoinAddress address = pkhash.GetAddress(privateKey.Network);
string addressStr = address.ToString();
string addrStr = privateKey.PubKeyHash.GetAddress(privateKey.Network).ToString();
string destAddress = addressStr;
string uri = bsvConfiguration_class.RestApiUri;
Base58Encoder base58Encoder = new Base58Encoder();
byte[] cipherBase58Bytes = base58Encoder.DecodeData(richTextBox3.Text);
string plaintextStr = AES_class.AesDecrypt(cipherBase58Bytes, privateKeyStr);
richTextBox2.Clear();
richTextBox2.AppendText(plaintextStr + '\n');
Task<RestApiAddressHistoryTx[]> addrHistoryTask = Task.Run(() =>
{
RestApiAddressHistoryTx[] addrHistory2 = RestApi_class.getAddressHistory(uri, network, addrStr);
return addrHistory2;
});
addrHistoryTask.Wait();
RestApiAddressHistoryTx[] addrHistory = addrHistoryTask.Result;
List<string> txHashs = new List<string> { };
for (int i = 0; i < addrHistory.Length; i++)
{
txHashs.Add(addrHistory[i].TxHash);
}
Task<RestApiTransaction[]> transactionsTask = Task.Run(() =>
{
RestApiTransaction[] transactionsTemp = new RestApiTransaction[txHashs.Count];
for (int i = 0; i < txHashs.Count; i++)
{
transactionsTemp[i] = RestApi_class.getTransaction(uri, network, txHashs[i]);
}
return transactionsTemp;
});
transactionsTask.Wait();
RestApiTransaction[] transactions = transactionsTask.Result;
List<string> opReturnDataStrings = new List<string> { };
for (int i = 0; i < txHashs.Count; i++)
{
opReturnDataStrings.Add(RestApi_class.getOpReturnData
(transactions[i], bsvConfiguration_class.encoding));
}
Console.WriteLine();
foreach (var opReturnDataString in opReturnDataStrings)
{
if (opReturnDataString != null)
{
try
{
byte[] tempBase58Bytes = base58Encoder.DecodeData(opReturnDataString);
richTextBox4.AppendText(opReturnDataString + '\n');
string tempDecrypt = AES_class.AesDecrypt(tempBase58Bytes, privateKeyStr);
richTextBox1.AppendText(tempDecrypt + '\n');
}
catch (Exception)
{
richTextBox1.AppendText(opReturnDataString);
}
}
}
}
catch (Exception)
{
MessageBox.Show("私钥无效!");
}
}
private void button2_Click(object sender, EventArgs e)
{
string wifPrivateKeyStr = textBox1.Text;
BitcoinSecret privateKey = new BitcoinSecret(wifPrivateKeyStr);
string privateKeyStr = privateKey.ToString();
string plaintextStr = richTextBox2.Text;
byte[] aesEncryptedTextBytes = AES_class.AesEncrypt(plaintextStr, privateKeyStr);
Base58Encoder base58Encoder = new Base58Encoder();
string base58TextData = base58Encoder.EncodeData(aesEncryptedTextBytes);
string network = string.Empty;
if (privateKey.Network == Network.TestNet)
{
network = bsvConfiguration_class.testNetwork;
}
else if (privateKey.Network == Network.Main)
{
network = bsvConfiguration_class.mainNetwork;
}
else
{
MessageBox.Show("网络错误!");
}
string destAddress = privateKey.PubKeyHash.GetAddress(privateKey.Network).ToString();
try
{
Task<Dictionary<string, string>> sendTask = Task.Run(() =>
{
Dictionary<string, string> response = bsvTransaction_class.send(privateKeyStr, 0, network, destAddress, null, base58TextData, 0.5, 0);
return response;
});
sendTask.Wait();
}
catch (Exception)
{
MessageBox.Show("发送失败!");
}
richTextBox3.Clear();
richTextBox3.Text += base58TextData;
richTextBox1.AppendText(plaintextStr+'\n');
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox4_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox4_TextChanged_1(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
}
}
四、演示界面
初始界面如图所示
本程序的健壮性有一定的保证,在默认私钥为空时是不能读取和记录的
输入无效的秘钥也有处理:
在Electrum钱包中找到本账户的私钥,如图所示:
在输入相应私钥后,点击读取,读取本账户链上的信息,该信息是经过Aes高级加密标准加密过的,读取时读到的是密文,在右上角输出框中显示
经过AES解密后还原其相应的明文在左上角输出框,如图所示
同时,控制台中会显示相应交易信息的历史记录详细信息:
例如txid、哈希值、区块大小等信息
本地记录信息时在左下角的输入框中输入需要记录上链的信息,本例以”you are the best”为例,在输入完成后点击记录按钮,程序自动对明文进行加密,加密后的内容如右下角所示,
再将加密后的密文发送上链,同时在本地将记录的信息发送到左上角明文输出框中
上链完成后,其区块信息在控制台中显示如图:
我们可以在whatsonchain网站中根据控制台提供的信息查询交易;
可以看到本交易密文和网站上记录的内容一致: