-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'NewCiphers' of https://github.com/n1073645/CyberChef in…
…to n1073645-NewCiphers
- Loading branch information
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Utils from "../Utils.mjs"; | ||
|
||
/** | ||
* @author n1073645 [n1073645@gmail.com] | ||
* @copyright Crown Copyright 2020 | ||
* @license Apache-2.0 | ||
*/ | ||
export function encode(tempIVP, args, input) { | ||
const ivp = new Uint8Array(Utils.strToByteArray(args[0]).concat(tempIVP)); | ||
const state = new Array(256).fill(0); | ||
let j = 0, i = 0; | ||
const result = []; | ||
|
||
// Mixing states based off of IV. | ||
for (let i = 0; i < 256; i++) | ||
state[i] = i; | ||
const ivpLength = ivp.length; | ||
for (let r = 0; r < args[1]; r ++) { | ||
for (let k = 0; k < 256; k++) { | ||
j = (j + state[k] + ivp[k % ivpLength]) % 256; | ||
[state[k], state[j]] = [state[j], state[k]]; | ||
} | ||
} | ||
j = 0; | ||
i = 0; | ||
|
||
// XOR cipher with key. | ||
for (let x = 0; x < input.length; x++) { | ||
i = (++i) % 256; | ||
j = (j + state[i]) % 256; | ||
[state[i], state[j]] = [state[j], state[i]]; | ||
const n = (state[i] + state[j]) % 256; | ||
result.push(state[n] ^ input[x]); | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @author n1073645 [n1073645@gmail.com] | ||
* @copyright Crown Copyright 2020 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import { encode } from "../lib/CipherSaber2.mjs"; | ||
|
||
/** | ||
* CipherSaber2 Decrypt operation | ||
*/ | ||
class CipherSaber2Decrypt extends Operation { | ||
|
||
/** | ||
* CipherSaber2Decrypt constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "CipherSaber2 Decrypt"; | ||
this.module = "Crypto"; | ||
this.description = "CipherSaber is a simple symmetric encryption protocol based on the RC4 stream cipher. It gives reasonably strong protection of message confidentiality, yet it's designed to be simple enough that even novice programmers can memorize the algorithm and implement it from scratch."; | ||
this.infoURL = "https://wikipedia.org/wiki/CipherSaber"; | ||
this.inputType = "ArrayBuffer"; | ||
this.outputType = "ArrayBuffer"; | ||
this.args = [ | ||
{ | ||
name: "Key", | ||
type: "string", | ||
value: "" | ||
}, | ||
{ | ||
name: "Rounds", | ||
type: "number", | ||
value: 20 | ||
} | ||
]; | ||
} | ||
|
||
/** | ||
* @param {ArrayBuffer} input | ||
* @param {Object[]} args | ||
* @returns {ArrayBuffer} | ||
*/ | ||
run(input, args) { | ||
input = new Uint8Array(input); | ||
const result = []; | ||
|
||
const tempIVP = input.slice(0, 10); | ||
input = input.slice(10); | ||
return new Uint8Array(result.concat(encode(tempIVP, args, input))).buffer; | ||
} | ||
|
||
} | ||
|
||
export default CipherSaber2Decrypt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @author n1073645 [n1073645@gmail.com] | ||
* @copyright Crown Copyright 2020 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import crypto from "crypto"; | ||
import { encode } from "../lib/CipherSaber2.mjs"; | ||
|
||
/** | ||
* CipherSaber2 operation | ||
*/ | ||
class CipherSaber2 extends Operation { | ||
|
||
/** | ||
* CipherSaber2 constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "CipherSaber2 Encrypt"; | ||
this.module = "Crypto"; | ||
this.description = "CipherSaber is a simple symmetric encryption protocol based on the RC4 stream cipher. It gives reasonably strong protection of message confidentiality, yet it's designed to be simple enough that even novice programmers can memorize the algorithm and implement it from scratch."; | ||
this.infoURL = "https://wikipedia.org/wiki/CipherSaber"; | ||
this.inputType = "ArrayBuffer"; | ||
this.outputType = "ArrayBuffer"; | ||
this.args = [ | ||
{ | ||
name: "Key", | ||
type: "string", | ||
value: "" | ||
}, | ||
{ | ||
name: "Rounds", | ||
type: "number", | ||
value: 20 | ||
} | ||
]; | ||
} | ||
|
||
/** | ||
* @param {ArrayBuffer} input | ||
* @param {Object[]} args | ||
* @returns {ArrayBuffer} | ||
*/ | ||
run(input, args) { | ||
input = new Uint8Array(input); | ||
const result = []; | ||
|
||
// Assign into initialisation vector based on cipher mode. | ||
const tempIVP = crypto.randomBytes(10); | ||
for (let m = 0; m < 10; m++) | ||
result.push(tempIVP[m]); | ||
|
||
return new Uint8Array(result.concat(encode(tempIVP, args, input))).buffer; | ||
} | ||
|
||
} | ||
|
||
export default CipherSaber2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Ciphersaber2 tests. | ||
* | ||
* @author n1073645 [n1073645@gmail.com] | ||
* | ||
* @copyright Crown Copyright 2020 | ||
* @license Apache-2.0 | ||
*/ | ||
import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "CipherSaber2 Encrypt", | ||
input: "Hello World", | ||
expectedMatch: /.{21}/s, | ||
recipeConfig: [ | ||
{ | ||
op: "CipherSaber2 Encrypt", | ||
args: ["test", 20], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "CipherSaber2 Decrypt", | ||
input: "\x5d\xd9\x7f\xeb\x77\x3c\x42\x9d\xfe\x9c\x3b\x21\x63\xbd\x53\x38\x18\x7c\x36\x37", | ||
expectedOutput: "helloworld", | ||
recipeConfig: [ | ||
{ | ||
op: "CipherSaber2 Decrypt", | ||
args: ["test", 20], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "CipherSaber2 Encrypt", | ||
input: "", | ||
expectedMatch: /.{10}/s, | ||
recipeConfig: [ | ||
{ | ||
op: "CipherSaber2 Encrypt", | ||
args: ["", 20], | ||
}, | ||
], | ||
}, | ||
]); |