mirror of
https://github.com/planetrenox/inzerosight.git
synced 2026-03-17 03:01:02 +00:00
Refactor: Use split cipher modules, upgrade to 48/96
This commit is contained in:
74
dash.js
74
dash.js
@@ -1,54 +1,13 @@
|
|||||||
import zwus from 'zwus';
|
import zwus from 'zwus';
|
||||||
import { blake2bHex } from 'blakejs';
|
import * as speck48_96ctr from './speck48_96ctr.js';
|
||||||
import speck from 'generic-speck';
|
import * as speck32_64ecb from './speck32_64ecb.js';
|
||||||
|
|
||||||
const speck32_64 = speck();
|
|
||||||
const textarea = document.getElementById('textarea');
|
const textarea = document.getElementById('textarea');
|
||||||
const encoderDropdown = document.getElementById('encoder');
|
const encoderDropdown = document.getElementById('encoder');
|
||||||
const cipherDropdown = document.getElementById('cipher');
|
const cipherDropdown = document.getElementById('cipher');
|
||||||
document.getElementById('encodeButton').addEventListener('click', ACT);
|
document.getElementById('encodeButton').addEventListener('click', ACT);
|
||||||
document.getElementById('decodeButton').addEventListener('click', ACT);
|
document.getElementById('decodeButton').addEventListener('click', ACT);
|
||||||
|
|
||||||
function getKey(kStr) {
|
|
||||||
const key64bit = blake2bHex(kStr, null, 8);
|
|
||||||
return [
|
|
||||||
parseInt(key64bit.slice(0, 4), 16),
|
|
||||||
parseInt(key64bit.slice(4, 8), 16),
|
|
||||||
parseInt(key64bit.slice(8, 12), 16),
|
|
||||||
parseInt(key64bit.slice(12, 16), 16)
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
function deriveNonce(key64arr) {
|
|
||||||
return speck32_64.encrypt(0, key64arr) & 0xFFFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
function ctrKeystream(key64arr, index) {
|
|
||||||
const nonce = deriveNonce(key64arr);
|
|
||||||
const ctrBlock = ((nonce << 16) | (index & 0xFFFF)) >>> 0;
|
|
||||||
return speck32_64.encrypt(ctrBlock, key64arr) >>> 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function ctrEncrypt(ptStr, key64arr) {
|
|
||||||
return Array.from(ptStr, (c, i) => {
|
|
||||||
const ks = ctrKeystream(key64arr, i);
|
|
||||||
return (c.codePointAt(0) ^ ks) >>> 0;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function ctrDecrypt(numArr, key64arr) {
|
|
||||||
return numArr.map((ct, i) => {
|
|
||||||
const ks = ctrKeystream(key64arr, i);
|
|
||||||
const cp = (ct ^ ks) >>> 0;
|
|
||||||
try { return String.fromCodePoint(cp); }
|
|
||||||
catch { return ''; }
|
|
||||||
}).join('');
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCipherKey() {
|
|
||||||
return cipherDropdown.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
function ACT(event) {
|
function ACT(event) {
|
||||||
if (textarea.value === '') {
|
if (textarea.value === '') {
|
||||||
textarea.value = 'The text box is empty.';
|
textarea.value = 'The text box is empty.';
|
||||||
@@ -76,30 +35,25 @@ function ACT(event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCipherKey() {
|
||||||
|
return cipherDropdown.value;
|
||||||
|
}
|
||||||
|
|
||||||
const DESCRY = {
|
const DESCRY = {
|
||||||
NO: {
|
NO: {
|
||||||
PLAIN: (ptStr, base) =>
|
PLAIN: (ptStr, base) =>
|
||||||
zwus.encodeString(ptStr, base),
|
zwus.encodeString(ptStr, base),
|
||||||
SPECK32_64CTR: (ptStr, base, kStr) =>
|
SPECK48_96CTR: (ptStr, base, kStr) =>
|
||||||
zwus.encodeNumberArray(ctrEncrypt(ptStr, getKey(kStr)), base),
|
zwus.encodeNumberArray(speck48_96ctr.encrypt(ptStr, speck48_96ctr.getKey(kStr)), base),
|
||||||
'SPECK32_64ECB (insecure)': (ptStr, base, kStr) => {
|
'SPECK32_64ECB (insecure)': (ptStr, base, kStr) =>
|
||||||
const key = getKey(kStr);
|
zwus.encodeNumberArray(speck32_64ecb.encrypt(ptStr, speck32_64ecb.getKey(kStr)), base),
|
||||||
return zwus.encodeNumberArray(
|
|
||||||
Array.from(ptStr, c => speck32_64.encrypt(c.codePointAt(0), key)),
|
|
||||||
base
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
YES: {
|
YES: {
|
||||||
PLAIN: (ptStr, base) =>
|
PLAIN: (ptStr, base) =>
|
||||||
zwus.decodeToString(ptStr, base),
|
zwus.decodeToString(ptStr, base),
|
||||||
SPECK32_64CTR: (ptStr, base, kStr) =>
|
SPECK48_96CTR: (ptStr, base, kStr) =>
|
||||||
ctrDecrypt(zwus.decodeToNumberArray(ptStr, base), getKey(kStr)),
|
speck48_96ctr.decrypt(zwus.decodeToNumberArray(ptStr, base), speck48_96ctr.getKey(kStr)),
|
||||||
'SPECK32_64ECB (insecure)': (ptStr, base, kStr) => {
|
'SPECK32_64ECB (insecure)': (ptStr, base, kStr) =>
|
||||||
const key = getKey(kStr);
|
speck32_64ecb.decrypt(zwus.decodeToNumberArray(ptStr, base), speck32_64ecb.getKey(kStr)),
|
||||||
return zwus.decodeToNumberArray(ptStr, base)
|
|
||||||
.map(x => { try { return String.fromCodePoint(speck32_64.decrypt(x, key)); } catch { return ''; } })
|
|
||||||
.join('');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user