diff --git a/index.js b/index.js index bdfeb70..fcd018a 100644 --- a/index.js +++ b/index.js @@ -123,6 +123,13 @@ const { encryptIDAES256CCM, decryptIDAES256CCM, encryptIDAES256GCM, decryptIDAES256GCM } = require("./lib/id/index"); +const { + encryptSEED, decryptSEED, + encryptSEEDCBC, decryptSEEDCBC, + encryptSEEDCFB, decryptSEEDCFB, + encryptSEEDECB, decryptSEEDECB, + encryptSEEDOFB, decryptSEEDOFB +} = require("./lib/seed/index"); const { encryptSM4, decryptSM4, encryptSM4CBC, decryptSM4CBC, @@ -259,6 +266,11 @@ module.exports = { encryptIDAES192GCM, decryptIDAES192GCM, encryptIDAES256CCM, decryptIDAES256CCM, encryptIDAES256GCM, decryptIDAES256GCM, + encryptSEED, decryptSEED, + encryptSEEDCBC, decryptSEEDCBC, + encryptSEEDCFB, decryptSEEDCFB, + encryptSEEDECB, decryptSEEDECB, + encryptSEEDOFB, decryptSEEDOFB, encryptSM4, decryptSM4, encryptSM4CBC, decryptSM4CBC, encryptSM4CFB, decryptSM4CFB, diff --git a/lib/algorithmList.js b/lib/algorithmList.js index 9055b7c..8fbcbb9 100644 --- a/lib/algorithmList.js +++ b/lib/algorithmList.js @@ -114,6 +114,11 @@ const algorithmSupported = [ "id-aes192-gcm", "id-aes256-ccm", "id-aes256-gcm", + "seed", + "seed-cbc", + "seed-cfb", + "seed-ecb", + "seed-ofb", "sm4", "sm4-cbc", "sm4-cfb", @@ -174,12 +179,7 @@ const algorithmUnsupported = [ "rc2-64", "rc2-64-cbc", "rc4-cbc", - "rc4-40", - "seed", - "seed-cbc", - "seed-cfb", - "seed-ecb", - "seed-ofb" + "rc4-40" ]; module.exports = { diff --git a/lib/decrypt.js b/lib/decrypt.js index 0c26697..4dd8bdf 100644 --- a/lib/decrypt.js +++ b/lib/decrypt.js @@ -127,6 +127,13 @@ const { decryptIDAES256CCM, decryptIDAES256GCM } = require("./id/index"); +const { + decryptSEED, + decryptSEEDCBC, + decryptSEEDCFB, + decryptSEEDECB, + decryptSEEDOFB +} = require("./seed/index"); const { decryptSM4, decryptSM4CBC, @@ -470,6 +477,21 @@ function decrypt(algorithm, data, password, extra = null) { return decryptIDAES256GCM(data, password, extra); } + /***************************************************** + * SEED * + *****************************************************/ + if (algorithm === "seed") { + return decryptSEED(data, password, extra); + } else if (algorithm === "seed-cbc") { + return decryptSEEDCBC(data, password, extra); + } else if (algorithm === "seed-cfb") { + return decryptSEEDCFB(data, password, extra); + } else if (algorithm === "seed-ecb") { + return decryptSEEDECB(data, password, extra); + } else if (algorithm === "seed-ofb") { + return decryptSEEDOFB(data, password, extra); + } + /***************************************************** * SM4 * *****************************************************/ diff --git a/lib/encrypt.js b/lib/encrypt.js index f292881..582e404 100644 --- a/lib/encrypt.js +++ b/lib/encrypt.js @@ -127,6 +127,13 @@ const { encryptIDAES256CCM, encryptIDAES256GCM } = require("./id/index"); +const { + encryptSEED, + encryptSEEDCBC, + encryptSEEDCFB, + encryptSEEDECB, + encryptSEEDOFB +} = require("./seed/index"); const { encryptSM4, encryptSM4CBC, @@ -470,6 +477,21 @@ function encrypt(algorithm, data, password, extra = null) { return encryptIDAES256GCM(data, password, extra); } + /***************************************************** + * SEED * + *****************************************************/ + if (algorithm === "seed") { + return encryptSEED(data, password, extra); + } else if (algorithm === "seed-cbc") { + return encryptSEEDCBC(data, password, extra); + } else if (algorithm === "seed-cfb") { + return encryptSEEDCFB(data, password, extra); + } else if (algorithm === "seed-ecb") { + return encryptSEEDECB(data, password, extra); + } else if (algorithm === "seed-ofb") { + return encryptSEEDOFB(data, password, extra); + } + /***************************************************** * SM4 * *****************************************************/ diff --git a/lib/seed/index.js b/lib/seed/index.js new file mode 100644 index 0000000..78bb082 --- /dev/null +++ b/lib/seed/index.js @@ -0,0 +1,13 @@ +const { encryptSEED, decryptSEED } = require("./seed"); +const { encryptSEEDCBC, decryptSEEDCBC } = require("./seed-cbc"); +const { encryptSEEDCFB, decryptSEEDCFB } = require("./seed-cfb"); +const { encryptSEEDECB, decryptSEEDECB } = require("./seed-ecb"); +const { encryptSEEDOFB, decryptSEEDOFB } = require("./seed-ofb"); + +module.exports = { + encryptSEED, decryptSEED, + encryptSEEDCBC, decryptSEEDCBC, + encryptSEEDCFB, decryptSEEDCFB, + encryptSEEDECB, decryptSEEDECB, + encryptSEEDOFB, decryptSEEDOFB +}; diff --git a/lib/seed/seed-cbc.js b/lib/seed/seed-cbc.js new file mode 100644 index 0000000..1a11e82 --- /dev/null +++ b/lib/seed/seed-cbc.js @@ -0,0 +1,67 @@ +const crypto = require("crypto"); +const zlib = require("zlib"); + +/** + * Encrypts data using the SEED-CBC algorithm. + * + * @param {string} data - The data to be encrypted. + * @param {string} password - The password used for key derivation. + * @param {string|null} extra - Additional options for encryption (e.g., 'base64', 'binary', 'hex', or 'zlib'). + * @returns {Buffer|string} - The encrypted data. + */ +function encryptSEEDCBC(data, password, extra = null) { + const salt = crypto.randomBytes(8); + const iv = crypto.randomBytes(16); + const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256"); + const cipher = crypto.createCipheriv("seed-cbc", key, iv); + var encrypted = Buffer.concat([Buffer.from("Salted__"), salt, iv, cipher.update(data, "utf-8"), cipher.final()]); + if (extra === "base64") { + var encrypted = encrypted.toString("base64"); + } else if (extra === "binary") { + var encrypted = encrypted.toString("binary"); + } else if (extra === "hex") { + var encrypted = encrypted.toString("hex"); + } else if (extra === "zlib") { + var encrypted = zlib.deflateSync(encrypted); + } + + return encrypted; +} + +/** + * Decrypts data using the SEED-CBC algorithm. + * + * @param {Buffer|string} data - The data to be decrypted. + * @param {string} password - The password used for key derivation. + * @param {string|null} extra - Additional options for decryption (e.g., 'base64', 'binary', 'hex', or 'zlib'). + * @returns {string} - The decrypted data. + */ +function decryptSEEDCBC(encryptedData, password, extra = null) { + var data = encryptedData; + if (extra === "base64") { + var data = Buffer.from(data, "base64"); + } else if (extra === "binary") { + var data = Buffer.from(data, "binary"); + } else if (extra === "hex") { + var data = Buffer.from(data, "hex"); + } else if (extra === "zlib") { + var data = zlib.inflateSync(data); + } + + if (! Buffer.isBuffer(data)) { + var buffString = data; + var hexValues = buffString.replace(//g, "").split(" ").filter(Boolean); + var bufferArray = hexValues.map(hex => parseInt(hex, 16)); + var data = Buffer.from(bufferArray); + } + + const salt = data.slice(8, 16); + const iv = data.slice(16, 32); + const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256"); + const decipher = crypto.createDecipheriv("seed-cbc", key, iv); + const decrypted = Buffer.concat([decipher.update(data.slice(32)), decipher.final()]).toString("utf-8"); + + return decrypted; +} + +module.exports = { encryptSEEDCBC, decryptSEEDCBC }; diff --git a/lib/seed/seed-cfb.js b/lib/seed/seed-cfb.js new file mode 100644 index 0000000..ec4049c --- /dev/null +++ b/lib/seed/seed-cfb.js @@ -0,0 +1,67 @@ +const crypto = require("crypto"); +const zlib = require("zlib"); + +/** + * Encrypts data using the SEED-CFB algorithm. + * + * @param {string} data - The data to be encrypted. + * @param {string} password - The password used for key derivation. + * @param {string|null} extra - Additional options for encryption (e.g., 'base64', 'binary', 'hex', or 'zlib'). + * @returns {Buffer|string} - The encrypted data. + */ +function encryptSEEDCFB(data, password, extra = null) { + const salt = crypto.randomBytes(8); + const iv = crypto.randomBytes(16); + const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256"); + const cipher = crypto.createCipheriv("seed-cfb", key, iv); + var encrypted = Buffer.concat([Buffer.from("Salted__"), salt, iv, cipher.update(data, "utf-8"), cipher.final()]); + if (extra === "base64") { + var encrypted = encrypted.toString("base64"); + } else if (extra === "binary") { + var encrypted = encrypted.toString("binary"); + } else if (extra === "hex") { + var encrypted = encrypted.toString("hex"); + } else if (extra === "zlib") { + var encrypted = zlib.deflateSync(encrypted); + } + + return encrypted; +} + +/** + * Decrypts data using the SEED-CFB algorithm. + * + * @param {Buffer|string} data - The data to be decrypted. + * @param {string} password - The password used for key derivation. + * @param {string|null} extra - Additional options for decryption (e.g., 'base64', 'binary', 'hex', or 'zlib'). + * @returns {string} - The decrypted data. + */ +function decryptSEEDCFB(encryptedData, password, extra = null) { + var data = encryptedData; + if (extra === "base64") { + var data = Buffer.from(data, "base64"); + } else if (extra === "binary") { + var data = Buffer.from(data, "binary"); + } else if (extra === "hex") { + var data = Buffer.from(data, "hex"); + } else if (extra === "zlib") { + var data = zlib.inflateSync(data); + } + + if (! Buffer.isBuffer(data)) { + var buffString = data; + var hexValues = buffString.replace(//g, "").split(" ").filter(Boolean); + var bufferArray = hexValues.map(hex => parseInt(hex, 16)); + var data = Buffer.from(bufferArray); + } + + const salt = data.slice(8, 16); + const iv = data.slice(16, 32); + const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256"); + const decipher = crypto.createDecipheriv("seed-cfb", key, iv); + const decrypted = Buffer.concat([decipher.update(data.slice(32)), decipher.final()]).toString("utf-8"); + + return decrypted; +} + +module.exports = { encryptSEEDCFB, decryptSEEDCFB }; diff --git a/lib/seed/seed-ecb.js b/lib/seed/seed-ecb.js new file mode 100644 index 0000000..d03de29 --- /dev/null +++ b/lib/seed/seed-ecb.js @@ -0,0 +1,65 @@ +const crypto = require("crypto"); +const zlib = require("zlib"); + +/** + * Encrypts data using the SEED-ECB algorithm. + * + * @param {string} data - The data to be encrypted. + * @param {string} password - The password used for key derivation. + * @param {string|null} extra - Additional options for encryption (e.g., 'base64', 'binary', 'hex', or 'zlib'). + * @returns {Buffer|string} - The encrypted data. + */ +function encryptSEEDECB(data, password, extra = null) { + const salt = crypto.randomBytes(8); + const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256"); + const cipher = crypto.createCipheriv("seed-ecb", key, null); + var encrypted = Buffer.concat([Buffer.from("Salted__"), salt, cipher.update(data, "utf-8"), cipher.final()]); + if (extra === "base64") { + var encrypted = encrypted.toString("base64"); + } else if (extra === "binary") { + var encrypted = encrypted.toString("binary"); + } else if (extra === "hex") { + var encrypted = encrypted.toString("hex"); + } else if (extra === "zlib") { + var encrypted = zlib.deflateSync(encrypted); + } + + return encrypted; +} + +/** + * Decrypts data using the SEED-ECB algorithm. + * + * @param {Buffer|string} data - The data to be decrypted. + * @param {string} password - The password used for key derivation. + * @param {string|null} extra - Additional options for decryption (e.g., 'base64', 'binary', 'hex', or 'zlib'). + * @returns {string} - The decrypted data. + */ +function decryptSEEDECB(encryptedData, password, extra = null) { + var data = encryptedData; + if (extra === "base64") { + var data = Buffer.from(data, "base64"); + } else if (extra === "binary") { + var data = Buffer.from(data, "binary"); + } else if (extra === "hex") { + var data = Buffer.from(data, "hex"); + } else if (extra === "zlib") { + var data = zlib.inflateSync(data); + } + + if (! Buffer.isBuffer(data)) { + var buffString = data; + var hexValues = buffString.replace(//g, "").split(" ").filter(Boolean); + var bufferArray = hexValues.map(hex => parseInt(hex, 16)); + var data = Buffer.from(bufferArray); + } + + const salt = data.slice(8, 16); + const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256"); + const decipher = crypto.createDecipheriv("seed-ecb", key, null); + const decrypted = Buffer.concat([decipher.update(data.slice(16)), decipher.final()]).toString("utf-8"); + + return decrypted; +} + +module.exports = { encryptSEEDECB, decryptSEEDECB }; diff --git a/lib/seed/seed-ofb.js b/lib/seed/seed-ofb.js new file mode 100644 index 0000000..7079447 --- /dev/null +++ b/lib/seed/seed-ofb.js @@ -0,0 +1,67 @@ +const crypto = require("crypto"); +const zlib = require("zlib"); + +/** + * Encrypts data using the SEED-OFB algorithm. + * + * @param {string} data - The data to be encrypted. + * @param {string} password - The password used for key derivation. + * @param {string|null} extra - Additional options for encryption (e.g., 'base64', 'binary', 'hex', or 'zlib'). + * @returns {Buffer|string} - The encrypted data. + */ +function encryptSEEDOFB(data, password, extra = null) { + const salt = crypto.randomBytes(8); + const iv = crypto.randomBytes(16); + const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256"); + const cipher = crypto.createCipheriv("seed-ofb", key, iv); + var encrypted = Buffer.concat([Buffer.from("Salted__"), salt, iv, cipher.update(data, "utf-8"), cipher.final()]); + if (extra === "base64") { + var encrypted = encrypted.toString("base64"); + } else if (extra === "binary") { + var encrypted = encrypted.toString("binary"); + } else if (extra === "hex") { + var encrypted = encrypted.toString("hex"); + } else if (extra === "zlib") { + var encrypted = zlib.deflateSync(encrypted); + } + + return encrypted; +} + +/** + * Decrypts data using the SEED-OFB algorithm. + * + * @param {Buffer|string} data - The data to be decrypted. + * @param {string} password - The password used for key derivation. + * @param {string|null} extra - Additional options for decryption (e.g., 'base64', 'binary', 'hex', or 'zlib'). + * @returns {string} - The decrypted data. + */ +function decryptSEEDOFB(encryptedData, password, extra = null) { + var data = encryptedData; + if (extra === "base64") { + var data = Buffer.from(data, "base64"); + } else if (extra === "binary") { + var data = Buffer.from(data, "binary"); + } else if (extra === "hex") { + var data = Buffer.from(data, "hex"); + } else if (extra === "zlib") { + var data = zlib.inflateSync(data); + } + + if (! Buffer.isBuffer(data)) { + var buffString = data; + var hexValues = buffString.replace(//g, "").split(" ").filter(Boolean); + var bufferArray = hexValues.map(hex => parseInt(hex, 16)); + var data = Buffer.from(bufferArray); + } + + const salt = data.slice(8, 16); + const iv = data.slice(16, 32); + const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256"); + const decipher = crypto.createDecipheriv("seed-ofb", key, iv); + const decrypted = Buffer.concat([decipher.update(data.slice(32)), decipher.final()]).toString("utf-8"); + + return decrypted; +} + +module.exports = { encryptSEEDOFB, decryptSEEDOFB }; diff --git a/lib/seed/seed.js b/lib/seed/seed.js new file mode 100644 index 0000000..4b1cd4a --- /dev/null +++ b/lib/seed/seed.js @@ -0,0 +1,67 @@ +const crypto = require("crypto"); +const zlib = require("zlib"); + +/** + * Encrypts data using the SEED algorithm. + * + * @param {string} data - The data to be encrypted. + * @param {string} password - The password used for key derivation. + * @param {string|null} extra - Additional options for encryption (e.g., 'base64', 'binary', 'hex', or 'zlib'). + * @returns {Buffer|string} - The encrypted data. + */ +function encryptSEED(data, password, extra = null) { + const salt = crypto.randomBytes(8); + const iv = crypto.randomBytes(16); + const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256"); + const cipher = crypto.createCipheriv("seed", key, iv); + var encrypted = Buffer.concat([Buffer.from("Salted__"), salt, iv, cipher.update(data, "utf-8"), cipher.final()]); + if (extra === "base64") { + var encrypted = encrypted.toString("base64"); + } else if (extra === "binary") { + var encrypted = encrypted.toString("binary"); + } else if (extra === "hex") { + var encrypted = encrypted.toString("hex"); + } else if (extra === "zlib") { + var encrypted = zlib.deflateSync(encrypted); + } + + return encrypted; +} + +/** + * Decrypts data using the SEED algorithm. + * + * @param {Buffer|string} data - The data to be decrypted. + * @param {string} password - The password used for key derivation. + * @param {string|null} extra - Additional options for decryption (e.g., 'base64', 'binary', 'hex', or 'zlib'). + * @returns {string} - The decrypted data. + */ +function decryptSEED(encryptedData, password, extra = null) { + var data = encryptedData; + if (extra === "base64") { + var data = Buffer.from(data, "base64"); + } else if (extra === "binary") { + var data = Buffer.from(data, "binary"); + } else if (extra === "hex") { + var data = Buffer.from(data, "hex"); + } else if (extra === "zlib") { + var data = zlib.inflateSync(data); + } + + if (! Buffer.isBuffer(data)) { + var buffString = data; + var hexValues = buffString.replace(//g, "").split(" ").filter(Boolean); + var bufferArray = hexValues.map(hex => parseInt(hex, 16)); + var data = Buffer.from(bufferArray); + } + + const salt = data.slice(8, 16); + const iv = data.slice(16, 32); + const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256"); + const decipher = crypto.createDecipheriv("seed", key, iv); + const decrypted = Buffer.concat([decipher.update(data.slice(32)), decipher.final()]).toString("utf-8"); + + return decrypted; +} + +module.exports = { encryptSEED, decryptSEED };