DESEncrypt.mjs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. import Utils from "../Utils.mjs";
  8. import OperationError from "../errors/OperationError.mjs";
  9. import forge from "node-forge/dist/forge.min.js";
  10. /**
  11. * DES Encrypt operation
  12. */
  13. class DESEncrypt extends Operation {
  14. /**
  15. * DESEncrypt constructor
  16. */
  17. constructor() {
  18. super();
  19. this.name = "DES Encrypt";
  20. this.module = "Ciphers";
  21. this.description = "DES is a previously dominant algorithm for encryption, and was published as an official U.S. Federal Information Processing Standard (FIPS). It is now considered to be insecure due to its small key size.<br><br><b>Key:</b> DES uses a key length of 8 bytes (64 bits).<br>Triple DES uses a key length of 24 bytes (192 bits).<br><br>You can generate a password-based key using one of the KDF operations.<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.<br><br><b>Padding:</b> In CBC and ECB mode, PKCS#7 padding will be used.";
  22. this.infoURL = "https://wikipedia.org/wiki/Data_Encryption_Standard";
  23. this.inputType = "string";
  24. this.outputType = "string";
  25. this.args = [
  26. {
  27. "name": "Key",
  28. "type": "toggleString",
  29. "value": "",
  30. "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
  31. },
  32. {
  33. "name": "IV",
  34. "type": "toggleString",
  35. "value": "",
  36. "toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
  37. },
  38. {
  39. "name": "Mode",
  40. "type": "option",
  41. "value": ["CBC", "CFB", "OFB", "CTR", "ECB"]
  42. },
  43. {
  44. "name": "Input",
  45. "type": "option",
  46. "value": ["Raw", "Hex"]
  47. },
  48. {
  49. "name": "Output",
  50. "type": "option",
  51. "value": ["Hex", "Raw"]
  52. }
  53. ];
  54. }
  55. /**
  56. * @param {string} input
  57. * @param {Object[]} args
  58. * @returns {string}
  59. */
  60. run(input, args) {
  61. const key = Utils.convertToByteString(args[0].string, args[0].option),
  62. iv = Utils.convertToByteArray(args[1].string, args[1].option),
  63. [,, mode, inputType, outputType] = args;
  64. if (key.length !== 8) {
  65. throw new OperationError(`Invalid key length: ${key.length} bytes
  66. DES uses a key length of 8 bytes (64 bits).
  67. Triple DES uses a key length of 24 bytes (192 bits).`);
  68. }
  69. if (iv.length !== 8 && mode !== "ECB") {
  70. throw new OperationError(`Invalid IV length: ${iv.length} bytes
  71. DES uses an IV length of 8 bytes (64 bits).
  72. Make sure you have specified the type correctly (e.g. Hex vs UTF8).`);
  73. }
  74. input = Utils.convertToByteString(input, inputType);
  75. const cipher = forge.cipher.createCipher("DES-" + mode, key);
  76. cipher.start({iv: iv});
  77. cipher.update(forge.util.createBuffer(input));
  78. cipher.finish();
  79. return outputType === "Hex" ? cipher.output.toHex() : cipher.output.getBytes();
  80. }
  81. }
  82. export default DESEncrypt;