Skip to content

Commit 0f733d1

Browse files
committed
Revert "feat: change AES mechanism to crypto-js"
This reverts commit a9ba144.
1 parent 64ecebe commit 0f733d1

5 files changed

Lines changed: 30 additions & 43 deletions

File tree

package-lock.json

Lines changed: 1 addition & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@
4545
"@eslint/js": "^9.39.1",
4646
"@microsoft/api-extractor": "^7.54.0",
4747
"@microsoft/tsdoc": "^0.15.1",
48-
"@types/crypto-js": "^4.2.2",
4948
"@types/node": "^24.10.0",
50-
"crypto-js": "^4.2.0",
5149
"eslint": "^9.39.1",
5250
"eslint-config-prettier": "^10.1.8",
5351
"globals": "^16.5.0",

src/client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { http } from "./helper/http";
2-
import { decryptText } from "./crypto.js";
2+
import { aesDecryptHexWithKey } from "./crypto.js";
33

44
const API_BASE = "https://everybody.codes/api";
55
const CDN_BASE = "https://everybody-codes.b-cdn.net/assets";
@@ -100,7 +100,8 @@ export class EverybodyCodesClient {
100100
(["1", "2", "3"] as const).forEach((p) => {
101101
const hex = encrypted[p];
102102
const key = keys[p];
103-
if (hex && key) out[Number(p) as 1 | 2 | 3] = decryptText(key, hex);
103+
if (hex && key)
104+
out[Number(p) as 1 | 2 | 3] = aesDecryptHexWithKey(key, hex);
104105
});
105106
return out;
106107
}

src/crypto.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import CryptoJS from "crypto-js";
1+
import crypto from "node:crypto";
22

3-
export const decryptText = (key: string, text: string) => {
4-
const cipherParams = CryptoJS.lib.CipherParams.create({
5-
ciphertext: CryptoJS.enc.Hex.parse(text),
6-
});
3+
export function aesDecryptHexWithKey(key: string, hexCipher: string): string {
4+
const keyBytes = Buffer.from(key, "utf8");
5+
const iv = Buffer.from(key.substring(0, 16), "utf8");
6+
const cipherBytes = Buffer.from(hexCipher, "hex");
77

8-
const keyWA = CryptoJS.enc.Utf8.parse(key);
9-
const ivWA = CryptoJS.enc.Utf8.parse(key.substring(0, 16));
8+
const algo =
9+
keyBytes.length === 16
10+
? "aes-128-cbc"
11+
: keyBytes.length === 24
12+
? "aes-192-cbc"
13+
: keyBytes.length === 32
14+
? "aes-256-cbc"
15+
: "aes-256-cbc";
1016

11-
const decrypted = CryptoJS.AES.decrypt(cipherParams, keyWA, { iv: ivWA });
12-
return decrypted.toString(CryptoJS.enc.Utf8);
13-
};
17+
const dec = crypto.createDecipheriv(algo, keyBytes, iv);
18+
const out = Buffer.concat([dec.update(cipherBytes), dec.final()]);
19+
return out.toString("utf8");
20+
}

src/crypto/encryptionHelper.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import CryptoJS from "crypto-js";
1+
import crypto from "crypto";
22

3-
export const createEncryptedHex = (key: string, input: string): string => {
4-
const keyWA = CryptoJS.enc.Utf8.parse(key);
5-
const ivWA = CryptoJS.enc.Utf8.parse(key.substring(0, 16));
6-
7-
const encrypted = CryptoJS.AES.encrypt(input, keyWA, {
8-
iv: ivWA,
9-
mode: CryptoJS.mode.CBC,
10-
padding: CryptoJS.pad.Pkcs7,
11-
});
12-
13-
return encrypted.ciphertext.toString(CryptoJS.enc.Hex);
3+
export const createEncryptedHex = (key: string, input: string) => {
4+
const iv = Buffer.from(key.substring(0, 16), "utf8");
5+
const cipher = crypto.createCipheriv(
6+
"aes-128-cbc",
7+
Buffer.from(key, "utf8"),
8+
iv,
9+
);
10+
return cipher.update(input, "utf8", "hex") + cipher.final("hex");
1411
};

0 commit comments

Comments
 (0)