import hashlib
def msg_to_int(msg: str):
return int(hashlib.sha256(msg.encode()).hexdigest(), 16)
P_recovered = k_inv * C
m_recovered = P_recovered.x # or y, depending on your encoding
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Reversible ECC k·P Tool</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
padding: 20px;
}
h2 { color: #333; }
.box {
background: white;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
box-shadow: 0 0 5px rgba(0,0,0,0.1);
}
input, textarea {
width: 100%;
padding: 8px;
margin-top: 5px;
margin-bottom: 10px;
border-radius: 5px;
border: 1px solid #aaa;
}
button {
padding: 10px 20px;
background: #0078ff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover { background: #005fcc; }
pre {
background: #222;
color: #0f0;
padding: 10px;
border-radius: 5px;
overflow-x: auto;
}
</style>
</head>
<body>
<h2>Reversible ECC k·P Encryption Tool</h2>
<div class="box">
<label>Message:</label>
<textarea id="msg" rows="3"></textarea>
<label>Scalar k:</label>
<input id="k" type="number" value="123456789">
<button onclick="runECC()">Encrypt + Decrypt</button>
</div>
<div class="box">
<h3>Results</h3>
<pre id="output"></pre>
</div>
<script>
// --- secp256k1 parameters ---
const p = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F");
const n = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
const Gx = BigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240");
const Gy = BigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424");
// Point object
class Point {
constructor(x, y) { this.x = x; this.y = y; }
}
// Modular inverse
function modInv(a, m) {
return modPow(a, m - 2n, m);
}
// Modular exponentiation
function modPow(base, exp, mod) {
let r = 1n;
base %= mod;
while (exp > 0) {
if (exp & 1n) r = (r * base) % mod;
base = (base * base) % mod;
exp >>= 1n;
}
return r;
}
// Point addition
function pointAdd(P, Q) {
if (P === null) return Q;
if (Q === null) return P;
if (P.x === Q.x && P.y !== Q.y) return null;
let m;
if (P.x === Q.x && P.y === Q.y) {
m = (3n * P.x * P.x) * modInv(2n * P.y, p) % p;
} else {
m = (Q.y - P.y) * modInv(Q.x - P.x, p) % p;
}
const rx = (m*m - P.x - Q.x) % p;
const ry = (m*(P.x - rx) - P.y) % p;
return new Point((rx+p)%p, (ry+p)%p);
}
// Scalar multiplication
function scalarMult(k, P) {
let R = null;
let A = P;
while (k > 0n) {
if (k & 1n) R = pointAdd(R, A);
A = pointAdd(A, A);
k >>= 1n;
}
return R;
}
// Convert text → integer
function textToInt(txt) {
let bytes = new TextEncoder().encode(txt);
let hex = "0x" + Array.from(bytes).map(b => b.toString(16).padStart(2,"0")).join("");
return BigInt(hex);
}
// Convert integer → text
function intToText(x) {
let hex = x.toString(16);
if (hex.length % 2) hex = "0" + hex;
let bytes = new Uint8Array(hex.match(/.{1,2}/g).map(b => parseInt(b,16)));
return new TextDecoder().decode(bytes);
}
// Main ECC reversible process
function runECC() {
const msg = document.getElementById("msg").value;
const k = BigInt(document.getElementById("k").value);
const m = textToInt(msg) % n;
const P = scalarMult(m, new Point(Gx, Gy));
const C = scalarMult(k, P);
const k_inv = modInv(k, n);
const P2 = scalarMult(k_inv, C);
const recovered = intToText(P2.x);
document.getElementById("output").textContent =
"Message integer m:\n" + m + "\n\n" +
"Point P = m·G:\n" + JSON.stringify(P, null, 2) + "\n\n" +
"Encrypted C = k·P:\n" + JSON.stringify(C, null, 2) + "\n\n" +
"Decrypted P = k⁻¹·C:\n" + JSON.stringify(P2, null, 2) + "\n\n" +
"Recovered message:\n" + recovered;
}
</script>
</body>
</html>
























No comments:
Post a Comment