crypto.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import CryptoJS from 'crypto-js';
  2. /** AES密钥 */
  3. const aeskey: CryptoJS.lib.WordArray = CryptoJS.enc.Utf8.parse('F7A72DE7D6264530F01BA49BC73EB873');
  4. /** MAC检验码左8字节 */
  5. const macKeyLeft: CryptoJS.lib.WordArray = CryptoJS.enc.Hex.parse('B0FB83E39A5EBFAA'); // 这里要用CryptoJS.enc.Hex.parse,CryptoJS.enc.Utf8.parse返回是16位的
  6. /** MAC检验码右8字节 */
  7. const macKeyRight: CryptoJS.lib.WordArray = CryptoJS.enc.Hex.parse('BE471362A58393FF');
  8. /** Phone AES密钥 */
  9. const phoneaeskey = '0d299ce2d4105282f7471074cb0f9f9d';
  10. /** MAC检验向量 */
  11. const iv = new Uint8Array([0xd9, 0x51, 0xdb, 0xe0, 0x37, 0xc8, 0x23, 0x25]);
  12. const aesOption = {
  13. mode: CryptoJS.mode.ECB,
  14. padding: CryptoJS.pad.Pkcs7,
  15. };
  16. const desOption = {
  17. mode: CryptoJS.mode.ECB,
  18. padding: CryptoJS.pad.NoPadding,
  19. };
  20. /**
  21. * 将Uint8Array数据转化为WordArray数据的方法
  22. * @param arr Uint8Array数据
  23. */
  24. function uint8ArrayToWordArray(arr: Uint8Array): CryptoJS.lib.WordArray {
  25. const word = [];
  26. for (let i = 0; i < arr.length; i += 4) {
  27. word.push((arr[i] << 24) | (arr[i + 1] << 16) | (arr[i + 2] << 8) | (arr[i + 3] << 0));
  28. }
  29. macKeyLeft;
  30. return CryptoJS.lib.WordArray.create(word, arr.length);
  31. }
  32. /**
  33. * 将WordArray数据转化为Uint8Array数据的方法
  34. * @param wordArray WordArray数据
  35. */
  36. export function wordArrayToUint8Array(wordArray: CryptoJS.lib.WordArray): Uint8Array {
  37. const arrayOfWords = Object.prototype.hasOwnProperty.call(wordArray, 'words') ? wordArray.words : [];
  38. const length = Object.prototype.hasOwnProperty.call(wordArray, 'sigBytes') ? wordArray.sigBytes : arrayOfWords.length * 4;
  39. const uInt8Array = new Uint8Array(length);
  40. let index = 0;
  41. let word;
  42. for (let i = 0; i < length; i++) {
  43. word = arrayOfWords[i];
  44. uInt8Array[index++] = word >> 24;
  45. uInt8Array[index++] = (word >> 16) & 0xff;
  46. uInt8Array[index++] = (word >> 8) & 0xff;
  47. uInt8Array[index++] = word & 0xff;
  48. }
  49. return uInt8Array;
  50. }
  51. /**
  52. * MAC校检码生成子方法
  53. * @param plainText 目标数据
  54. * @param key DES密钥
  55. */
  56. function dataMacAnsiX99(plainText: Uint8Array, key: CryptoJS.lib.WordArray): Uint8Array | null {
  57. if (plainText.length % 8 !== 0) {
  58. return null;
  59. }
  60. const macData = new Uint8Array(8);
  61. macData.set(iv, 0);
  62. const xorData = new Uint8Array(8);
  63. for (let i = 0; i < plainText.length; i += 8) {
  64. for (let j = 0; j < 8; j++) {
  65. xorData[j] = macData[j] ^ plainText[i + j];
  66. }
  67. const desData = CryptoJS.DES.encrypt(uint8ArrayToWordArray(xorData), key, desOption);
  68. const desBase64 = CryptoJS.enc.Base64.parse(desData.toString());
  69. const desArray = wordArrayToUint8Array(desBase64);
  70. for (let k = 0; k < desArray.length; k++) {
  71. macData[k] = desArray[k];
  72. }
  73. }
  74. return macData;
  75. }
  76. /**
  77. * MAC校检码生成方法
  78. * @param plainText 目标数据
  79. */
  80. function dataMacAnsiX919(plainText: Uint8Array): Uint8Array | null {
  81. const data1 = dataMacAnsiX99(plainText, macKeyLeft);
  82. if (data1 === null) {
  83. return null;
  84. }
  85. const cipherParams = CryptoJS.lib.CipherParams.create({
  86. ciphertext: uint8ArrayToWordArray(data1),
  87. });
  88. const data2 = CryptoJS.DES.decrypt(cipherParams, macKeyRight, desOption);
  89. const retData = CryptoJS.DES.encrypt(data2, macKeyLeft, desOption);
  90. const retBase64 = CryptoJS.enc.Base64.parse(retData.toString());
  91. return wordArrayToUint8Array(retBase64);
  92. }
  93. /**
  94. * 十六进制字符串转Uint8Array
  95. * @param str
  96. * @returns
  97. */
  98. function hexStringToUint8Array(str: string) {
  99. const buffer = str.match(/.{1,2}/g)?.map((byte) => parseInt(byte, 16));
  100. return new Uint8Array(buffer ?? []);
  101. }
  102. /**
  103. * 5.0报文数据加密方法
  104. * @param plainText 明文
  105. */
  106. export const encrypt50 = (plainText: Uint8Array) => {
  107. if (plainText === null) {
  108. return null;
  109. }
  110. const a = CryptoJS.AES.encrypt(uint8ArrayToWordArray(plainText), aeskey, aesOption);
  111. const a1 = CryptoJS.enc.Base64.parse(a.toString());
  112. const a2 = wordArrayToUint8Array(a1);
  113. const data1 = new Uint8Array(4 + a2.length + 8);
  114. const dataView = new DataView(new ArrayBuffer(4));
  115. dataView.setUint32(0, plainText.length, true);
  116. const lengthArray = new Uint8Array(dataView.buffer);
  117. data1.set(lengthArray);
  118. data1.set(a2, 4);
  119. const mDataTemp = new Uint8Array(8);
  120. mDataTemp.set(lengthArray);
  121. mDataTemp.set(new Uint8Array(4), 4);
  122. const macData = dataMacAnsiX919(mDataTemp);
  123. if (macData === null) {
  124. return null;
  125. }
  126. data1.set(macData, 4 + a2.length);
  127. return data1;
  128. };
  129. /**
  130. * 5.0报文数据解密方法
  131. * @param encryptData
  132. * @returns
  133. */
  134. export const decrypt50 = (encryptData: Uint8Array) => {
  135. encryptData = encryptData.subarray(4, encryptData.length - 8);
  136. const cipherParams = CryptoJS.lib.CipherParams.create({
  137. ciphertext: uint8ArrayToWordArray(encryptData),
  138. });
  139. const decrytped = CryptoJS.AES.decrypt(cipherParams, aeskey, aesOption);
  140. return wordArrayToUint8Array(decrytped);
  141. };
  142. /**
  143. * 5.0报文数据加密方法
  144. * @param data
  145. * @returns
  146. */
  147. export const encryptBody = (data: string) => {
  148. const text = new TextEncoder()
  149. const content = text.encode(data)
  150. const encryptData = encrypt50(content)
  151. if (encryptData) {
  152. const words = uint8ArrayToWordArray(encryptData)
  153. return CryptoJS.enc.Base64.stringify(words)
  154. }
  155. return ''
  156. }
  157. /**
  158. * 5.0报文数据解密方法
  159. * @param base64
  160. * @returns
  161. */
  162. export const decryptBody = (base64: string) => {
  163. const words = CryptoJS.enc.Base64.parse(base64) // 解析base64
  164. const content = wordArrayToUint8Array(words)
  165. const decryptData = decrypt50(content)
  166. return new TextDecoder().decode(decryptData)
  167. }
  168. /**
  169. * AES数据解密方法
  170. * @param encryptData 密文
  171. * @param size 明文长度
  172. */
  173. export const decryptAES = (value: string) => {
  174. try {
  175. const ciphertext = hexStringToUint8Array(value);
  176. const key = hexStringToUint8Array(phoneaeskey);
  177. const cipherParams = CryptoJS.lib.CipherParams.create({
  178. ciphertext: uint8ArrayToWordArray(ciphertext),
  179. });
  180. const decrytped = CryptoJS.AES.decrypt(cipherParams, uint8ArrayToWordArray(key), aesOption);
  181. const h = wordArrayToUint8Array(decrytped);
  182. return new TextDecoder().decode(h);
  183. } catch {
  184. return ''
  185. }
  186. }