crypto.uts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /**
  2. * AES 加密工具
  3. * 使用 AES-128-CBC 模式,PKCS7/PKCS5 填充
  4. */
  5. // 加密密钥和 IV(与后端保持一致)
  6. const KEY_STR = 'ygtxgxtkey030206'
  7. const IV_STR = 'ygtxgxt-iv030206'
  8. /**
  9. * AES 加密
  10. * @param word 需要加密的字符串
  11. * @returns 加密后的 Base64 字符串
  12. */
  13. export const encryptAES = (word: string): string => {
  14. // #ifdef APP-ANDROID
  15. return encryptAESAndroid(word, KEY_STR, IV_STR)
  16. // #endif
  17. // #ifdef APP-HARMONY
  18. return encryptAESHarmony(word, KEY_STR, IV_STR)
  19. // #endif
  20. // #ifdef APP-IOS
  21. return encryptAESiOS(word, KEY_STR, IV_STR)
  22. // #endif
  23. // #ifdef WEB
  24. return encryptAESWeb(word, KEY_STR, IV_STR)
  25. // #endif
  26. // #ifndef APP-ANDROID || APP-HARMONY || WEB
  27. // 其他平台暂时返回原文
  28. console.warn('当前平台暂不支持 AES 加密,返回原文')
  29. return word
  30. // #endif
  31. }
  32. /**
  33. * AES 解密
  34. * @param word 需要解密的 Base64 字符串
  35. * @returns 解密后的字符串
  36. */
  37. export const decryptAES = (word: string): string => {
  38. // #ifdef APP-ANDROID
  39. console.warn('APP-ANDROID')
  40. return decryptAESAndroid(word, KEY_STR, IV_STR)
  41. // #endif
  42. // #ifdef APP-HARMONY
  43. console.warn('APP-HARMONY')
  44. return decryptAESHarmony(word, KEY_STR, IV_STR)
  45. // #endif
  46. // #ifdef APP-IOS
  47. return decryptAESiOS(word, KEY_STR, IV_STR)
  48. // #endif
  49. // #ifdef WEB
  50. return decryptAESWeb(word, KEY_STR, IV_STR)
  51. // #endif
  52. // #ifndef APP-ANDROID || APP-HARMONY || WEB
  53. // 其他平台暂时返回原文
  54. console.warn('当前平台暂不支持 AES 解密,返回原文')
  55. return word
  56. // #endif
  57. }
  58. // Android 平台实现(使用条件编译)
  59. // #ifdef APP-ANDROID
  60. /**
  61. * Android 平台 AES 加密实现
  62. */
  63. function encryptAESAndroid(plainText: string, keyStr: string, ivStr: string): string {
  64. try {
  65. // 转换密钥和 IV 为字节数组
  66. const keyBytes = new java.lang.String(keyStr).getBytes("UTF-8")
  67. const ivBytes = new java.lang.String(ivStr).getBytes("UTF-8")
  68. // 创建密钥规范和 IV 规范
  69. const secretKeySpec = new javax.crypto.spec.SecretKeySpec(keyBytes, "AES")
  70. const ivParameterSpec = new javax.crypto.spec.IvParameterSpec(ivBytes)
  71. // 创建 Cipher 实例并初始化为加密模式
  72. // 注意:Java 中使用 PKCS5Padding,但在 AES 中它等同于 PKCS7Padding
  73. const cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding")
  74. cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec)
  75. // 加密数据
  76. const plainBytes = new java.lang.String(plainText).getBytes("UTF-8")
  77. const encryptedBytes = cipher.doFinal(plainBytes)
  78. // 转为 Base64 字符串
  79. const base64Str = android.util.Base64.encodeToString(encryptedBytes, android.util.Base64.NO_WRAP)
  80. return base64Str.toString()
  81. } catch (e) {
  82. console.error('AES 加密失败', e)
  83. return plainText
  84. }
  85. }
  86. /**
  87. * Android 平台 AES 解密实现
  88. */
  89. function decryptAESAndroid(cipherText: string, keyStr: string, ivStr: string): string {
  90. try {
  91. // 转换密钥和 IV 为字节数组
  92. const keyBytes = new java.lang.String(keyStr).getBytes("UTF-8")
  93. const ivBytes = new java.lang.String(ivStr).getBytes("UTF-8")
  94. // 创建密钥规范和 IV 规范
  95. const secretKeySpec = new javax.crypto.spec.SecretKeySpec(keyBytes, "AES")
  96. const ivParameterSpec = new javax.crypto.spec.IvParameterSpec(ivBytes)
  97. // 创建 Cipher 实例并初始化为解密模式
  98. // 注意:Java 中使用 PKCS5Padding,但在 AES 中它等同于 PKCS7Padding
  99. const cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding")
  100. cipher.init(javax.crypto.Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec)
  101. // 解密数据
  102. const cipherBytes = android.util.Base64.decode(cipherText, android.util.Base64.NO_WRAP)
  103. const decryptedBytes = cipher.doFinal(cipherBytes)
  104. // 转为字符串
  105. const decryptedStr = new java.lang.String(decryptedBytes, "UTF-8")
  106. return decryptedStr.toString()
  107. } catch (e) {
  108. console.error('AES 解密失败', e)
  109. return cipherText
  110. }
  111. }
  112. // #endif
  113. // ============ iOS 平台实现(使用纯 TypeScript)============
  114. // #ifdef APP-IOS
  115. /**
  116. * iOS 平台 AES 加密实现(使用纯 TypeScript)
  117. */
  118. function encryptAESiOS(plainText: string, keyStr: string, ivStr: string): string {
  119. try {
  120. console.log('iOS 使用纯 TypeScript 加密')
  121. return encryptAESPure(plainText, keyStr, ivStr)
  122. } catch (e) {
  123. console.error('AES 加密失败(iOS)', e)
  124. return plainText
  125. }
  126. }
  127. /**
  128. * iOS 平台 AES 解密实现(使用纯 TypeScript)
  129. */
  130. function decryptAESiOS(cipherText: string, keyStr: string, ivStr: string): string {
  131. try {
  132. console.log('iOS 使用纯 TypeScript 解密')
  133. return decryptAESPure(cipherText, keyStr, ivStr)
  134. } catch (e) {
  135. console.error('AES 解密失败(iOS)', e)
  136. return cipherText
  137. }
  138. }
  139. // #endif
  140. // 鸿蒙平台 AES 加密实现
  141. // #ifdef APP-HARMONY
  142. /**
  143. * 鸿蒙平台 AES 加密实现
  144. * 使用纯 TypeScript 实现 AES-128-CBC
  145. */
  146. function encryptAESHarmony(plainText: string, keyStr: string, ivStr: string): string {
  147. try {
  148. return encryptAESPure(plainText, keyStr, ivStr)
  149. } catch (e) {
  150. console.error('AES 加密失败(鸿蒙)', e)
  151. return plainText
  152. }
  153. }
  154. /**
  155. * 鸿蒙平台 AES 解密实现
  156. */
  157. function decryptAESHarmony(cipherText: string, keyStr: string, ivStr: string): string {
  158. try {
  159. return decryptAESPure(cipherText, keyStr, ivStr)
  160. } catch (e) {
  161. console.error('AES 解密失败(鸿蒙)', e)
  162. return cipherText
  163. }
  164. }
  165. // #endif
  166. // Web 平台 AES 加密实现
  167. // #ifdef WEB
  168. /**
  169. * Web 平台 AES 加密实现
  170. * 使用纯 TypeScript 实现 AES-128-CBC
  171. */
  172. function encryptAESWeb(plainText: string, keyStr: string, ivStr: string): string {
  173. try {
  174. return encryptAESPure(plainText, keyStr, ivStr)
  175. } catch (e) {
  176. console.error('加密失败(Web)', e)
  177. return plainText
  178. }
  179. }
  180. /**
  181. * Web 平台 AES 解密实现
  182. */
  183. function decryptAESWeb(cipherText: string, keyStr: string, ivStr: string): string {
  184. try {
  185. return decryptAESPure(cipherText, keyStr, ivStr)
  186. } catch (e) {
  187. console.error('解密失败(Web)', e)
  188. return cipherText
  189. }
  190. }
  191. // #endif
  192. // 纯 TypeScript 实现的 AES 加密(用于 Web 和鸿蒙平台)
  193. // #ifndef APP-ANDROID
  194. /**
  195. * 纯 TypeScript AES 加密实现(简化版 CryptoJS 逻辑)
  196. */
  197. function encryptAESPure(plainText: string, keyStr: string, ivStr: string): string {
  198. // 将字符串转为字节数组
  199. const plainBytes = stringToBytes(plainText)
  200. const keyBytes = stringToBytes(keyStr)
  201. const ivBytes = stringToBytes(ivStr)
  202. // PKCS7 填充
  203. const paddedBytes = pkcs7Pad(plainBytes)
  204. // AES-CBC 加密
  205. const encryptedBytes = aesCBCEncrypt(paddedBytes, keyBytes, ivBytes)
  206. // 转为 Base64
  207. return bytesToBase64(encryptedBytes)
  208. }
  209. /**
  210. * 纯 TypeScript AES 解密实现
  211. */
  212. function decryptAESPure(cipherText: string, keyStr: string, ivStr: string): string {
  213. // Base64 转字节数组
  214. const cipherBytes = base64ToBytes(cipherText)
  215. const keyBytes = stringToBytes(keyStr)
  216. const ivBytes = stringToBytes(ivStr)
  217. // AES-CBC 解密
  218. const decryptedBytes = aesCBCDecrypt(cipherBytes, keyBytes, ivBytes)
  219. // 去除 PKCS7 填充
  220. const unpaddedBytes = pkcs7Unpad(decryptedBytes)
  221. // 转为字符串
  222. return bytesToString(unpaddedBytes)
  223. }
  224. // ============ 辅助函数 ============
  225. /**
  226. * 字符串转字节数组(UTF-8)
  227. */
  228. function stringToBytes(str: string): number[] {
  229. const bytes: number[] = []
  230. for (let i = 0; i < str.length; i++) {
  231. const charCode = str.charCodeAt(i)
  232. if (charCode < 0x80) {
  233. bytes.push(charCode)
  234. } else if (charCode < 0x800) {
  235. bytes.push(0xc0 | (charCode >> 6))
  236. bytes.push(0x80 | (charCode & 0x3f))
  237. } else if (charCode < 0xd800 || charCode >= 0xe000) {
  238. bytes.push(0xe0 | (charCode >> 12))
  239. bytes.push(0x80 | ((charCode >> 6) & 0x3f))
  240. bytes.push(0x80 | (charCode & 0x3f))
  241. } else {
  242. // UTF-16 代理对
  243. i++
  244. const surrogate = 0x10000 + (((charCode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff))
  245. bytes.push(0xf0 | (surrogate >> 18))
  246. bytes.push(0x80 | ((surrogate >> 12) & 0x3f))
  247. bytes.push(0x80 | ((surrogate >> 6) & 0x3f))
  248. bytes.push(0x80 | (surrogate & 0x3f))
  249. }
  250. }
  251. return bytes
  252. }
  253. /**
  254. * 字节数组转字符串(UTF-8)
  255. */
  256. function bytesToString(bytes: number[]): string {
  257. let str = ''
  258. let i = 0
  259. while (i < bytes.length) {
  260. const byte = bytes[i]
  261. if (byte < 0x80) {
  262. str += String.fromCharCode(byte)
  263. i++
  264. } else if (byte < 0xe0) {
  265. str += String.fromCharCode(((byte & 0x1f) << 6) | (bytes[i + 1] & 0x3f))
  266. i += 2
  267. } else if (byte < 0xf0) {
  268. str += String.fromCharCode(((byte & 0x0f) << 12) | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f))
  269. i += 3
  270. } else {
  271. const codePoint = ((byte & 0x07) << 18) | ((bytes[i + 1] & 0x3f) << 12) | ((bytes[i + 2] & 0x3f) << 6) | (bytes[i + 3] & 0x3f)
  272. const surrogate = codePoint - 0x10000
  273. str += String.fromCharCode(0xd800 + (surrogate >> 10), 0xdc00 + (surrogate & 0x3ff))
  274. i += 4
  275. }
  276. }
  277. return str
  278. }
  279. /**
  280. * 字节数组转 Base64
  281. */
  282. function bytesToBase64(bytes: number[]): string {
  283. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  284. let base64 = ''
  285. for (let i = 0; i < bytes.length; i += 3) {
  286. const byte1 = bytes[i]
  287. const byte2 = i + 1 < bytes.length ? bytes[i + 1] : 0
  288. const byte3 = i + 2 < bytes.length ? bytes[i + 2] : 0
  289. const enc1 = byte1 >> 2
  290. const enc2 = ((byte1 & 3) << 4) | (byte2 >> 4)
  291. const enc3 = ((byte2 & 15) << 2) | (byte3 >> 6)
  292. const enc4 = byte3 & 63
  293. base64 += chars.charAt(enc1) + chars.charAt(enc2)
  294. base64 += i + 1 < bytes.length ? chars.charAt(enc3) : '='
  295. base64 += i + 2 < bytes.length ? chars.charAt(enc4) : '='
  296. }
  297. return base64
  298. }
  299. /**
  300. * Base64 转字节数组
  301. */
  302. function base64ToBytes(base64: string): number[] {
  303. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  304. const bytes: number[] = []
  305. for (let i = 0; i < base64.length; i += 4) {
  306. const enc1 = chars.indexOf(base64.charAt(i))
  307. const enc2 = chars.indexOf(base64.charAt(i + 1))
  308. const enc3 = chars.indexOf(base64.charAt(i + 2))
  309. const enc4 = chars.indexOf(base64.charAt(i + 3))
  310. const byte1 = (enc1 << 2) | (enc2 >> 4)
  311. const byte2 = ((enc2 & 15) << 4) | (enc3 >> 2)
  312. const byte3 = ((enc3 & 3) << 6) | enc4
  313. bytes.push(byte1)
  314. if (enc3 !== -1) bytes.push(byte2)
  315. if (enc4 !== -1) bytes.push(byte3)
  316. }
  317. return bytes
  318. }
  319. /**
  320. * PKCS7 填充
  321. */
  322. function pkcs7Pad(bytes: number[]): number[] {
  323. const blockSize = 16
  324. const padding = blockSize - (bytes.length % blockSize)
  325. const paddedBytes = bytes.slice()
  326. for (let i = 0; i < padding; i++) {
  327. paddedBytes.push(padding)
  328. }
  329. return paddedBytes
  330. }
  331. /**
  332. * PKCS7 去填充
  333. */
  334. function pkcs7Unpad(bytes: number[]): number[] {
  335. const padding = bytes[bytes.length - 1]
  336. return bytes.slice(0, bytes.length - padding)
  337. }
  338. /**
  339. * AES-CBC 加密
  340. */
  341. function aesCBCEncrypt(plainBytes: number[], keyBytes: number[], ivBytes: number[]): number[] {
  342. const blocks = []
  343. const blockSize = 16
  344. let previousBlock = ivBytes.slice()
  345. // 扩展密钥
  346. const expandedKey = expandKey(keyBytes)
  347. // 分块加密
  348. for (let i = 0; i < plainBytes.length; i += blockSize) {
  349. const block = plainBytes.slice(i, i + blockSize)
  350. // CBC 模式:先与上一个密文块异或
  351. const xorBlock = xorBytes(block, previousBlock)
  352. // AES 加密
  353. const encryptedBlock = aesEncryptBlock(xorBlock, expandedKey)
  354. blocks.push(...encryptedBlock)
  355. previousBlock = encryptedBlock
  356. }
  357. return blocks
  358. }
  359. /**
  360. * AES-CBC 解密
  361. */
  362. function aesCBCDecrypt(cipherBytes: number[], keyBytes: number[], ivBytes: number[]): number[] {
  363. const blocks = []
  364. const blockSize = 16
  365. let previousBlock = ivBytes.slice()
  366. // 扩展密钥
  367. const expandedKey = expandKey(keyBytes)
  368. // 分块解密
  369. for (let i = 0; i < cipherBytes.length; i += blockSize) {
  370. const block = cipherBytes.slice(i, i + blockSize)
  371. // AES 解密
  372. const decryptedBlock = aesDecryptBlock(block, expandedKey)
  373. // CBC 模式:再与上一个密文块异或
  374. const xorBlock = xorBytes(decryptedBlock, previousBlock)
  375. blocks.push(...xorBlock)
  376. previousBlock = block
  377. }
  378. return blocks
  379. }
  380. /**
  381. * 字节数组异或
  382. */
  383. function xorBytes(a: number[], b: number[]): number[] {
  384. const result: number[] = []
  385. for (let i = 0; i < a.length; i++) {
  386. result.push(a[i] ^ b[i])
  387. }
  388. return result
  389. }
  390. /**
  391. * AES 密钥扩展
  392. */
  393. function expandKey(key: number[]): number[][] {
  394. const Nk = 4 // 128-bit key
  395. const Nr = 10 // 10 rounds
  396. const w: number[][] = []
  397. // 初始化前 Nk 个字
  398. for (let i = 0; i < Nk; i++) {
  399. w[i] = [key[4 * i], key[4 * i + 1], key[4 * i + 2], key[4 * i + 3]]
  400. }
  401. // 扩展密钥
  402. for (let i = Nk; i < 4 * (Nr + 1); i++) {
  403. let temp = w[i - 1].slice()
  404. if (i % Nk === 0) {
  405. temp = xorWord(subWord(rotWord(temp)), rcon(i / Nk))
  406. }
  407. w[i] = xorWord(w[i - Nk], temp)
  408. }
  409. return w
  410. }
  411. /**
  412. * AES 加密单个块
  413. */
  414. function aesEncryptBlock(block: number[], expandedKey: number[][]): number[] {
  415. const state = blockToState(block)
  416. const Nr = 10
  417. addRoundKey(state, expandedKey, 0)
  418. for (let round = 1; round < Nr; round++) {
  419. subBytes(state)
  420. shiftRows(state)
  421. mixColumns(state)
  422. addRoundKey(state, expandedKey, round)
  423. }
  424. subBytes(state)
  425. shiftRows(state)
  426. addRoundKey(state, expandedKey, Nr)
  427. return stateToBlock(state)
  428. }
  429. /**
  430. * AES 解密单个块
  431. */
  432. function aesDecryptBlock(block: number[], expandedKey: number[][]): number[] {
  433. const state = blockToState(block)
  434. const Nr = 10
  435. addRoundKey(state, expandedKey, Nr)
  436. for (let round = Nr - 1; round > 0; round--) {
  437. invShiftRows(state)
  438. invSubBytes(state)
  439. addRoundKey(state, expandedKey, round)
  440. invMixColumns(state)
  441. }
  442. invShiftRows(state)
  443. invSubBytes(state)
  444. addRoundKey(state, expandedKey, 0)
  445. return stateToBlock(state)
  446. }
  447. // ============ AES 核心函数 ============
  448. // S-box
  449. const sbox = [
  450. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  451. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  452. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  453. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  454. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  455. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  456. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  457. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  458. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  459. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  460. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  461. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  462. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  463. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  464. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  465. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  466. ]
  467. // 逆 S-box
  468. const invSbox = [
  469. 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  470. 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  471. 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  472. 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  473. 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  474. 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  475. 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  476. 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  477. 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  478. 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  479. 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  480. 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  481. 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  482. 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  483. 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  484. 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  485. ]
  486. // Rcon
  487. const rcon_array = [0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36]
  488. function rcon(i: number): number[] {
  489. return [rcon_array[i], 0, 0, 0]
  490. }
  491. function rotWord(word: number[]): number[] {
  492. return [word[1], word[2], word[3], word[0]]
  493. }
  494. function subWord(word: number[]): number[] {
  495. return word.map(b => sbox[b])
  496. }
  497. function xorWord(a: number[], b: number[]): number[] {
  498. return [a[0] ^ b[0], a[1] ^ b[1], a[2] ^ b[2], a[3] ^ b[3]]
  499. }
  500. function blockToState(block: number[]): number[][] {
  501. const state: number[][] = []
  502. for (let i = 0; i < 4; i++) {
  503. state[i] = []
  504. for (let j = 0; j < 4; j++) {
  505. state[i][j] = block[i + 4 * j]
  506. }
  507. }
  508. return state
  509. }
  510. function stateToBlock(state: number[][]): number[] {
  511. const block: number[] = []
  512. for (let j = 0; j < 4; j++) {
  513. for (let i = 0; i < 4; i++) {
  514. block.push(state[i][j])
  515. }
  516. }
  517. return block
  518. }
  519. function addRoundKey(state: number[][], expandedKey: number[][], round: number): void {
  520. for (let i = 0; i < 4; i++) {
  521. for (let j = 0; j < 4; j++) {
  522. state[i][j] ^= expandedKey[round * 4 + j][i]
  523. }
  524. }
  525. }
  526. function subBytes(state: number[][]): void {
  527. for (let i = 0; i < 4; i++) {
  528. for (let j = 0; j < 4; j++) {
  529. state[i][j] = sbox[state[i][j]]
  530. }
  531. }
  532. }
  533. function invSubBytes(state: number[][]): void {
  534. for (let i = 0; i < 4; i++) {
  535. for (let j = 0; j < 4; j++) {
  536. state[i][j] = invSbox[state[i][j]]
  537. }
  538. }
  539. }
  540. function shiftRows(state: number[][]): void {
  541. let temp: number
  542. // Row 1: shift left by 1
  543. temp = state[1][0]
  544. state[1][0] = state[1][1]
  545. state[1][1] = state[1][2]
  546. state[1][2] = state[1][3]
  547. state[1][3] = temp
  548. // Row 2: shift left by 2
  549. temp = state[2][0]
  550. state[2][0] = state[2][2]
  551. state[2][2] = temp
  552. temp = state[2][1]
  553. state[2][1] = state[2][3]
  554. state[2][3] = temp
  555. // Row 3: shift left by 3 (or right by 1)
  556. temp = state[3][3]
  557. state[3][3] = state[3][2]
  558. state[3][2] = state[3][1]
  559. state[3][1] = state[3][0]
  560. state[3][0] = temp
  561. }
  562. function invShiftRows(state: number[][]): void {
  563. let temp: number
  564. // Row 1: shift right by 1
  565. temp = state[1][3]
  566. state[1][3] = state[1][2]
  567. state[1][2] = state[1][1]
  568. state[1][1] = state[1][0]
  569. state[1][0] = temp
  570. // Row 2: shift right by 2
  571. temp = state[2][0]
  572. state[2][0] = state[2][2]
  573. state[2][2] = temp
  574. temp = state[2][1]
  575. state[2][1] = state[2][3]
  576. state[2][3] = temp
  577. // Row 3: shift right by 3 (or left by 1)
  578. temp = state[3][0]
  579. state[3][0] = state[3][1]
  580. state[3][1] = state[3][2]
  581. state[3][2] = state[3][3]
  582. state[3][3] = temp
  583. }
  584. function mixColumns(state: number[][]): void {
  585. for (let j = 0; j < 4; j++) {
  586. const s0 = state[0][j]
  587. const s1 = state[1][j]
  588. const s2 = state[2][j]
  589. const s3 = state[3][j]
  590. state[0][j] = gfMul(0x02, s0) ^ gfMul(0x03, s1) ^ s2 ^ s3
  591. state[1][j] = s0 ^ gfMul(0x02, s1) ^ gfMul(0x03, s2) ^ s3
  592. state[2][j] = s0 ^ s1 ^ gfMul(0x02, s2) ^ gfMul(0x03, s3)
  593. state[3][j] = gfMul(0x03, s0) ^ s1 ^ s2 ^ gfMul(0x02, s3)
  594. }
  595. }
  596. function invMixColumns(state: number[][]): void {
  597. for (let j = 0; j < 4; j++) {
  598. const s0 = state[0][j]
  599. const s1 = state[1][j]
  600. const s2 = state[2][j]
  601. const s3 = state[3][j]
  602. state[0][j] = gfMul(0x0e, s0) ^ gfMul(0x0b, s1) ^ gfMul(0x0d, s2) ^ gfMul(0x09, s3)
  603. state[1][j] = gfMul(0x09, s0) ^ gfMul(0x0e, s1) ^ gfMul(0x0b, s2) ^ gfMul(0x0d, s3)
  604. state[2][j] = gfMul(0x0d, s0) ^ gfMul(0x09, s1) ^ gfMul(0x0e, s2) ^ gfMul(0x0b, s3)
  605. state[3][j] = gfMul(0x0b, s0) ^ gfMul(0x0d, s1) ^ gfMul(0x09, s2) ^ gfMul(0x0e, s3)
  606. }
  607. }
  608. /**
  609. * 伽罗瓦域乘法
  610. */
  611. function gfMul(a: number, b: number): number {
  612. let p = 0
  613. for (let i = 0; i < 8; i++) {
  614. if ((b & 1) !== 0) {
  615. p ^= a
  616. }
  617. const hiBitSet = (a & 0x80) !== 0
  618. a = (a << 1) & 0xff
  619. if (hiBitSet) {
  620. a ^= 0x1b // AES 的不可约多项式
  621. }
  622. b >>= 1
  623. }
  624. return p
  625. }
  626. // #endif