create account

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

View this thread on: hive.blogpeakd.comecency.com
· @the.journal ·
$3.48
How to encrypt information in custom_json [episode 2]
Continuation of this post:

- https://hive.blog/blockchain/@the.journal/how-to-encrypt-information-in-customjson-episode-1

Now we are going to retrieve the encrypted message from the blockchain, and decrypt it for our usage.

The script I'm going to use is in `python` like the previous one, and it will parse each single block to check if there is a custom_json with our id in order to get the message. so let's start to import the libraries 

```python
from beem.blockchain import Blockchain
from beem import Steem
from beem.nodelist import NodeList
import re
from cryptography.fernet import Fernet
nodelist = NodeList()
nodelist.update_nodes()
```

After that we need to load the `Fernet` key we saved before in order to decrypt the message. 

```python
def load_key():
    """
    Loads the key named `secret.key` from the current directory.
    """
    k=open("secret.key", "rb").read()
    return k
key = load_key()
f = Fernet(key)
```

Let's start the blockchain instance for this example we will use the `steem` blockchain, but it works as well for the `hive` one.

```python
stm = Steem(node=nodelist.get_steem_nodes())
b = Blockchain(blockchain_instance=stm)
```

Now let's start an infinite loop to keep check the blocks, and print out the message once found in the blockchain.

```python
for i in b.blocks(start=b.get_current_block_num()):
    print (i['id'])
    for t in i['transactions']:
        for o in t['operations']:
            if o['type']=='custom_json_operation':
                if o['value']['id']=='test_DM':
                    enc= re.findall('"type":"(.+?)"',o['value']['json'])[0]
                    ms=enc.encode('utf_8')
                    decrypted_message = str(f.decrypt(ms))
                    print (decrypted_message.replace("b'",'').replace("'",''))
```
Normally it takes around a minute from the custom_json publication to get discovered in the blockchain.
👍  , , , , , , ,
properties (23)
authorthe.journal
permlinkhow-to-encrypt-information-in-customjson-episode-2
categoryhive
json_metadata{"app":"peakd/2020.07.1","format":"markdown","tags":["hive","steem","blockchain","development","code","python","tutorial"],"users":["the.journal"],"links":["https://hive.blog/blockchain/@the.journal/how-to-encrypt-information-in-customjson-episode-1"]}
created2020-07-07 22:08:33
last_update2020-07-07 22:08:33
depth0
children1
last_payout2020-07-14 22:08:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value3.484 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,903
author_reputation5,799,246,878,950
root_title"How to encrypt information in custom_json [episode 2]"
beneficiaries
0.
accountdigital.mine
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,397,259
net_rshares18,320,880,092,028
author_curate_reward""
vote details (8)
@gitplait-mod1 ·
Thanks for sharing anorher tutorial nice tutorial about emcryption in python. Love to see that you share the link of part 1. It will be easier for python enthusiast to explore your nice efforts.

<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
permlinkqd5b2o
categoryhive
json_metadata{"users":["gitplait"],"links":["https://hive.blog"],"app":"hiveblog/0.1"}
created2020-07-08 10:13:36
last_update2020-07-08 10:13:36
depth1
children0
last_payout2020-07-15 10:13:36
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_length407
author_reputation64,455,719,431
root_title"How to encrypt information in custom_json [episode 2]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,405,078
net_rshares0