create account

How to encrypt information in custom_json [episode 1] by the.journal

View this thread on: hive.blogpeakd.comecency.com
· @the.journal ·
$4.16
How to encrypt information in custom_json [episode 1]
Custom json operations are used every day from dapp to manage informations about their users and to store them in a decentralized way in the `steem` or `hive' blockchains.

In my opinion this is a very powerful tool that these blockchains and the new `blurt` as well, to implement a new way to manage users in a public ledger, reducing back end costs, like database management and server expenses.

Thinking about this concept I stumble upon a problem, `what if you wish to store in the public blockchain sensible data` like users email for example.

The solution is to encrypt the informations and in order to hide them while saving them in the blockchain.

To solve this problem I wrote a few lines of python code as a proof of concept.

At the beginning we need to import the libraries to work with:

```python
from beem import Steem
from beem.account import Account
from cryptography.fernet import Fernet
```
The last line `from cryptography.fernet import Fernet`, is our way to hide the info in the blockchain.

Now lets create with `Fernet` our key to be able to encrypt and decrypt the info.

```python
def generate_key():
    key = Fernet.generate_key()
    with open("secret.key", "wb") as key_file:
        key_file.write(key)
```
In this case the key is saved in the same directory of the code in a file named `secret.key`.

Let's pass the information about the user that is going to broadcast the `custom_json` operation in the blockchain, for this example we are using the `steem` blockchain but the same thing can be done on `hive` with the same code.

```language
stm = Steem("https://api.steemit.com", keys=['YOUR POSTING KEY'])
acc ="YOUR ACCOUNT NAME"
```
Now let's broadcast the `custom_json`

```python
def transaction():
    key=load_key()
    secret='super secret message'.encode()
    fer=Fernet(key)
    secret=fer.encrypt(secret)
    secret=secret.decode('utf_8')
    data = {"secret":secret, "version" : "0.01"}
    stm.custom_json("test_DM", data, required_posting_auths=[acc])
transaction()
```
As you can see this operation required the `posting authority` so it is necessary to use just the posting key.
The message to encrypt is: `super secret message`

Here you can check it in the `steem` blockchain:

txid: https://steemworld.org/block/44871043/ab5ce3554d2815121d5369dd75fd632871f56113?login=digitalmine.app

![test_DM.png](https://files.peakd.com/file/peakd-hive/the.journal/0PnIe3iu-test_DM.png)

AS you can see the message is encrypted and just who have the key can read the content.

In the next episode I'll write bout how to actually decode the message and read its content.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorthe.journal
permlinkhow-to-encrypt-information-in-customjson-episode-1
categoryblockchain
json_metadata{"app":"peakd/2020.07.1","format":"markdown","tags":["blockchain","steem","hive","python","develpment","developer","code","json"],"links":["https://steemworld.org/block/44871043/ab5ce3554d2815121d5369dd75fd632871f56113?login=digitalmine.app"],"image":["https://files.peakd.com/file/peakd-hive/the.journal/0PnIe3iu-test_DM.png"]}
created2020-07-06 13:50:18
last_update2020-07-06 13:50:18
depth0
children1
last_payout2020-07-13 13:50:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value4.156 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,614
author_reputation5,799,246,878,950
root_title"How to encrypt information in custom_json [episode 1]"
beneficiaries
0.
accountdigital.mine
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,372,289
net_rshares20,949,351,203,900
author_curate_reward""
vote details (65)
@gitplait-mod1 ·
Thanks for sharing an other amazing tutorial. We are looking for people like you in our community. 

<sub> Your post has been submitted to be curated with @gitplait community account because this is the kind of publications we like to see in our community. </sub>

Join our [Community on Hive](https://hive.blog)
properties (22)
authorgitplait-mod1
permlinkqd1w59
categoryblockchain
json_metadata{"users":["gitplait"],"links":["https://hive.blog"],"app":"hiveblog/0.1"}
created2020-07-06 13:58:21
last_update2020-07-06 13:58:21
depth1
children0
last_payout2020-07-13 13:58:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length312
author_reputation64,455,719,431
root_title"How to encrypt information in custom_json [episode 1]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,372,406
net_rshares0