### 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:

**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.

**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.

**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

**M****D5-**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"))