ig it seems to be workan rn
This commit is contained in:
+191
-51
@@ -3,7 +3,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Random Binary Generator with Monobit Test</title>
|
||||
<title>One-Time Pad Tool</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg-color: #030712;
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
body {
|
||||
font-family: system-ui, -apple-system, sans-serif;
|
||||
max-width: 500px;
|
||||
max-width: 520px;
|
||||
margin: 40px auto;
|
||||
padding: 0 20px;
|
||||
line-height: 1.5;
|
||||
@@ -47,6 +47,9 @@
|
||||
font-size: 1.35rem;
|
||||
letter-spacing: -0.01em;
|
||||
}
|
||||
.form-group {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
label {
|
||||
display: block;
|
||||
font-weight: 600;
|
||||
@@ -54,24 +57,43 @@
|
||||
color: var(--text-main);
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
input[type="number"] {
|
||||
input[type="number"], select {
|
||||
width: 100%;
|
||||
padding: 10px 12px;
|
||||
box-sizing: border-box;
|
||||
margin-bottom: 16px;
|
||||
background-color: var(--input-bg);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 6px;
|
||||
color: var(--text-main);
|
||||
font-size: 1rem;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
input[type="number"]:focus {
|
||||
input[type="file"] {
|
||||
width: 100%;
|
||||
padding: 8px;
|
||||
box-sizing: border-box;
|
||||
background-color: var(--input-bg);
|
||||
border: 1px dashed var(--border-color);
|
||||
border-radius: 6px;
|
||||
color: var(--text-muted);
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
input[type="file"]::file-selector-button {
|
||||
background: var(--border-color);
|
||||
color: var(--text-main);
|
||||
border: none;
|
||||
padding: 6px 12px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
margin-right: 10px;
|
||||
}
|
||||
input:focus, select:focus {
|
||||
outline: 2px solid var(--primary);
|
||||
border-color: transparent;
|
||||
}
|
||||
button {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
margin-top: 8px;
|
||||
background-color: var(--primary);
|
||||
color: #ffffff;
|
||||
border: none;
|
||||
@@ -112,37 +134,76 @@
|
||||
<body>
|
||||
|
||||
<div class="card">
|
||||
<h2>CSPRNG Binary Generator</h2>
|
||||
<label for="multiplier">Enter Max Message Length (X*16-bits):</label>
|
||||
<input type="number" id="multiplier" min="1" value="600" placeholder="Enter an integer">
|
||||
|
||||
<button id="generateBtn">Generate, Validate & Download</button>
|
||||
|
||||
<div class="info" id="bitCountInfo">
|
||||
Total bits: 9,600 bits (1,200 bytes)
|
||||
<h2>One-Time Pad Cipher Tool</h2>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="modeSelect">Operation Mode:</label>
|
||||
<select id="modeSelect">
|
||||
<option value="encode-new">Encrypt File (Generate New Pad)</option>
|
||||
<option value="encode-existing">Encrypt File (Use Existing Pad)</option>
|
||||
<option value="decode-existing">Decrypt File (Use Existing Pad)</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group" id="messageGroup">
|
||||
<label for="messageInput">Select File / Message:</label>
|
||||
<input type="file" id="messageInput">
|
||||
</div>
|
||||
|
||||
<div class="form-group" id="padGroup" style="display: none;">
|
||||
<label for="padInput">Select Existing Key Pad (.pad):</label>
|
||||
<input type="file" id="padInput">
|
||||
</div>
|
||||
|
||||
<div class="form-group" id="sizeGroup">
|
||||
<label for="multiplier">Pad Block Multiplier (X*16-bits):</label>
|
||||
<input type="number" id="multiplier" min="1" value="600" placeholder="Enter an integer">
|
||||
<div class="info" id="bitCountInfo">
|
||||
Generated pad size: 9,600 bits (1,200 bytes)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button id="processBtn">Process & Download</button>
|
||||
|
||||
<div id="statusBox" class="status"></div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const modeSelect = document.getElementById('modeSelect');
|
||||
const messageInput = document.getElementById('messageInput');
|
||||
const padInput = document.getElementById('padInput');
|
||||
const multiplierInput = document.getElementById('multiplier');
|
||||
const bitCountInfo = document.getElementById('bitCountInfo');
|
||||
const generateBtn = document.getElementById('generateBtn');
|
||||
const processBtn = document.getElementById('processBtn');
|
||||
const statusBox = document.getElementById('statusBox');
|
||||
|
||||
const padGroup = document.getElementById('padGroup');
|
||||
const sizeGroup = document.getElementById('sizeGroup');
|
||||
|
||||
// Toggle visible UI controls depending on selected mode
|
||||
modeSelect.addEventListener('change', () => {
|
||||
const mode = modeSelect.value;
|
||||
if (mode === 'encode-new') {
|
||||
padGroup.style.display = 'none';
|
||||
sizeGroup.style.display = 'block';
|
||||
} else {
|
||||
padGroup.style.display = 'block';
|
||||
sizeGroup.style.display = 'none';
|
||||
}
|
||||
statusBox.style.display = 'none';
|
||||
});
|
||||
|
||||
multiplierInput.addEventListener('input', () => {
|
||||
const x = parseInt(multiplierInput.value, 10) || 0;
|
||||
const bits = x * 16;
|
||||
const bytes = bits / 8;
|
||||
bitCountInfo.textContent = `Total bits: ${bits.toLocaleString()} bits (${bytes.toLocaleString()} bytes)`;
|
||||
bitCountInfo.textContent = `Generated pad size: ${bits.toLocaleString()} bits (${bytes.toLocaleString()} bytes)`;
|
||||
});
|
||||
|
||||
// Trigger generator when pressing Enter inside the input field
|
||||
multiplierInput.addEventListener('keydown', (event) => {
|
||||
if (event.key === 'Enter') {
|
||||
event.preventDefault();
|
||||
generateBtn.click();
|
||||
processBtn.click();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -171,7 +232,6 @@
|
||||
for (let i = 0; i < uint8Array.length; i++) {
|
||||
let byte = uint8Array[i];
|
||||
for (let bit = 0; bit < 8; bit++) {
|
||||
// Convert bit (0 -> -1, 1 -> +1)
|
||||
sum += ((byte >> bit) & 1) ? 1 : -1;
|
||||
}
|
||||
}
|
||||
@@ -179,7 +239,6 @@
|
||||
const sObs = Math.abs(sum) / Math.sqrt(totalBits);
|
||||
const pValue = erfc(sObs / Math.sqrt(2));
|
||||
|
||||
// NIST standard threshold: p-value must be >= 0.01 to pass
|
||||
return {
|
||||
passed: pValue >= 0.01,
|
||||
pValue: pValue,
|
||||
@@ -188,7 +247,6 @@
|
||||
};
|
||||
}
|
||||
|
||||
// Helper to generate a cryptographically secure 8-digit random integer string
|
||||
function generateRandom8Digits() {
|
||||
const randomBuffer = new Uint32Array(1);
|
||||
window.crypto.getRandomValues(randomBuffer);
|
||||
@@ -196,53 +254,135 @@
|
||||
return num.toString();
|
||||
}
|
||||
|
||||
generateBtn.addEventListener('click', () => {
|
||||
statusBox.style.display = 'none';
|
||||
const x = parseInt(multiplierInput.value, 10);
|
||||
// Reads a File object into an ArrayBuffer asynchronously
|
||||
function readFileAsBuffer(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(new Uint8Array(reader.result));
|
||||
reader.onerror = () => reject(reader.error);
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
}
|
||||
|
||||
if (isNaN(x) || x < 1) {
|
||||
alert('Please enter a valid positive integer for X.');
|
||||
// Helper to trigger direct browser file download
|
||||
function triggerDownload(uint8Array, filename) {
|
||||
const blob = new Blob([uint8Array], { type: 'application/octet-stream' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
processBtn.addEventListener('click', async () => {
|
||||
statusBox.style.display = 'none';
|
||||
const mode = modeSelect.value;
|
||||
|
||||
if (!messageInput.files.length) {
|
||||
alert('Please select a input message/file to process.');
|
||||
return;
|
||||
}
|
||||
|
||||
const byteCount = x * 2;
|
||||
const buffer = new Uint8Array(byteCount);
|
||||
const messageFile = messageInput.files[0];
|
||||
let messageData;
|
||||
|
||||
try {
|
||||
// 1. Generate CSPRNG bytes
|
||||
window.crypto.getRandomValues(buffer);
|
||||
messageData = await readFileAsBuffer(messageFile);
|
||||
} catch (err) {
|
||||
alert('Error reading input message file: ' + err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Perform Randomness Check
|
||||
const testResult = runMonobitTest(buffer);
|
||||
const randomPrefix = generateRandom8Digits();
|
||||
|
||||
if (!testResult.passed) {
|
||||
// MODE 1: Encrypt with a newly generated CSPRNG pad
|
||||
if (mode === 'encode-new') {
|
||||
const x = parseInt(multiplierInput.value, 10);
|
||||
if (isNaN(x) || x < 1) {
|
||||
alert('Please enter a valid positive integer for X.');
|
||||
return;
|
||||
}
|
||||
|
||||
const generatedPadByteCount = x * 2;
|
||||
if (messageData.length > generatedPadByteCount) {
|
||||
statusBox.className = 'status error';
|
||||
statusBox.innerHTML = `<strong>Test Failed!</strong><br>Sequence failed Monobit randomness test (P-value: ${testResult.pValue.toFixed(4)} < 0.01). Download aborted.`;
|
||||
statusBox.innerHTML = `<strong>Encryption Failed!</strong><br>Message size (${messageData.length.toLocaleString()} bytes) exceeds pad size (${generatedPadByteCount.toLocaleString()} bytes). Increase X.`;
|
||||
statusBox.style.display = 'block';
|
||||
return;
|
||||
}
|
||||
|
||||
// Display Success Stats
|
||||
const padData = new Uint8Array(generatedPadByteCount);
|
||||
window.crypto.getRandomValues(padData);
|
||||
|
||||
const testResult = runMonobitTest(padData);
|
||||
if (!testResult.passed) {
|
||||
statusBox.className = 'status error';
|
||||
statusBox.innerHTML = `<strong>Randomness Check Failed!</strong><br>Generated key failed Monobit test (P-value: ${testResult.pValue.toFixed(4)} < 0.01). Process aborted.`;
|
||||
statusBox.style.display = 'block';
|
||||
return;
|
||||
}
|
||||
|
||||
// Bitwise XOR encryption
|
||||
const cipherData = new Uint8Array(messageData.length);
|
||||
for (let i = 0; i < messageData.length; i++) {
|
||||
cipherData[i] = messageData[i] ^ padData[i];
|
||||
}
|
||||
|
||||
// Trigger downloads for both the cipher text and key pad
|
||||
triggerDownload(cipherData, `${randomPrefix}_${messageFile.name}.enc`);
|
||||
triggerDownload(padData, `${randomPrefix}_onetime_${x}x16bits.pad`);
|
||||
|
||||
statusBox.className = 'status success';
|
||||
statusBox.innerHTML = `<strong>Randomness Test Passed!</strong><br>` +
|
||||
`P-value: ${testResult.pValue.toFixed(4)} (≥ 0.01)<br>` +
|
||||
`Bit distribution: ${testResult.onesCount} ones out of ${testResult.totalBits} bits.`;
|
||||
statusBox.innerHTML = `<strong>Encryption Complete!</strong><br>` +
|
||||
`Monobit P-value: ${testResult.pValue.toFixed(4)}<br>` +
|
||||
`Downloaded: Encrypted payload and matching <code>.pad</code> key.`;
|
||||
statusBox.style.display = 'block';
|
||||
}
|
||||
|
||||
// MODE 2 & 3: Encrypt or Decrypt using a supplied .pad file
|
||||
else {
|
||||
if (!padInput.files.length) {
|
||||
alert('Please select an existing .pad file.');
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Trigger Download with random 8-digit prefix
|
||||
const randomPrefix = generateRandom8Digits();
|
||||
const blob = new Blob([buffer], { type: 'application/octet-stream' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `${randomPrefix}_onetime_${x}x16bits.pad`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
let padData;
|
||||
try {
|
||||
padData = await readFileAsBuffer(padInput.files[0]);
|
||||
} catch (err) {
|
||||
alert('Error reading key pad file: ' + err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
} catch (error) {
|
||||
alert('An error occurred during process: ' + error.message);
|
||||
if (messageData.length > padData.length) {
|
||||
statusBox.className = 'status error';
|
||||
statusBox.innerHTML = `<strong>Process Failed!</strong><br>Payload length (${messageData.length.toLocaleString()} bytes) exceeds key pad length (${padData.length.toLocaleString()} bytes).`;
|
||||
statusBox.style.display = 'block';
|
||||
return;
|
||||
}
|
||||
|
||||
// Bitwise XOR operation (Symmetric)
|
||||
const outputData = new Uint8Array(messageData.length);
|
||||
for (let i = 0; i < messageData.length; i++) {
|
||||
outputData[i] = messageData[i] ^ padData[i];
|
||||
}
|
||||
|
||||
if (mode === 'encode-existing') {
|
||||
triggerDownload(outputData, `${randomPrefix}_${messageFile.name}.enc`);
|
||||
statusBox.className = 'status success';
|
||||
statusBox.innerHTML = `<strong>Encryption Complete!</strong><br>File encrypted using supplied key pad.`;
|
||||
} else {
|
||||
// Strip .enc extension if present during decryption
|
||||
let originalName = messageFile.name.replace(/\.enc$/i, '');
|
||||
triggerDownload(outputData, `decrypted_${originalName}`);
|
||||
statusBox.className = 'status success';
|
||||
statusBox.innerHTML = `<strong>Decryption Complete!</strong><br>File successfully decrypted using supplied key pad.`;
|
||||
}
|
||||
|
||||
statusBox.style.display = 'block';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user