crypto-util.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import './crypto-js.js';
  2. function window_b64() {
  3. let b64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  4. let base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
  5. function btoa(str) {
  6. var out, i, len;
  7. var c1, c2, c3;
  8. len = str.length;
  9. i = 0;
  10. out = "";
  11. while (i < len) {
  12. c1 = str.charCodeAt(i++) & 0xff;
  13. if (i == len) {
  14. out += b64map.charAt(c1 >> 2);
  15. out += b64map.charAt((c1 & 0x3) << 4);
  16. out += "==";
  17. break;
  18. }
  19. c2 = str.charCodeAt(i++);
  20. if (i == len) {
  21. out += b64map.charAt(c1 >> 2);
  22. out += b64map.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  23. out += b64map.charAt((c2 & 0xF) << 2);
  24. out += "=";
  25. break;
  26. }
  27. c3 = str.charCodeAt(i++);
  28. out += b64map.charAt(c1 >> 2);
  29. out += b64map.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  30. out += b64map.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  31. out += b64map.charAt(c3 & 0x3F);
  32. }
  33. return out;
  34. }
  35. function atob(str) {
  36. var c1, c2, c3, c4;
  37. var i, len, out;
  38. len = str.length;
  39. i = 0;
  40. out = "";
  41. while (i < len) {
  42. do {
  43. c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
  44. } while (i < len && c1 == -1);
  45. if (c1 == -1) break;
  46. do {
  47. c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
  48. } while (i < len && c2 == -1);
  49. if (c2 == -1) break;
  50. out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
  51. do {
  52. c3 = str.charCodeAt(i++) & 0xff;
  53. if (c3 == 61) return out;
  54. c3 = base64DecodeChars[c3];
  55. } while (i < len && c3 == -1);
  56. if (c3 == -1) break;
  57. out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
  58. do {
  59. c4 = str.charCodeAt(i++) & 0xff;
  60. if (c4 == 61) return out;
  61. c4 = base64DecodeChars[c4];
  62. } while (i < len && c4 == -1);
  63. if (c4 == -1) break;
  64. out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  65. }
  66. return out;
  67. }
  68. return {
  69. atob,
  70. btoa
  71. }
  72. }
  73. export const {atob, btoa} = window_b64();
  74. export function base64Encode(text) {
  75. return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text));
  76. // return text
  77. }
  78. export function base64Decode(text) {
  79. return CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(text));
  80. // return text
  81. }
  82. export function md5(text) {
  83. return CryptoJS.MD5(text).toString();
  84. }
  85. export function rc4Encrypt(word, key) {
  86. return CryptoJS.RC4.encrypt(word, CryptoJS.enc.Utf8.parse(key)).toString()
  87. }
  88. export function rc4Decrypt(word, key) {
  89. const ciphertext = CryptoJS.enc.Hex.parse(word);
  90. const key_data = CryptoJS.enc.Utf8.parse(key)
  91. const decrypted = CryptoJS.RC4.decrypt({ciphertext: ciphertext}, key_data, {
  92. mode: CryptoJS.mode.ECB,
  93. padding: CryptoJS.pad.Pkcs7
  94. });
  95. return decrypted.toString(CryptoJS.enc.Utf8);
  96. }
  97. export function rc4_decode(data, key, t) {
  98. let pwd = key || 'ffsirllq';
  99. let cipher = '';
  100. key = [];
  101. let box = [];
  102. let pwd_length = pwd.length;
  103. if (t === 1) {
  104. data = atob(data);
  105. } else {
  106. data = encodeURIComponent(data);
  107. }
  108. let data_length = data.length;
  109. for (let i = 0; i < 256; i++) {
  110. key[i] = pwd[i % pwd_length].charCodeAt();
  111. box[i] = i;
  112. }
  113. for (let j = 0, i = 0; i < 256; i++) {
  114. j = (j + box[i] + key[i]) % 256;
  115. let tmp = box[i];
  116. box[i] = box[j];
  117. box[j] = tmp;
  118. }
  119. for (let a = 0, j = 0, i = 0; i < data_length; i++) {
  120. a = (a + 1) % 256;
  121. j = (j + box[a]) % 256;
  122. let tmp = box[a];
  123. box[a] = box[j];
  124. box[j] = tmp;
  125. let k = box[((box[a] + box[j]) % 256)];
  126. cipher += String.fromCharCode(data[i].charCodeAt() ^ k);
  127. }
  128. if (t === 1) {
  129. return decodeURIComponent(cipher);
  130. } else {
  131. return btoa(cipher);
  132. }
  133. }
  134. // https://github.com/Hiram-Wong/ZyPlayer/blob/main/src/renderer/src/utils/crypto.ts
  135. const hex = {
  136. decode: function (val) {
  137. return Buffer.from(val, 'hex').toString('utf-8');
  138. },
  139. encode: function (val) {
  140. return Buffer.from(val, 'utf-8').toString('hex');
  141. },
  142. };
  143. const parseEncode = function (value, encoding) {
  144. switch (encoding) {
  145. case 'base64':
  146. return CryptoJS.enc.Base64.parse(value);
  147. case 'hex':
  148. return CryptoJS.enc.Hex.parse(value);
  149. case 'latin1':
  150. return CryptoJS.enc.Latin1.parse(value);
  151. case 'utf8':
  152. return CryptoJS.enc.Utf8.parse(value);
  153. default:
  154. return CryptoJS.enc.Utf8.parse(value);
  155. }
  156. };
  157. const formatEncode = function (value, encoding) {
  158. switch (encoding.toLowerCase()) {
  159. case 'base64':
  160. return value.toString(); // 整个CipherParams对象(含原数据), 默认输出 Base64
  161. case 'hex':
  162. return value.ciphertext.toString(); // ciphertext属性(仅密文), 默认输出 Hex
  163. }
  164. };
  165. const formatDecode = function (value, encoding) {
  166. switch (encoding.toLowerCase()) {
  167. case 'utf8':
  168. return value.toString(CryptoJS.enc.Utf8);
  169. case 'base64':
  170. return value.toString(CryptoJS.enc.Base64);
  171. case 'hex':
  172. return value.toString(CryptoJS.enc.Hex);
  173. default:
  174. return value.toString(CryptoJS.enc.Utf8);
  175. }
  176. };
  177. const getMode = function (mode) {
  178. switch (mode.toLowerCase()) {
  179. case 'cbc':
  180. return CryptoJS.mode.CBC;
  181. case 'cfb':
  182. return CryptoJS.mode.CFB;
  183. case 'ofb':
  184. return CryptoJS.mode.OFB;
  185. case 'ctr':
  186. return CryptoJS.mode.CTR;
  187. case 'ecb':
  188. return CryptoJS.mode.ECB;
  189. default:
  190. return CryptoJS.mode.CBC;
  191. }
  192. };
  193. const getPad = function (padding) {
  194. switch (padding.toLowerCase()) {
  195. case 'zeropadding':
  196. return CryptoJS.pad.ZeroPadding;
  197. case 'pkcs5padding':
  198. case 'pkcs7padding':
  199. return CryptoJS.pad.Pkcs7;
  200. case 'ansix923':
  201. return CryptoJS.pad.AnsiX923;
  202. case 'iso10126':
  203. return CryptoJS.pad.Iso10126;
  204. case 'iso97971':
  205. return CryptoJS.pad.Iso97971;
  206. case 'nopadding':
  207. return CryptoJS.pad.NoPadding;
  208. default:
  209. return CryptoJS.pad.ZeroPadding;
  210. }
  211. };
  212. export const rc4 = {
  213. encode: function (val, key, encoding = 'utf8', keyEncoding = 'utf8', outputEncode = 'base64') {
  214. if (!['base64', 'hex'].includes(outputEncode.toLowerCase())) return '';
  215. if (!key || !val) return '';
  216. const plaintext = parseEncode(val, encoding);
  217. const v = parseEncode(key, keyEncoding);
  218. return formatEncode(CryptoJS.RC4.encrypt(plaintext, v), outputEncode);
  219. },
  220. decode: function (val, key, encoding = 'utf8', keyEncoding = 'utf8', outputEncode = 'base64') {
  221. if (!['base64', 'hex'].includes(encoding.toLowerCase())) return '';
  222. if (!key || !val) return '';
  223. const plaintext = parseEncode(val, encoding);
  224. const v = parseEncode(key, keyEncoding);
  225. return formatDecode(CryptoJS.RC4.toString(plaintext, v), outputEncode);
  226. },
  227. };