Public Key vs Private Key Blockchain: Key Differences and Usage
public key is a cryptographic code shared openly to receive transactions, while a private key is a secret code used to sign and authorize transactions securely. The public key ensures transparency, and the private key ensures control and security over assets.Quick Comparison
This table summarizes the main differences between public key and private key in blockchain.
| Factor | Public Key | Private Key |
|---|---|---|
| Visibility | Shared openly on the network | Kept secret by the owner |
| Purpose | Receive funds and verify signatures | Sign transactions and prove ownership |
| Security Role | Used to verify authenticity | Used to authorize and control assets |
| Length | Usually longer (e.g., 256 bits) | Same length as public key but kept private |
| Example Use | Wallet address derived from public key | Unlock wallet and sign transactions |
Key Differences
The public key is like your email address that you can share with anyone to receive messages (or funds). It is derived from the private key using a one-way mathematical function, so it is safe to share publicly without revealing the private key.
The private key is like your password or signature. It must be kept secret because it allows you to sign transactions and prove ownership of your blockchain assets. If someone else gets your private key, they can control your funds.
In Solidity and blockchain, the public key is often used to generate wallet addresses, while the private key is used off-chain to sign transactions before sending them to the network for verification.
Code Comparison
This Solidity example shows how a public key (address) is used to verify ownership by checking the signer of a message.
pragma solidity ^0.8.0; contract VerifySignature { // This function verifies that a message was signed by the owner of the given address (public key) function verify(address signer, bytes32 messageHash, bytes memory signature) public pure returns (bool) { // Recover the address that signed the message address recovered = recoverSigner(messageHash, signature); // Check if recovered address matches the signer (public key) return recovered == signer; } function recoverSigner(bytes32 messageHash, bytes memory signature) internal pure returns (address) { // Split signature into r, s, v variables require(signature.length == 65, "Invalid signature length"); bytes32 r; bytes32 s; uint8 v; assembly { r := mload(add(signature, 32)) s := mload(add(signature, 64)) v := byte(0, mload(add(signature, 96))) } // Recover signer address using ecrecover return ecrecover(messageHash, v, r, s); } }
Private Key Equivalent
Private keys are not used directly in Solidity because they must remain secret and are used off-chain to sign transactions. Here is a simple JavaScript example using ethers.js to sign a message with a private key.
import { ethers } from "ethers"; async function signMessage() { const privateKey = "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; const wallet = new ethers.Wallet(privateKey); const message = "Hello, blockchain!"; const signature = await wallet.signMessage(message); console.log("Signature:", signature); } signMessage();
When to Use Which
Choose public key when you want to share your address to receive funds or verify signatures on-chain. It is safe and necessary for transparency.
Choose private key when you need to sign transactions or prove ownership off-chain. Keep it secret and never expose it in smart contracts or public code.
In practice, use the private key in your wallet software or backend to sign transactions, and use the public key/address in Solidity contracts to verify or identify users.