crypto.ts 6.7 KB

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