index.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. function wordArrayToUint8Array(wordArray: CryptoJS.lib.WordArray): Uint8Array {
  37. const words = wordArray.words;
  38. const byteArray = [];
  39. for (let i = 0; i < words.length; ++i) {
  40. const word = words[i];
  41. for (let j = 3; j >= 0; --j) {
  42. byteArray.push((word >> (8 * j)) & 0xff);
  43. }
  44. }
  45. return new Uint8Array(byteArray);
  46. }
  47. /**
  48. * MAC校检码生成子方法
  49. * @param plainText 目标数据
  50. * @param key DES密钥
  51. */
  52. function dataMacAnsiX99(plainText: Uint8Array, key: CryptoJS.lib.WordArray): Uint8Array | null {
  53. if (plainText.length % 8 !== 0) {
  54. return null;
  55. }
  56. const macData = new Uint8Array(8);
  57. macData.set(iv, 0);
  58. const xorData = new Uint8Array(8);
  59. for (let i = 0; i < plainText.length; i += 8) {
  60. for (let j = 0; j < 8; j++) {
  61. xorData[j] = macData[j] ^ plainText[i + j];
  62. }
  63. const desData = CryptoJS.DES.encrypt(uint8ArrayToWordArray(xorData), key, desOption);
  64. const desBase64 = CryptoJS.enc.Base64.parse(desData.toString());
  65. const desArray = wordArrayToUint8Array(desBase64);
  66. for (let k = 0; k < desArray.length; k++) {
  67. macData[k] = desArray[k];
  68. }
  69. }
  70. return macData;
  71. }
  72. /**
  73. * MAC校检码生成方法
  74. * @param plainText 目标数据
  75. */
  76. function dataMacAnsiX919(plainText: Uint8Array): Uint8Array | null {
  77. const data1 = dataMacAnsiX99(plainText, macKeyLeft);
  78. if (data1 === null) {
  79. return null;
  80. }
  81. const cipherParams = CryptoJS.lib.CipherParams.create({
  82. ciphertext: uint8ArrayToWordArray(data1),
  83. });
  84. const data2 = CryptoJS.DES.decrypt(cipherParams, macKeyRight, desOption);
  85. const retData = CryptoJS.DES.encrypt(data2, macKeyLeft, desOption);
  86. const retBase64 = CryptoJS.enc.Base64.parse(retData.toString());
  87. return wordArrayToUint8Array(retBase64);
  88. }
  89. /**
  90. * 5.0报文数据加密方法
  91. * @param plainText 明文
  92. */
  93. export const encrypt50 = (plainText: Uint8Array): Uint8Array | null => {
  94. if (plainText === null) {
  95. return null;
  96. }
  97. const a = CryptoJS.AES.encrypt(uint8ArrayToWordArray(plainText), aeskey, aesOption);
  98. const a1 = CryptoJS.enc.Base64.parse(a.toString());
  99. const a2 = wordArrayToUint8Array(a1);
  100. const data1 = new Uint8Array(4 + a2.length + 8);
  101. const dataView = new DataView(new ArrayBuffer(4));
  102. dataView.setUint32(0, plainText.length, true);
  103. const lengthArray = new Uint8Array(dataView.buffer);
  104. data1.set(lengthArray);
  105. data1.set(a2, 4);
  106. const mDataTemp = new Uint8Array(8);
  107. mDataTemp.set(lengthArray);
  108. mDataTemp.set(new Uint8Array(4), 4);
  109. const macData = dataMacAnsiX919(mDataTemp);
  110. if (macData === null) {
  111. return null;
  112. }
  113. data1.set(macData, 4 + a2.length);
  114. return data1;
  115. };
  116. /**
  117. * 5.0报文数据解密方法
  118. * @param encryptData 密文
  119. * @param size 明文长度
  120. */
  121. export const decrypt50 = (encryptData: Uint8Array, size: number): Uint8Array | null => {
  122. const cipherParams = CryptoJS.lib.CipherParams.create({
  123. ciphertext: uint8ArrayToWordArray(encryptData),
  124. });
  125. const decrytped = CryptoJS.AES.decrypt(cipherParams, aeskey, aesOption);
  126. const a = wordArrayToUint8Array(decrytped);
  127. if (a.length < size) {
  128. return null;
  129. }
  130. return a.subarray(0, size);
  131. };
  132. /**
  133. * AES数据解密方法
  134. * @param encryptData 密文
  135. * @param size 明文长度
  136. */
  137. export const decryptAES = (value: string): string | undefined => {
  138. const ciphertext = Uint8Array.from(Buffer.from(value, 'hex'));
  139. const key = Uint8Array.from(Buffer.from(phoneaeskey, 'hex'));
  140. const cipherParams = CryptoJS.lib.CipherParams.create({
  141. ciphertext: uint8ArrayToWordArray(ciphertext),
  142. });
  143. const decrytped = CryptoJS.AES.decrypt(cipherParams, uint8ArrayToWordArray(key), aesOption);
  144. const h = wordArrayToUint8Array(decrytped).subarray(0, decrytped.words.length * 4 - 1);
  145. const result = new TextDecoder().decode(h);
  146. return result;
  147. };