|
@@ -0,0 +1,95 @@
|
|
|
+import './decrypt-pre1.js'
|
|
|
+import './decrypt-pre2.js'
|
|
|
+
|
|
|
+const config = {
|
|
|
+ plainKey: '', // rsa解密后的key
|
|
|
+ privateKey: `-----BEGIN PRIVATE KEY-----
|
|
|
+ ${window.__pkContent || ''}
|
|
|
+ -----END PRIVATE KEY-----`,
|
|
|
+ // privateKey: `-----BEGIN PRIVATE KEY-----
|
|
|
+ // MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAOhM0pNOfGeiBr+t
|
|
|
+ // nunphCHReY3RiS4Fuc2nD3cbjKNdLezeViGmsZwHsb2SVUb6rpPHyX0+3xjXYn//
|
|
|
+ // n39/Q8uPjWRA332TtN8MDEkSR2HMbn8ufRRt2TnlfsFDFTgBywSP7cwd0CiEdvBX
|
|
|
+ // 5w8Jifc9VbedwbeplBWyDeLLqjRjAgMBAAECgYB4es+EAuLWxNwHMb8Hxkr3VzNZ
|
|
|
+ // 8GDbc7DIDmsg9TLdz4fwH+hAD7pyGDOBBJIh/AXrM2U3BhKjSaIWjLdmYtT/kzg8
|
|
|
+ // BxQDr9YoO7u2jvTcEE+/6p2YugYX/ngpinawFJqyM+N7Or8yRABaw6Aq8VuKtv6p
|
|
|
+ // 980Y2BBVVYn+/KorYQJBAP+9lu8iolzKRzJrFt/rosdWkOpNg5ujcSCwbxhYnYC0
|
|
|
+ // UY85sPLsMvnLgegkpO8jocSAt586BmcsA+Q9o97qVCkCQQDoiSVegtOvG3U0mNlN
|
|
|
+ // rCVpPEL22s9Kkwps3ZCdTl3VtUtNiyfhE8rbw/qOGti3VxMCRhpKi9hTIgeq13UG
|
|
|
+ // 67WrAkEA/WQ1c5XGd9f4eU1AKffInmf4SB8rgn+L7I7EVMQgstB3a0kHOXqs+3IX
|
|
|
+ // shL01PliJFhBF+QfSgSDipdEke9uGQJBAOcw46xxmhDw1bizdulYi+Fy/oj7xzi3
|
|
|
+ // tJfEObGMZpLBKtsvzThkOz4APS3n1yuBMO8Dz8PqAeu1W7YpfLqiwv0CQF68N244
|
|
|
+ // dFebDSoZLl1hbCExpbtC7SDBpYxlIVNVqwN7ymr+Z0rIcAMVv5Ldp/bJEWaXJs9C
|
|
|
+ // 0sPCBpjDnyK9Z04=
|
|
|
+ // -----END PRIVATE KEY-----`
|
|
|
+}
|
|
|
+
|
|
|
+const decryptTools = {
|
|
|
+ // rsa解密
|
|
|
+ rsaDecrypt: function (cipherText, privateKey) {
|
|
|
+ // 解密
|
|
|
+ var decrypt = new JSEncrypt()//创建解密对象实例
|
|
|
+ decrypt.setPrivateKey(privateKey)//设置秘钥
|
|
|
+ var uncrypted = decrypt.decrypt(cipherText)//解密之前拿公钥加密的内容
|
|
|
+ return uncrypted
|
|
|
+ },
|
|
|
+ // AES解密
|
|
|
+ async AESDecrypt(content, base64Key) {
|
|
|
+ const key = new TextEncoder().encode(base64Key)
|
|
|
+ const encryptedBase64 = content;
|
|
|
+ const encryptedData = Uint8Array.from(window.atob(encryptedBase64), c => c.charCodeAt(0));
|
|
|
+
|
|
|
+ const decryptData = async () => {
|
|
|
+ const iv = encryptedData.slice(0, 16);
|
|
|
+ const ciphertext = encryptedData.slice(16);
|
|
|
+
|
|
|
+ const aesKey = await crypto.subtle.importKey("raw", key, {name: "AES-CTR"}, false, ["encrypt", "decrypt"]);
|
|
|
+
|
|
|
+ const decryptedData = await crypto.subtle.decrypt({name: "AES-CTR", counter: iv, length: 128}, aesKey, ciphertext);
|
|
|
+ const decryptedText = new TextDecoder().decode(decryptedData);
|
|
|
+
|
|
|
+ return {
|
|
|
+ value: decryptedText
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ const result = await decryptData()
|
|
|
+ return result
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const message = {
|
|
|
+ sendPostMessage: function(e, result) {
|
|
|
+ const win = e.source
|
|
|
+ var payload = {
|
|
|
+ ...result,
|
|
|
+ type: 'after-decrypt'
|
|
|
+ }
|
|
|
+ window.parent.postMessage(payload, payload.fromOrigin)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+window.addEventListener('message', async (e) => {
|
|
|
+ if (e.data.type !== 'decrypt') {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (window === e.source) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var base64Key = e.data.base64Key
|
|
|
+ var cipherText = e.data.cipherText
|
|
|
+ // 1. 先解密base64Key
|
|
|
+ var plainKey = decryptTools.rsaDecrypt(base64Key, config.privateKey)
|
|
|
+ config.plainKey = plainKey
|
|
|
+ // 2. 再用key解密cipherText
|
|
|
+ var plainText = await decryptTools.AESDecrypt(cipherText, plainKey)
|
|
|
+
|
|
|
+ const result = {
|
|
|
+ ...e.data,
|
|
|
+ plainKey: plainKey,
|
|
|
+ plainText: plainText.value,
|
|
|
+ }
|
|
|
+ message.sendPostMessage(e, result)
|
|
|
+})
|
|
|
+
|