EXPERIMENT NO - 01
Aim : Cryptography in Blockchain, Merkle root tree hash
Theory :
Cryptography in Blockchain
What is Cryptography?
o Cryptography is made up of two ancient Greek terms, Kryptos meaning "hidden
secret" and Graphein means “to write”. Cryptography is the study and practice of
sending secure, encrypted messages between two or more parties. Cryptography allows
digital currency transactions to be pseudonymous, secure, and “trustless” – with no
bank or other intermediary required.
Terminology:
o Encryption:
– It is a process of plaintext (normal text) to a cipher text (random sequence of bits).
o Decryption:
– The inverse process of encryption, conversion of cipher text to plaintext.
o Cipher:
– The mathematical function, i.e. a cryptographic algorithm which is used to convert
plaintext to get cipher text.
o Key:
– A key in cryptography is a piece of information, usually a string of numbers or
letters that are stored in a file, which, when processed through a cryptographic
algorithm, can encode or decode cryptographic data.
Types of Cryptography
1. Symmetric-key cryptography
o Symmetric-key encryption focuses on using similar keys (known as secret-key) for
encryption as well as decryption of data. Using a common single key creates a
problem of securely transferring the key between the sender and the receiver. It is
also called Secret-Key Cryptography
2. Asymmetric key cryptography
o This encryption method uses a pair of keys for an encryption (public key) and
decryption key (private key). It is also called Public-Key Cryptography.
3. Hash functions
o This type of encryption doesn’t make use of keys. It uses a cipher to generate a hash
value of a fixed length from the plaintext. It is nearly impossible for the contents of
plain text to be recovered from the cipher text.
Use of Cryptography in Blockchain
o Blockchain make use of two types of cryptographic algorithms, asymmetric-key
algorithms, and hash functions. Hash functions are used to provide the functionality of
a single view of Blockchain to every participant. Blockchain generally use the SHA-256
hashing algorithm as their hash function.
o Cryptographic hash functions provide the following benefits to the Blockchain:
– Avalanche effect: A small change in the data can result in a significantly different
output.
– Uniqueness: Every input has a unique output.
– Deterministic: Any input will always have the same output if passed through the
hash function.
– Quickness: The output can be generated in a very small amount of time.
– Reverse engineering is not possible, i.e. we cannot generate the input by having the
output and the hash function.
o Hash functions have a major role in linking the blocks to one another and also to
maintain the integrity of the data stored inside each block. Any alteration in the block
data can lead to inconsistency and break the Blockchain, making it invalid. This
requirement is achieved by the property of the hash functions, called the ‘avalanche
effect’.
o Let us take an example of an SHA-256 hash function, and compare their outputs,
Input: Welcome to Blockchain Demo
Output: 00001b0da1a1e081476ba50be4cb9cc326daeb1017a0d1e27eab6078688bb104
Input with a slight difference: Welcome to Blockchain Demo 1
Output: 0000d7f8bdc48930c9938eb3b5775c904b620d176d6efb4e2deeac0011d76960
o You can notice the huge difference in output after changing a single character at the
input from lowercase to uppercase. This makes the data reliable and secure on the
blockchain; any changes in the block data will lead to this difference in hash value and
make the blockchain invalid, making it immutable.
Digital Signatures :
o One of the major parts of asymmetric-key cryptography is digital signatures. Digital
signature is used to verify authenticity, integrity, non-repudiation, i.e. it is assuring that
the message is sent by the known user and not modified. The digital signatures ensure
that the Blockchain is valid and the data is verified and correct.
o Hashing, public-private key pairs, and the digital signatures together constitute the
foundation for the Blockchain. These cryptographic features make it possible for
blocks to get securely linked by other blocks, and also ensure the reliability and
immutability of the data stored on the Blockchain.
Let us look at its application in cryptocurrencies.
o Cryptocurrencies are one of the major applications of Blockchain, and they use public-
private key pairs to maintain the addresses of the users on the Blockchain.
o For cryptography in Blockchain,
– The public key is used as the address of the person. The public key is visible
globally, i.e. it is visible to any participant of the participant.
– The private key is a secret value and is used to access that address data and
authorize any of the actions for the ‘address’, which are generally transactions.
– Digital signatures are widely used for cryptocurrencies. They are used to approve
transactions by signing them securely (offline) and are also used for multi-signature
contracts and digital wallets on the Blockchain.
Merkle Tree
Merkle trees is an implementation of binary trees where each non-leaf node is a hash of
the two child nodes. The leaves can either be the data itself or a hash/signature of the data.
If we found any difference in root hash between the systems, we can use binary search to
detect the defective subtree. Thus by simple log (N) complexity, we can find the problematic
area or say problematic subtree.
The Merkel tree is used to verify the kind of data is handled, transferred between the
computers. Here the main use of the Merkel tree is that the blocks received from the other
peers in the peer to peer network are not damaged or altered. It also verifies that the other
peers should not lie about the data sent by them.
Role of Merkle tree in Blockchain
– Merkle tree is the Core data structure which used in the Bitcoin to verify the existence
of the transaction in Bitcoin in such a way that the time and space is conserved.
– A Merkle tree is a binary tree in which each leaf node is a hash of a block of data and
each internal node is a hash of its children. In the bitcoin protocol it ensures the
immutability of the set of transactions by taking the hash of each data transaction and
generating a single unique root hash representing the hash of the entire set.
Algorithm and Implementation
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
public class MerkleTrees
{
List<String> txList; // A list of transaction
String root; // Merkle Root
public MerkleTrees(List<String> txList)
{
this.txList = txList;
root = "";
}
public void merkle_tree()
{
List<String> tempTxList = new ArrayList<String>();
for (int i = 0; i < this.txList.size(); i++){
tempTxList.add(this.txList.get(i));
}
List<String> newTxList = getNewTxList(tempTxList);
while (newTxList.size() != 1)
{
newTxList = getNewTxList(newTxList);
}
this.root = newTxList.get(0);
}
private List<String> getNewTxList(List<String> tempTxList)
{
List<String> newTxList = new ArrayList<String>();
int index = 0;
while (index < tempTxList.size())
{
// left
String left = tempTxList.get(index);
index++;
// right
String right = ""; if (index != tempTxList.size())
{
right = tempTxList.get(index);
}
// sha2 hex value
String sha2HexValue = getSHA2HexValue (left + right);
newTxList.add(sha2HexValue);
index++;
}
return newTxList;
}
public String getSHA2HexValue(String str)
{
byte[] cipher_byte;
try
{
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(str.getBytes());
cipher_byte = md.digest();
StringBuilder sb = new StringBuilder(2 * cipher_byte.length);
for (byte b : cipher_byte) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String getRoot() {
return this.root;
}
}
Output: