AES-GCM decrypt a gogo

master
Bel LaPointe 2020-07-24 16:19:58 -06:00
parent 887e67bc7d
commit 26c5ec2c77
2 changed files with 42 additions and 95 deletions

View File

@ -2,111 +2,57 @@
<header> <header>
<meta charset="UTF-8"> <meta charset="UTF-8">
<script> <script>
function who(form) { function b64tobuff(b64) {
var items = {} binary = window.atob(b64)
for (var i=0; i<form.length; i++) { return stobuff(binary)
if (form[i].name)
items[form[i].name] = form[i].value
} }
var params = "?" function stobuff(s) {
for(var k in items) { bytes = new Uint8Array(s.length)
params += k + "=" + items[k] + "&" for (var i = 0; i < s.length; i++) {
bytes[i] = s.charCodeAt(i)
} }
params = params.substr(0, params.length-1) return bytes.buffer
http("GET", "http://localhost:18114/who" + params, function(body, status) {
console.log(status, "body=",body)
}, null)
return false;
} }
function http(method, remote, callback, body) { key = "123".repeat(32).substr(0, 32)
var xmlhttp = new XMLHttpRequest(); console.log("key:", key)
xmlhttp.onreadystatechange = function() { ciphertext = atob("SOY05yF/9iv3YG71sKkQPVaEwO53PCX8qZhDHS9JUohBgVl5Qr9/GTKK/TJ6OozhHN7QBIGmHNzQxTRRSLs4Lw==")
if (xmlhttp.readyState == XMLHttpRequest.DONE) { console.log("ciphertext:", ciphertext)
console.log("KK", xmlhttp.readyState, xmlhttp.responseText, xmlhttp.status) data = ciphertext.substr(12, ciphertext.length)
callback(xmlhttp.responseText, xmlhttp.status) nonce = ciphertext.substr(0, 12)
} console.log("data:", data)
}; console.log("nonce:", nonce)
xmlhttp.open(method, remote, true);
if (typeof body == "undefined") {
body = null
}
xmlhttp.send(body);
}
function callback(responseBody, responseStatus) {
}
function draw(one) { key = crypto.subtle.importKey(
var id = one.id "raw",
var meta = one.meta stobuff(key),
var know = one.know "AES-GCM",
var html = "<div>\n" false,
html += '<h2>' + id + '</h2>\n' ["decrypt"],
html += '<ul>\n' ).then(function(cryptoKey) {
for(var k in meta) { console.log("cryptoKey:", cryptoKey)
html += '<li>' window.crypto.subtle.decrypt(
html += k + ': ' {
var lines = meta[k].split("\n") name: "AES-GCM",
if (lines.length == 1) { iv: stobuff(nonce),
html += lines[0] },
} else { cryptoKey,
html += "<ul>" stobuff(data),
lines.forEach(function(e) { ).then(function(decrypted) {
html += "<li>" + e + "</li>" console.log(decrypted)
console.log(String.fromCharCode.apply(null, new Uint8Array(decrypted)))
}) })
html += "</ul>" .catch(function(eb){
} console.log("eb:",eb.code, eb.message, eb.name)
html += '</li>\n' })
} })
html += '</ul>\n' .catch(function(ea){console.log("ea:",ea.code, ea.message, ea.name)})
html += '<h3>Knows</h3>\n'
html += '<ul>\n'
for(var k of know) {
html += '<li>\n'
var action = '\'who([{name: "id", value: "' + k.id + '"}])\''
html += "<a href='#' onclick=" + action + ">"
html += k.id + ", <i>" + k.relation + " </i>"
html += "</a>"
html += '</li>\n'
}
html += '</ul>\n'
html += "</div>\n"
document.getElementById("display").innerHTML += html
}
function draw_clear() {
document.getElementById("display").innerHTML = ""
}
</script> </script>
</header> </header>
<body> <body>
<h1>Goodbye World</h1>
<form onsubmit="who(this); return false;">
<input name="id" type="text"/>
<input type="submit"/>
</form>
<div id="display"> <div id="display">
</div> </div>
</body> </body>
<footer> <footer>
<script>
draw({
id: "id",
meta: {
"20200710": "hello",
"20200711": "world\n!",
},
know: [
{
id: "id0",
relation: ":)",
},
{
id: "id1",
relation: ":(",
},
]
})
</script>
</footer> </footer>
</html> </html>

View File

@ -35,6 +35,7 @@ func aesDec(key, payload string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
log.Println(gcm.NonceSize())
if len(ciphertext) < gcm.NonceSize() { if len(ciphertext) < gcm.NonceSize() {
return "", errors.New("short ciphertext") return "", errors.New("short ciphertext")
} }