# Efficient and Secure EMR Storage and Sharing Scheme Based on Hyperledger Fabric and IPFS
## 5 Scheme Implementation
### Data Sharing Phase
#### 5.5.1 Principle of AES-PRE Implementation  
To address the potential theft or tampering of shared data during the data sharing process, the system incorporates proxy re-encryption technology [28] to ensure the security of shared data. The nodes in the IPFS only provide proxy services for key transformation and cannot access plaintext data. The proxy re-encryption algorithm, based on asymmetric encryption, is often inefficient for large volumes of data sharing. Therefore, in the proposed method, we introduce the AES encryption algorithm and combine it with proxy re-encryption to improve the efficiency of the encryption process. We assume that [PERSON] and [PERSON] are the data exchangers. The specific process is described as follows:  
1. [PERSON] has \(sk_{A}\) and \(pk_{A}\), and [PERSON] has \(sk_{B}\) and \(pk_{B}\).
2. [PERSON] generates a symmetric key, \(k_{AES}\), encrypts the plaintext \(M\) with \(k_{AES}\) to generate ciphertext \(C\), encrypts \(k_{AES}\) with \(pk_{A}\) to generate \(C_{(AES)}\), and sends \(C\) and \(C_{(AES)}\) to the proxy.
3. [PERSON] requests data from [PERSON] and sends his public key \(pk_{B}\) to [PERSON].
4. [PERSON] generates the proxy re-encryption key \(ReKey_{A\to B}\) based on \(sk_{A}\) and \(pk_{B}\) and sends \(ReKey_{A\to B}\) to the proxy.
5. The proxy transforms \(C_{(AES)}\) into \(C_{(AES)_{new}}\) based on \(ReKey_{A\to B}\).
6. [PERSON] retrieves \(C\) and \(C_{(AES)_{new}}\) and then decrypts \(C_{(AES)_{new}}\) using \(sk_{B}\) to obtain \(k_{AES}\). With \(k_{AES}\), he decrypts \(C\) to retrieve the plaintext data. With AES-PRE, we can achieve higher efficiency while ensuring data security. We next present one way to implement AES-PRE. In this method, the AES-256 algorithm is used to encrypt the shared data. A finite field \(\mathcal{F}_{r}\) is defined, where the elements are 256 bits. Let A be the data sender and B be the data receiver. The principle of the AES-PRE process is described as follows. First, define the generators of two groups. Suppose \(g\) and \(h\) are generators of groups \(G_{1}\) and \(G_{2}\), respectively; then, \(Z=e(g,h),e:G_{1}\times G_{2}\to G_{7}\). Next, generate asymmetric keys for A and B as shown in Equation (7): \[\begin{split} sk_{A}\in\mathcal{F}_{r},pk_{A}=g^{sk_{A}}\in G_{1} \\ sk_{B}\in\mathcal{F}_{r},pk_{B}=h^{sk_{B}}\in G_{2}\end{split}\] (7) A encrypts the AES key \(K_{AES}\) to obtain the ciphertext \(C_{(AES)}=\left((pk_{A})^{k},K_{AES}Z^{k}\right)\). Let \(\alpha=\left(pk_{A}\right)^{k}\) and \(\beta=K_{AES}Z^{k}=K_{AES}e(g,h)^{k}\). Decrypting the ciphertext \(C_{(AES)}\) using [PERSON]'s private key \(sk_{A}\) can be described as shown in Equation (8): \[\begin{split} Dec_{sk_{A}}\Big{(}C_{(AES)}\Big{)}=K_{AES}\end{split}\] (8) A generates the proxy re-encryption key \(ReKey_{A\to B}\) using \(sk_{A}\) and \(pk_{B}\), as shown in Equation (9): \[\begin{split} ReKey_{A\to B}=\left(pk_{B}\right)^{\frac{1}{ \alpha_{A}}}\in G_{2}\end{split}\] (9) According to \(C_{(AES)}=(\alpha,\beta)\), then, the proxy can utilize the re-encryption key \(ReKey_{A\to B}\) to compute \(\alpha^{\prime}=e(\alpha,ReKey_{A\to B})\) to obtain the re-encryption ciphertext \(C_{(AES)new}=(\alpha^{\prime},\beta)\). Finally, data recipient B can decrypt \(C_{(AES)new}\) using their own private key to obtain \(K_{AES}=ReDec_{sk_{B}}\Big{(}C_{(AES)new}\Big{)}\). Using \(K_{AES}\) decrypts the ciphertext of medical data to obtain its plaintext. For the correctness of the decryption process, a proof of the re-encryption-decryption process can be constructed as follows:  
\[\begin{split}\frac{\beta}{(\alpha^{\prime})^{\frac{1}{\alpha_{B }}}}=\frac{K_{AES}e(g,h)^{k}}{e(\alpha,ReKey_{A\to B})^{\frac{1}{\alpha_{B}}} }=\frac{K_{AES}e(g,h)^{k}}{e((pk_{A})^{k},(pk_{B})^{\frac{1}{\alpha_{A}}})^{ \frac{1}{\alpha_{B}}}}=\frac{K_{AES}e(g,h)^{k}}{e((g^{sk_{A}})^{k},(h^{sk_{B} })^{\frac{1}{\alpha_{A}}})^{\frac{1}{\alpha_{B}}}}=K_{AES}\end{split} \tag{10}\]  
As shown in Equation (10), AES-PRE satisfies correctness.