li.shaoyi 3 سال پیش
والد
کامیت
0108753d3b
1فایلهای تغییر یافته به همراه31 افزوده شده و 0 حذف شده
  1. 31 0
      src/utils/websocket/crypto.ts

+ 31 - 0
src/utils/websocket/crypto.ts

@@ -6,6 +6,8 @@ const aeskey: CryptoJS.lib.WordArray = CryptoJS.enc.Utf8.parse('F7A72DE7D6264530
 const macKeyLeft: CryptoJS.lib.WordArray = CryptoJS.enc.Hex.parse('B0FB83E39A5EBFAA'); // 这里要用CryptoJS.enc.Hex.parse,CryptoJS.enc.Utf8.parse返回是16位的
 /** MAC检验码右8字节 */
 const macKeyRight: CryptoJS.lib.WordArray = CryptoJS.enc.Hex.parse('BE471362A58393FF');
+/** Phone AES密钥 */
+const phoneaeskey = '0d299ce2d4105282f7471074cb0f9f9d';
 /** MAC检验向量 */
 const iv = new Uint8Array([0xd9, 0x51, 0xdb, 0xe0, 0x37, 0xc8, 0x23, 0x25]);
 
@@ -150,3 +152,32 @@ export const decrypt50 = (encryptData: Uint8Array, size: number): Uint8Array | n
 
     return a.subarray(0, size);
 };
+
+/**
+ * AES数据解密方法
+ * @param encryptData 密文
+ * @param size 明文长度
+ */
+export const decryptAES = (value: string): string | undefined => {
+    const ciphertext = hexStringToUint8Array(value);
+    const key = hexStringToUint8Array(phoneaeskey);
+
+    const cipherParams = CryptoJS.lib.CipherParams.create({
+        ciphertext: uint8ArrayToWordArray(ciphertext),
+    });
+
+    const decrytped = CryptoJS.AES.decrypt(cipherParams, uint8ArrayToWordArray(key), aesOption);
+    const h = wordArrayToUint8Array(decrytped).subarray(0, decrytped.sigBytes);
+    const result = new TextDecoder().decode(h);
+    return result;
+}
+
+/**
+ * 十六进制字符串转Uint8Array
+ * @param hex 
+ * @returns 
+ */
+function hexStringToUint8Array(hex: string) {
+    const buffer = hex.match(/.{1,2}/g)?.map((byte) => parseInt(byte, 16));
+    return new Uint8Array(buffer ?? []);
+}