Blockchain journal program

Junior data security course design.. I hope it can be helpful to your class or project ...
Read template:
Record template:

Junior data security course design.. I hope it can be helpful to your class or project

1, Title Requirements

The program can enter journal contents
The journal content needs to be encrypted
Ability to write encrypted data to blockchain
Able to read encrypted data on the chain
Decrypt the read encrypted data
Display in the program after decryption

Two. Template planning

Read template:

Record template:

3, Source code
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 diary { 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("The private key cannot be empty"); } 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("Network error!"); } 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("Invalid private key!"); } } 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("Network error!"); } 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("Sending failed!"); } 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) { } } }
4, Demo interface

The initial interface is shown in the figure

The robustness of this program has a certain guarantee. When the default private key is empty, it cannot be read and recorded

Invalid secret keys are also processed:

Find the private key of this account in the Electrum wallet, as shown in the figure:

After entering the corresponding private key, click read to read the information on the account chain. The information has been encrypted by Aes advanced encryption standard. When reading, the ciphertext is read and displayed in the output box in the upper right corner
After AES decryption, restore the corresponding plaintext in the output box in the upper left corner, as shown in the figure

At the same time, the console will display the history details of the corresponding transaction information:
For example, txid, hash value, block size and other information

When recording information locally, enter the information to be recorded in the input box in the lower left corner. In this example, take "you are the best" as an example. After entering, click the record button, and the program will automatically encrypt the plaintext. The encrypted content is shown in the lower right corner,
Then send the encrypted ciphertext to the chain, and send the recorded information locally to the plaintext output box in the upper left corner

After the uplink is completed, its block information is displayed in the console, as shown in the figure:

We can query the transaction on whatsonchain website according to the information provided by the console;

It can be seen that the transaction ciphertext is consistent with the content recorded on the website:

12 September 2021, 21:00 | Views: 2803

Add new comment

For adding a comment, please log in
or create account

0 comments