Well, how about
function gamma(text, key) {
let res="",
i = 0;
for (let c of text)
res += String.fromCharCode(
c.charCodeAt(0) ^ key.charCodeAt(i++ % key.length))
return res
}
site = "Z\t\u001d\u0005\u0012O\u0011\u0004\n\u0000\u0000VA\f\u000b\n\bHL\u0003\u000fO\u0006\u0003\u0003\u001d\u0017WI\t\u001d\u0005\u0012Q"
onload = function() {
document.body.innerHTML = gamma(site, prompt('password?'))
}
Unless you enter a valid password (which happens to be “fork”), you’ll get rubbish instead of the site.
Basically, it “encrypts” the whole web page (using a naive gamming algorithm). Since this a symmetric encryption, the same key can be used to turn the text back into the initial state.
Here’s a small snippet to automate the whole thing:
function gamma(text, key) {
let res="", i = 0;
for (let c of text)
res += String.fromCharCode(c.charCodeAt(0) ^ key.charCodeAt(i++ % key.length))
return res
}
$ = x => document.getElementById(x)
encode = function() {
let site = $('site').value,
pass = $('pass').value
let enc = JSON.stringify(gamma(site, pass))
$('out').value = [
'<script>',
`site = ${enc}`,
gamma,
'onload = function() {',
'document.body.innerHTML = gamma(site, prompt("password?"))',
'}</' + 'script>'
].join('\n')
}
<h3>Ultimate web protector, patent pending!</h3>
<p><textarea id="site" cols=80 rows=10>my site</textarea></p>
<p><input id="pass" value="my password"></p>
<p><button onclick="encode()">Encode!</button></p>
<p><textarea id="out" cols=80 rows=10></textarea></p>
For future readers: this code is a joke, don’t even think about using it to actually protect anything serious! However, if you replace the naive “encryption” above with a proven algorithm like AES, this can be used as a prototype for a real-world solution if you have a serverless environment, but still need to password-protect things.
8
solved Is there a type of password protection that works on local files? [closed]