mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-22 07:46:16 -04:00
Tidied up and added tests for RSA operations
This commit is contained in:
parent
99eb1cced5
commit
74ae77f17a
11 changed files with 70 additions and 50 deletions
|
@ -25,7 +25,7 @@ class GeneratePGPKeyPair extends Operation {
|
|||
|
||||
this.name = "Generate PGP Key Pair";
|
||||
this.module = "PGP";
|
||||
this.description = "Generates a new public/private PGP key pair. Supports RSA and Eliptic Curve (EC) keys.";
|
||||
this.description = "Generates a new public/private PGP key pair. Supports RSA and Eliptic Curve (EC) keys.<br><br>WARNING: Cryptographic operations in CyberChef should not be relied upon to provide security in any situation. No guarantee is offered for their correctness. We advise you not to use keys generated from CyberChef in operational contexts.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Pretty_Good_Privacy";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import forge from "node-forge/dist/forge.min.js";
|
||||
import Operation from "../Operation.mjs";
|
||||
import forge from "node-forge";
|
||||
|
||||
/**
|
||||
* Generate RSA Key Pair operation
|
||||
|
@ -21,7 +21,7 @@ class GenerateRSAKeyPair extends Operation {
|
|||
|
||||
this.name = "Generate RSA Key Pair";
|
||||
this.module = "Ciphers";
|
||||
this.description = "Generate an RSA key pair with a given number of bits";
|
||||
this.description = "Generate an RSA key pair with a given number of bits.<br><br>WARNING: Cryptographic operations in CyberChef should not be relied upon to provide security in any situation. No guarantee is offered for their correctness. We advise you not to use keys generated from CyberChef in operational contexts.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/RSA_(cryptosystem)";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
|
@ -56,7 +56,11 @@ class GenerateRSAKeyPair extends Operation {
|
|||
const [keyLength, outputFormat] = args;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
forge.pki.rsa.generateKeyPair({ bits: Number(keyLength), workers: -1, workerScript: "assets/forge/prime.worker.min.js"}, (err, keypair) => {
|
||||
forge.pki.rsa.generateKeyPair({
|
||||
bits: Number(keyLength),
|
||||
workers: -1,
|
||||
workerScript: "assets/forge/prime.worker.min.js"
|
||||
}, (err, keypair) => {
|
||||
if (err) return reject(err);
|
||||
|
||||
let result;
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import forge from "node-forge/dist/forge.min.js";
|
||||
import forge from "node-forge";
|
||||
import { MD_ALGORITHMS } from "../lib/RSA.mjs";
|
||||
|
||||
/**
|
||||
|
@ -25,7 +24,7 @@ class RSADecrypt extends Operation {
|
|||
this.module = "Ciphers";
|
||||
this.description = "Decrypt an RSA encrypted message with a PEM encoded private key.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/RSA_(cryptosystem)";
|
||||
this.inputType = "ArrayBuffer";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
|
@ -75,8 +74,8 @@ class RSADecrypt extends Operation {
|
|||
}
|
||||
try {
|
||||
const privKey = forge.pki.decryptRsaPrivateKey(pemKey, password);
|
||||
const dMsg = privKey.decrypt(Utils.arrayBufferToStr(input), scheme, {md: MD_ALGORITHMS[md].create()});
|
||||
return dMsg;
|
||||
const dMsg = privKey.decrypt(input, scheme, {md: MD_ALGORITHMS[md].create()});
|
||||
return forge.util.decodeUtf8(dMsg);
|
||||
} catch (err) {
|
||||
throw new OperationError(err);
|
||||
}
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import forge from "node-forge/dist/forge.min.js";
|
||||
import forge from "node-forge";
|
||||
import { MD_ALGORITHMS } from "../lib/RSA.mjs";
|
||||
|
||||
/**
|
||||
|
@ -26,7 +25,7 @@ class RSAEncrypt extends Operation {
|
|||
this.description = "Encrypt a message with a PEM encoded RSA public key.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/RSA_(cryptosystem)";
|
||||
this.inputType = "string";
|
||||
this.outputType = "ArrayBuffer";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
name: "RSA Public Key (PEM)",
|
||||
|
@ -72,9 +71,11 @@ class RSAEncrypt extends Operation {
|
|||
try {
|
||||
// Load public key
|
||||
const pubKey = forge.pki.publicKeyFromPem(pemKey);
|
||||
// https://github.com/digitalbazaar/forge/issues/465#issuecomment-271097600
|
||||
const plaintextBytes = forge.util.encodeUtf8(input);
|
||||
// Encrypt message
|
||||
const eMsg = pubKey.encrypt(input, scheme, {md: MD_ALGORITHMS[md].create()});
|
||||
return Utils.strToArrayBuffer(eMsg);
|
||||
const eMsg = pubKey.encrypt(plaintextBytes, scheme, {md: MD_ALGORITHMS[md].create()});
|
||||
return eMsg;
|
||||
} catch (err) {
|
||||
if (err.message === "RSAES-OAEP input message length is too long.") {
|
||||
throw new OperationError(`RSAES-OAEP input message length (${err.length}) is longer than the maximum allowed length (${err.maxLength}).`);
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import OperationError from "../errors/OperationError";
|
||||
import forge from "node-forge/dist/forge.min.js";
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import forge from "node-forge";
|
||||
import { MD_ALGORITHMS } from "../lib/RSA.mjs";
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import forge from "node-forge/dist/forge.min.js";
|
||||
import forge from "node-forge";
|
||||
import { MD_ALGORITHMS } from "../lib/RSA.mjs";
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue