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

View File

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