diff --git a/package.json b/package.json index 356b83f8..172d2b1e 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "@kenjiuno/decompressrtf": "^0.1.4", "@kenjiuno/msgreader": "^1.22.0", "@mdit/plugin-align": "^0.16.0", + "@noble/ciphers": "^1.2.1", "@regexper/render": "^1.0.0", "@sindresorhus/slugify": "^2.2.1", "@tabler/icons-vue": "^3.20.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index babc3d23..35802697 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -56,6 +56,9 @@ dependencies: '@mdit/plugin-align': specifier: ^0.16.0 version: 0.16.0(markdown-it@14.1.0) + '@noble/ciphers': + specifier: ^1.2.1 + version: 1.2.1 '@regexper/render': specifier: ^1.0.0 version: 1.0.0 @@ -5098,6 +5101,11 @@ packages: dev: false optional: true + /@noble/ciphers@1.2.1: + resolution: {integrity: sha512-rONPWMC7PeExE077uLE4oqWrZ1IvAfz3oH9LibVAcVCopJiA9R62uavnbEzdkVmJYI6M6Zgkbeb07+tWjlq2XA==} + engines: {node: ^14.21.3 || >=16} + dev: false + /@nodelib/fs.scandir@2.1.5: resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==} engines: {node: '>= 8'} diff --git a/src/tools/encryption/encryption.service.ts b/src/tools/encryption/encryption.service.ts new file mode 100644 index 00000000..dd720a12 --- /dev/null +++ b/src/tools/encryption/encryption.service.ts @@ -0,0 +1,74 @@ +import { Buffer } from 'node:buffer'; +import { RC4, Rabbit, TripleDES, enc } from 'crypto-js'; +import { managedNonce, randomBytes } from '@noble/ciphers/webcrypto'; +import { aeskw, aeskwp, cbc, cfb, ctr, ecb, gcm, siv } from '@noble/ciphers/aes'; +import { salsa20, xsalsa20, xsalsa20poly1305 } from '@noble/ciphers/salsa'; +import { chacha12, chacha20, chacha20poly1305, chacha8, xchacha20, xchacha20poly1305 } from '@noble/ciphers/chacha'; +import { bytesToUtf8, hexToBytes, utf8ToBytes } from '@noble/ciphers/utils'; + +function getCryptoJSKey(key: string, keyEncoding: KeyEncoding) { + return keyEncoding === 'Text' ? key : enc.Hex.parse(key); +} +function getNobleKey(key: string, keyEncoding: KeyEncoding) { + return keyEncoding === 'Text' ? utf8ToBytes(key) : hexToBytes(key); +} + +function getCryptoJSCipher(cipher: any) { + return { + encrypt: (value: string, key: string, keyEncoding: KeyEncoding) => cipher.encrypt(value, getCryptoJSKey(key, keyEncoding), { iv: enc.Hex.parse('') }).toString(), + decrypt: (value: string, key: string, keyEncoding: KeyEncoding) => cipher.decrypt(value, getCryptoJSKey(key, keyEncoding), { iv: enc.Hex.parse('') }).toString(enc.Utf8), + }; +} +function getNobleCipher(algo: any, nonceType: 'auto' | 'none' | 'manual' = 'auto', nonceSize: number = 0) { + return { + encrypt: (value: string, key: string, keyEncoding: KeyEncoding) => { + if (nonceType === 'auto') { + return Buffer.from(managedNonce(algo)(getNobleKey(key, keyEncoding)).encrypt(utf8ToBytes(value))).toString('base64'); + } + else if (nonceType === 'none') { + return Buffer.from(algo(getNobleKey(key, keyEncoding)).encrypt(utf8ToBytes(value))).toString('base64'); + } + else { + const nonce = randomBytes(nonceSize); + return Buffer.from(new Uint8Array([...nonce, ...algo(getNobleKey(key, keyEncoding), nonce, utf8ToBytes(value))])).toString('base64'); + } + }, + decrypt: (value: string, key: string, keyEncoding: KeyEncoding) => { + if (nonceType === 'auto') { + return bytesToUtf8(managedNonce(algo)(getNobleKey(key, keyEncoding)).decrypt(Buffer.from(value, 'base64'))); + } + else if (nonceType === 'none') { + return bytesToUtf8(algo(getNobleKey(key, keyEncoding)).decrypt(Buffer.from(value, 'base64'))); + } + else { + const ciphered = Buffer.from(value, 'base64'); + return bytesToUtf8(algo(getNobleKey(key, keyEncoding), ciphered.subarray(0, nonceSize), ciphered.subarray(nonceSize))); + } + }, + }; +} + +export const algos = { + 'TripleDES': getCryptoJSCipher(TripleDES), + 'Rabbit': getCryptoJSCipher(Rabbit), + 'RC4': getCryptoJSCipher(RC4), + 'AES-GCM': getNobleCipher(gcm), + 'AES-SIV': getNobleCipher(siv), + 'AES-CTR': getNobleCipher(ctr), + 'AES-CFB': getNobleCipher(cfb), + 'AES-CBC': getNobleCipher(cbc), + 'AES-ECB': getNobleCipher(ecb, 'none'), + 'AES-KW': getNobleCipher(aeskw, 'none'), + 'AES-KWP': getNobleCipher(aeskwp, 'none'), + 'Salsa-XSalsa20poly1305': getNobleCipher(xsalsa20poly1305), + 'Chacha-Chacha20poly1305': getNobleCipher(chacha20poly1305), + 'Chacha-XChacha20poly1305': getNobleCipher(xchacha20poly1305), + 'Salsa-Salsa20': getNobleCipher(salsa20, 'manual', 8), + 'Salsa-XSalsa20': getNobleCipher(xsalsa20, 'manual', 24), + 'Chacha-Chacha20': getNobleCipher(chacha20, 'manual', 12), + 'Chacha-XChacha20': getNobleCipher(xchacha20, 'manual', 24), + 'Chacha-Chacha8': getNobleCipher(chacha8, 'manual', 12), + 'Chacha-Chacha12 ': getNobleCipher(chacha12, 'manual', 12), +}; + +export type KeyEncoding = 'Text' | 'Hex'; diff --git a/src/tools/encryption/encryption.vue b/src/tools/encryption/encryption.vue index 42113254..3ccf324d 100644 --- a/src/tools/encryption/encryption.vue +++ b/src/tools/encryption/encryption.vue @@ -1,24 +1,22 @@