`;
};
// --- CORE LOGIC ---
const handleAnalysis = () => {
analyzeBtnSpinner.classList.remove('hidden');
analyzeBtnText.textContent = 'Analyzing...';
analyzeBtn.disabled = true;
setTimeout(() => {
const plainText = document.getElementById('plain-text').value;
const selectedAlgoId = algorithmSelect.value;
const algorithm = algorithms.find(a => a.id === selectedAlgoId);
const cracksPerSecondStr = document.getElementById('cracks-per-second').value.replace(/,/g, '');
const cracksPerSecond = BigInt(cracksPerSecondStr);
// 1. Simulate encryption
const encryptedText = [...plainText].map(() => Math.floor(Math.random() * 256).toString(16).padStart(2, '0')).join('');
// 2. Calculate time to crack using BigInt for precision
const combinations = BigInt(2) ** BigInt(algorithm.keySize);
const secondsToCrack = combinations / cracksPerSecond;
const crackTimeText = formatTime(secondsToCrack);
const result = {
plainText,
encryptedText,
algorithm,
cracksPerSecond: cracksPerSecondStr,
crackTimeText
};
renderAnalysisResult(result);
downloadPdfBtn.disabled = false;
analyzeBtnSpinner.classList.add('hidden');
analyzeBtnText.textContent = 'Encrypt & Analyze Strength';
analyzeBtn.disabled = false;
}, 500);
};
const formatTime = (totalSeconds) => {
if (totalSeconds < 60) return `${totalSeconds} seconds`;
const minutes = totalSeconds / BigInt(60);
if (minutes < 60) return `${minutes} minutes`;
const hours = minutes / BigInt(60);
if (hours < 24) return `${hours} hours`;
const days = hours / BigInt(24);
if (days < 365) return `${days} days`;
const years = days / BigInt(365);
const units = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion'];
let num = Number(years);
let unitIndex = 0;
while (num >= 1000 && unitIndex < units.length - 1) {
num /= 1000;
unitIndex++;
}
return `${num.toFixed(1)} ${units[unitIndex]} years`;
};
// --- UI & EVENT HANDLERS ---
const switchTab = (tabId) => {
currentTab = tabId;
Object.values(tabPanes).forEach(pane => pane.classList.add('hidden'));
tabPanes[tabId].classList.remove('hidden');
Object.values(tabButtons).forEach(btn => btn.classList.replace('tab-active', 'tab-inactive'));
tabButtons[tabId].classList.replace('tab-inactive', 'tab-active');
updateNavButtons();
};
const navigateTabs = (direction) => {
const currentIndex = tabs.indexOf(currentTab);
const newIndex = direction === 'next' ? currentIndex + 1 : currentIndex - 1;
if (newIndex >= 0 && newIndex < tabs.length) switchTab(tabs[newIndex]);
};
const updateNavButtons = () => {
const currentIndex = tabs.indexOf(currentTab);
prevBtn.disabled = currentIndex === 0;
nextBtn.disabled = currentIndex === tabs.length - 1;
prevBtn.classList.toggle('opacity-50', prevBtn.disabled);
nextBtn.classList.toggle('opacity-50', nextBtn.disabled);
};
const handlePdfDownload = () => {
if(!lastAnalysisResult) return;
const res = lastAnalysisResult;
let pdfHtml = `
Encryption Strength Analysis Report
| Algorithm | ${res.algorithm.name} |
| Key Size | ${res.algorithm.keySize} bits |
| Cracking Speed Assumption | ${Number(res.cracksPerSecond).toExponential()} keys/sec |
| Estimated Time to Crack | ${res.crackTimeText} |
| Encrypted Data (Simulated) | ${res.encryptedText.substring(0, 128)}... |
`;
pdfRenderContainer.innerHTML = pdfHtml;
html2canvas(pdfRenderContainer, { scale: 2 }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth(), margin = 40;
const contentWidth = pdfWidth - margin * 2;
const pdfHeight = (canvas.height * contentWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', margin, margin, contentWidth, pdfHeight);
pdf.save('Encryption-Analysis-Report.pdf');
});
};
// --- EVENT LISTENERS ---
window.switchTab = switchTab;
window.navigateTabs = navigateTabs;
analyzeBtn.addEventListener('click', handleAnalysis);
downloadPdfBtn.addEventListener('click', handlePdfDownload);
// Auto-format the large number input
document.getElementById('cracks-per-second').addEventListener('input', (e) => {
let value = e.target.value.replace(/\D/g, '');
e.target.value = new Intl.NumberFormat().format(BigInt(value || 0));
});
// --- INITIALIZATION ---
initialize();
});