|
@@ -0,0 +1,110 @@
|
|
|
+import { JSEncrypt } from 'jsencrypt'
|
|
|
+import * as CryptoJS from 'crypto-js'
|
|
|
+
|
|
|
+// 我方公钥
|
|
|
+export const myGkey =
|
|
|
+ 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnIBg3mmubtZV6hjTybrC51HpGeD2SbQ++xwbs1x3Ve9q/kpXBowjn5uzwCgmbNv+SRLZFsI6llKiCo9sEeWkoH7TMKXMB4qR/1c+7OB+mZ7O6a3v7gMuVz8Qpe5xQohrEjnmAQgG9C3EYvH8CRnsn6QDiYZpK+Me1BRGKzKGzIzVpzs0Mt0C26Opbg7WdhhGso6Dkt+6Rb+P3awGirp0d24df/VFAUOtUqQ3YWtCynSWtpIJRG9D5Fu7PJEtJBJ3nupTRdUIq+WeIATcq0y8Fwcxv8Jfm4xJPndptti4fH9foC0FD5Zdcbb4rQSfxp1D89E+ca/t6P2dMxAEWn5UhQIDAQAB'
|
|
|
+
|
|
|
+// 我方私钥
|
|
|
+export const mySkey = process.env.REACT_APP_PRIVATE_KEY!.replace(/\\n/g, '\n')
|
|
|
+
|
|
|
+// 甲方公钥
|
|
|
+export const publicKey = publicKeyTemp
|
|
|
+
|
|
|
+// 使用公钥加密
|
|
|
+const encryptData = (data: string, publicKey: string): string | false => {
|
|
|
+ const encryptor = new JSEncrypt()
|
|
|
+ encryptor.setPublicKey(publicKey)
|
|
|
+ return encryptor.encrypt(data)
|
|
|
+}
|
|
|
+
|
|
|
+export const myJiaMiFu = (val: string, key: string) => {
|
|
|
+ const encryptedData = encryptData(val, key)
|
|
|
+ if (encryptedData) {
|
|
|
+ // console.log('加密后的数据:', encryptedData)
|
|
|
+ return encryptedData
|
|
|
+ } else {
|
|
|
+ return ''
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 使用私钥解密
|
|
|
+export const myJieMiFu = (encryptedData: string, privateKey: string): string | false => {
|
|
|
+ const decryptor = new JSEncrypt()
|
|
|
+ decryptor.setPrivateKey(privateKey)
|
|
|
+ return decryptor.decrypt(encryptedData)
|
|
|
+}
|
|
|
+
|
|
|
+// 自己的 私钥 签名
|
|
|
+export const signData = (data: string, privateKey: string): string | false => {
|
|
|
+ const encryptor = new JSEncrypt()
|
|
|
+ encryptor.setPrivateKey(privateKey)
|
|
|
+ try {
|
|
|
+ // 使用SHA256进行签名
|
|
|
+ const signature = encryptor.sign(data, CryptoJS.SHA256 as any, 'sha256')
|
|
|
+ return signature
|
|
|
+ } catch (error) {
|
|
|
+ console.error('签名失败:', error)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// --------------我方生成公钥和私钥
|
|
|
+
|
|
|
+// const generateKeyPairWebCrypto = async (): Promise<{
|
|
|
+// publicKey: string
|
|
|
+// privateKey: string
|
|
|
+// }> => {
|
|
|
+// try {
|
|
|
+// // 生成密钥对
|
|
|
+// const keyPair = await window.crypto.subtle.generateKey(
|
|
|
+// {
|
|
|
+// name: 'RSA-OAEP',
|
|
|
+// modulusLength: 2048, // 密钥长度
|
|
|
+// publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // 65537
|
|
|
+// hash: { name: 'SHA-256' } // 哈希算法
|
|
|
+// },
|
|
|
+// true, // 是否可导出
|
|
|
+// ['encrypt', 'decrypt'] // 密钥用途
|
|
|
+// )
|
|
|
+
|
|
|
+// // 导出公钥 (SPKI 格式)
|
|
|
+// const exportedPublicKey = await window.crypto.subtle.exportKey(
|
|
|
+// 'spki',
|
|
|
+// keyPair.publicKey
|
|
|
+// )
|
|
|
+// // 导出私钥 (PKCS#8 格式)
|
|
|
+// const exportedPrivateKey = await window.crypto.subtle.exportKey(
|
|
|
+// 'pkcs8',
|
|
|
+// keyPair.privateKey
|
|
|
+// )
|
|
|
+
|
|
|
+// // 将 ArrayBuffer 转换为 Base64 字符串
|
|
|
+// const publicKeyBase64 = btoa(
|
|
|
+// String.fromCharCode(...new Uint8Array(exportedPublicKey))
|
|
|
+// )
|
|
|
+// const privateKeyBase64 = btoa(
|
|
|
+// String.fromCharCode(...new Uint8Array(exportedPrivateKey))
|
|
|
+// )
|
|
|
+
|
|
|
+// // 格式化为 PEM 格式(可选,许多库使用 PEM 格式)
|
|
|
+// const publicKeyPem = `-----BEGIN PUBLIC KEY-----\n${publicKeyBase64}\n-----END PUBLIC KEY-----`
|
|
|
+// const privateKeyPem = `-----BEGIN PRIVATE KEY-----\n${privateKeyBase64}\n-----END PRIVATE KEY-----`
|
|
|
+
|
|
|
+// return { publicKey: publicKeyPem, privateKey: privateKeyPem }
|
|
|
+// } catch (error) {
|
|
|
+// console.error('生成密钥对时发生错误:', error)
|
|
|
+// throw error
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// // 使用示例
|
|
|
+// generateKeyPairWebCrypto()
|
|
|
+// .then(({ publicKey, privateKey }) => {
|
|
|
+// console.log('公钥:', publicKey)
|
|
|
+// console.log('私钥:', privateKey)
|
|
|
+// // 注意:在实际应用中,私钥应妥善保管,避免暴露在前端代码或客户端环境中。
|
|
|
+// })
|
|
|
+// .catch(error => {
|
|
|
+// // 处理错误
|
|
|
+// })
|