crypto.uts 21 KB

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