Introduction
Project Name: VoteChain: Decentralized Voting Platform
Concept: Traditional voting systems face challenges with transparency, security, and verifiability. Voters must trust that their votes are counted correctly and that no tampering occurs. This project creates a blockchain-based voting system where every vote is recorded on an immutable ledger, results are transparently verifiable, and voter privacy is maintained through cryptographic techniques.
The Blockchain Solution: Instead of relying on centralized vote counting, we implement a voting system where each vote becomes a block in the chain. Votes are encrypted for privacy but can be verified by voters. The blockchain ensures that once cast, votes cannot be altered or removed, and the entire voting process is publicly auditable while maintaining voter anonymity.
Features
- đłïž Immutable Voting: Every vote is permanently recorded on the blockchain
- đ Voter Privacy: Votes are encrypted and voter identities are protected
- â Verifiable Results: Anyone can verify the final tally
- đ Real-time Results: Live vote counting with transparency
- đ Voter Authentication: Prevent duplicate voting
- â° Time-limited Voting: Configure voting periods
- đ Multiple Elections: Support for simultaneous elections
- đ Individual Verification: Voters can verify their vote was counted
Project File Structure
decentralized-voting/ â âââ index.php # Main application UI âââ style.css # Custom styling âââ script.js # Frontend JavaScript âââ api/ â âââ register_voter.php # Register new voters â âââ cast_vote.php # Cast a vote â âââ get_elections.php # Get active elections â âââ get_results.php # Get election results â âââ verify_vote.php # Verify a specific vote â âââ admin_setup.php # Create new elections âââ includes/ â âââ config.php # Database configuration â âââ VoteChain.php # Core blockchain logic for voting âââ database/ â âââ schema.sql # Database schema âââ README.md # Project documentation
Database Setup (MySQL)
1. Create a database named votechain_db.
2. Run the database/schema.sql script:
CREATE DATABASE IF NOT EXISTS votechain_db;
USE votechain_db;
-- Elections table
CREATE TABLE elections (
id INT AUTO_INCREMENT PRIMARY KEY,
election_id VARCHAR(50) NOT NULL UNIQUE,
title VARCHAR(255) NOT NULL,
description TEXT,
candidates JSON NOT NULL, -- Store candidates as JSON array
start_time BIGINT NOT NULL,
end_time BIGINT NOT NULL,
status ENUM('upcoming', 'active', 'completed', 'cancelled') DEFAULT 'upcoming',
total_votes INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_status (status),
INDEX idx_election (election_id)
);
-- Voters table (registered voters)
CREATE TABLE voters (
id INT AUTO_INCREMENT PRIMARY KEY,
voter_id VARCHAR(100) NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE,
public_key TEXT, -- For vote verification
registered_at BIGINT NOT NULL,
is_active BOOLEAN DEFAULT TRUE,
INDEX idx_voter (voter_id)
);
-- Vote blockchain table
CREATE TABLE vote_chain (
id INT AUTO_INCREMENT PRIMARY KEY,
block_index INT NOT NULL,
election_id VARCHAR(50) NOT NULL,
voter_id VARCHAR(100) NOT NULL,
encrypted_vote TEXT NOT NULL, -- Encrypted vote data
vote_hash VARCHAR(64) NOT NULL UNIQUE,
previous_hash VARCHAR(64) NOT NULL,
timestamp BIGINT NOT NULL,
nonce INT NOT NULL,
verified BOOLEAN DEFAULT TRUE,
FOREIGN KEY (election_id) REFERENCES elections(election_id),
INDEX idx_election_vote (election_id),
INDEX idx_voter (voter_id)
);
-- Vote receipts (for voter verification)
CREATE TABLE vote_receipts (
id INT AUTO_INCREMENT PRIMARY KEY,
election_id VARCHAR(50) NOT NULL,
voter_id VARCHAR(100) NOT NULL,
receipt_code VARCHAR(100) NOT NULL UNIQUE,
block_index INT NOT NULL,
vote_hash VARCHAR(64) NOT NULL,
created_at BIGINT NOT NULL,
FOREIGN KEY (election_id) REFERENCES elections(election_id),
INDEX idx_receipt (receipt_code)
);
-- Audit log
CREATE TABLE audit_log (
id INT AUTO_INCREMENT PRIMARY KEY,
action VARCHAR(50) NOT NULL,
election_id VARCHAR(50),
voter_id VARCHAR(100),
details JSON,
ip_address VARCHAR(45),
timestamp BIGINT NOT NULL,
INDEX idx_action (action),
INDEX idx_timestamp (timestamp)
);
-- Insert genesis block
INSERT INTO vote_chain (block_index, election_id, voter_id, encrypted_vote, vote_hash, previous_hash, timestamp, nonce)
VALUES (0, 'GENESIS', 'SYSTEM', 'Genesis Vote Block',
SHA2(CONCAT('0', 'GENESIS', 'SYSTEM', 'Genesis Vote Block', UNIX_TIMESTAMP(), '0', '0'), 256),
'0', UNIX_TIMESTAMP(), 0);
Code Implementation
1. includes/config.php (Database Configuration)
```php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'votechain_db');
function getDBConnection() {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error) {
die(json_encode(['success' => false, 'message' => 'Database connection failed: ' . $conn->connect_error]));
}
return $conn;
}
// Encryption key for vote privacy (in production, use proper key management)
define('ENCRYPTION_KEY', 'votech