Blockchain Tutorial

Blockchain Tutorial History of Blockchain Blockchain Terminologies Working of Blockchain Blockchain vs DLT Blockchain Versions Smart Contracts Blockchain Applications Cryptography Role of Bitcoin Miners Blockchain Hash Function Bitcoin Basic Component Blockchain Block Hashing How to Block Hashes Work in Blockchain Blockchain Pow Coinbase Transaction Key Concepts in Bitcoin Key Areas of Blockchain Blockchain Cryptocurrency Blockchain DAO Blockchain Double Spending Blockchain Bitcoin Cash Bitcoin Forks and SegWit Blockchain Merkle Tree Difference between Blockchain and Database Bitcoin Mitigating Attacks Who sets the Bitcoin Price Getting Started with Bitcoin How to choose Bitcoin Wallet Sending and Receiving Bitcoin Converting Bitcoins to Fiat Currency Ethereum 2.0 Blockchain Data Management Steps to become a Blockchain developer Smart Contracts Advantages of Blockchain in healthcare Decentralized Voting System using Blockchain Demur-rage currencies in Blockchain How can Blockchain Technology help IoT to reach its full potential Project Ideas on Blockchain for Professionals Consensus Algorithms in Blockchain Top 10 Blockchain Project Concepts Uses of Blockchain Obtaining Free Test Ethers What does a Blockchain contain What does the IT industry mean by BaaS Top Blockchain Project Ideas for Beginners

Cryptography

Introduction and Features of Cryptography DNA cryptography ECB Mode in Cryptography Elliptic curve in cryptography Format String Vulnerabilities in Cryptography and Network Security Kerberos in Cryptography and Network Security Blowfish Algorithm in Cryptography Data Encryption Standards Feistel Cipher in Cryptography HMAC Algorithm in Cryptography IP Security in Cryptography ElGamal Algorithm ElGamal Cryptosystem What is IDEA Advantages of Cryptography Role of Bitcoin Miners Blockchain Hash Function Blockchain Merkle Tree Blockchain Pow Coinbase Transactions Consensus Algorithms in Blockchain Converting Bitcoins to Fiat Currency Decentralized Voting System using Blockchain Demur-rage currencies in Blockchain Difference between Blockchain and Database Difference between Symmetric and Asymmetric Encryption Ethereum 2.0 Getting Started With Bitcoin How can Blockchain Technology help IoT to reach its full potential? How does Digital Signature Work Advantages of Blockchain in healthcare Basic Component of Bitcoin Bitcoin Forks and SegWit Bitcoin Mitigating Attacks Blockchain Bitcoin Cash Blockchain Block Hashing Blockchain Cryptocurrency Blockchain DAO Blockchain Data Management Blockchain Double Spending What does Blockchain contain? What does the IT industry mean by BaaS (Blockchain as a Service)? Who sets the Bitcoin Price? Working of Block Hashes in Blockchain How to Choose Bitcoin Wallet? Key Areas of Blockchain Key Concepts in Bitcoin Obtaining Free Test Ethers for the Rinkeby Test Network on the Ethereum Blockchain Project Ideas on Blockchain for Professionals Sending and Receiving Bitcoin Top 10 Blockchain Project Concepts Uses of Blockchain What do you need to do to become a Blockchain Developer? Blockchain Technology-Introduction

Cryptography in Blockchain

What is Cryptography?

Cryptography is a method for developing codes that prevent third parties from viewing private data. It helps in protecting data and information by using codes, such that it can only be interpreted by the intended receiver. Information can be encoded to make it difficult to steal the data. Clients want to be sure that their data and transactions are secure in the best possible way, though some organizations across the globe follow their own standard of security.

Cryptography is an essential component of Blockchain technology. Blockchain mainly uses asymmetric cryptography, also known as public-key cryptography. Blockchain uses cryptography in different ways, like- for wallets, security, and transactions. The role of cryptography is essential in Blockchain in terms of maintaining the trust and eliminating intermediates.

Public Key Cryptography

Public key cryptography uses the keys in pair, a public and private key. The public key is for encryption that can be distributed commonly, but the private key is not meant to share with anyone. Public key cryptography is mostly used between two users or two servers in a secure way. Blockchain technology mainly uses asymmetric cryptography for user authentication and data validation through digital signature.

There are many terms used in cryptography, and few are defined below:

  1. Encryption: Encryption is the method of converting data into a set of random numbers and alphabets, which makes it meaningless but not for the intended recipient. It is the process of encoding plaintext to ciphertext. In the process of encryption, the data and information are referred to as plaintext, whereas the converted data referred to as ciphertext.
Cryptography in Blockchain
  • Decryption: Decryption can be defined as the process of converting back the encrypted data into real data. It is the reverse process of encryption. An intended recipient can only decrypt the data by using the private key. If the key is not available, programmed software may be needed to decrypt the code using algorithms to make the data readable.
Cryptography in Blockchain
  • Cipher: Cipher is an Algorithm that takes some data and produces a fixed-length output called ciphertext. A ciphertext is an encrypted form of the plaintext. It comprises a well-defined set of operations for encryption and decryption.
  • Keys: Cryptography requires a key for encryption and decryption that allows changing the plaintext into ciphertext. The keys help to lock and unlock the data whenever needed; hence a key is not only a password.
  • Digital signature: Digital signature is a mathematical strategy used to generate digital codes, which are used to establish the legitimacy of digital messages & documents. These codes are produced and substantiated by public-key encryption. The content and the sender's specification are verified by attaching the signature to the electronically disseminated document.
  • Payload: Payload is the information or the actual data in the message, which needs to be transmitted safely between the two parties, namely the sender and receiver. The operations are performed on the payload.
  • Cryptographic Hashing: Cryptography hashing is an essential part of blockchain technology, which makes the Blockchain an immutable database. It is really important when someone needs to verify the authenticity of data or transactions. A hash function is created by a mathematical technique that takes an input any digital entity and produces an output of a 32-characters size fixed string, which is a combination of letters and numbers.

The SHA (Secure Hash Algorithm) is one of the most popular cryptographic hash functions (CHF). It is available in various forms like SHA1, SHA256, SHA512, etc. A cryptographic hash is used to make a signature for a text or a data file. We can convert data using various hashing algorithms

  • MD5- Message digest algorithm is used to generate a 128-bit hash value.
  • SHA1- It is an upgraded version of SHA designed by NIST and was published as per the Federal Information Processing Standard (FIPS).
  • SHA256- Hash value is computed with 32-bit words, and the message digest is 256 bits.
  • SHA512- Hash value is computed with 64-bit words, and the message digest is 512-bits.

Every such algorithm takes some data and produces a fixed-length output called as a cipher. 

Cryptography Example using Python

import hashlib
data=input("Enter data to encrypt ") a=hashlib.md5(data.encode()).hexdigest() b=hashlib.sha1(data.encode()).hexdigest() c=hashlib.sha256(data.encode()).hexdigest() d=hashlib.sha512(data.encode()).hexdigest() print(a,len(a))
print(b,len(b))
print(c,len(c))
print(d,len(d)) 

Output:

Enter data to encrypt   1234
81dc9bdb52d04dc20036dbd8313ed055   length 32
7110eda4d09e062aa5e4a390b0a572ac0d2c0220   length 40
03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4   length 64 d404559f602eab6fd602ac7680dacbfaadd13630335e951f097af3900e9de176b6db28512f2e
000b9d04fba5133e8b1c6e8df59db3a8ab9d60be4b97cc9e81db   length 128 

Cryptography Example using Java:

import java.security.MessageDigest;
import java.util.Scanner;
public class HashingTest {
private static String bytesToHex(byte[] hash) { StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in); System.out.println("Enter the String to encrypt : "); String s=sc.nextLine();
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] sha256digest = digest.digest(s.getBytes()); String sha256cipher=bytesToHex(sha256digest);
System.out.println("SHA-256 cipher is "+sha256cipher);
}         
} 

Cryptography Example using NodeJS

const SHA256=require('crypto-js/sha256');
function hashing(s)
{
return SHA256(s).toString();
}
console.log(hashing("xyz"))