serengil/cipherface
GitHub: serengil/cipherface
一个结合同态加密与深度学习人脸识别的隐私保护框架,使云端能够在不解密的情况下完成人脸相似度计算。
Stars: 20 | Forks: 4
# CipherFace
[](https://pepy.tech/project/cipherface)
[](https://github.com/serengil/cipherface/stargazers)
[](https://github.com/serengil/cipherface/blob/master/LICENSE)
[](https://github.com/serengil/cipherface/actions/workflows/tests.yml)
[](https://arxiv.org/abs/2503.05850)
[](https://arxiv.org/abs/2502.18514)
CipherFace 是一个由混合同态加密驱动的 Python 框架,用于安全的云端面部识别,同时支持部分同态加密和全同态加密。它结合了 [DeepFace](https://github.com/serengil/deepface)、[LightPHE](https://github.com/serengil/lightphe) 和 [TenSEAL](https://github.com/OpenMined/TenSEAL) 库。
## 安装 [](https://pypi.org/project/cipherface/)
安装 CipherFace 最简单的方法是从 [`PyPI`](https://pypi.org/project/cipherface/) 下载。这将会安装该库本身及其先决条件。
```
$ pip install cipherface
```
或者,您也可以从其源代码安装 deepface。源代码可能包含尚未在 pip 发布版本中发布的新功能。
```
$ git clone https://github.com/serengil/cipherface.git
$ cd cipherface
$ pip install -e .
```
安装该库后,您将能够导入它并使用其功能。
```
from cipherface import CipherFace, CipherFace Lite
```
# 部分同态加密
您需要初始化 CipherFaceLite 以使用 PHE。目前,CipherFaceLite 支持 [Paillier](https://sefiks.com/2023/04/03/a-step-by-step-partially-homomorphic-encryption-example-with-paillier-in-python/)、[Damgard-Jurik](https://sefiks.com/2023/10/20/a-step-by-step-partially-homomorphic-encryption-example-with-damgard-jurik-in-python/)、[Okamoto-Uchiyama](https://sefiks.com/2023/10/20/a-step-by-step-partially-homomorphic-encryption-example-with-okamoto-uchiyama-in-python/) 密码系统。
## 本地加密
当您初始化一个 CipherFaceLite 对象时,它会建立一个 PHE 密码系统。目前,它支持 [`VGG-Face`](https://sefiks.com/2018/08/06/deep-face-recognition-with-keras/)、[`Facenet`](https://sefiks.com/2018/09/03/face-recognition-with-facenet-in-keras/) 和 [`Facenet512`](https://sefiks.com/2018/09/03/face-recognition-with-facenet-in-keras/) 面部识别模型;余弦相似度。
```
# 构建 cryptosystem
onprem = CipherFaceLite(
model_name="Facenet",
algorithm_name = "Paillier",
)
# 导出已构建的 cryptosystem 的密钥
onprem.export_private_key("private.txt")
onprem.export_public_key("public.txt")
# 为第一张图像创建 vector embedding 并一次性加密
source_embedding_encrypted = onprem.securely_embed(img_path="dataset/img1.jpg")
```
本地系统应为其面部数据库生成 embedding 并提前加密。此过程只需执行一次即可提取加密的 embedding。一旦加密,这些 embedding 就可以安全地存储在云端。
## 云端加密相似度计算
云端也能生成向量 embedding。此外,它能够计算最近生成的明文 embedding 与本地端创建的加密 embedding 之间的加密距离。
```
# cloud 使用公钥加载 cryptosystem
onprem = CipherFaceLite(
model_name="Facenet",
algorithm_name = "Paillier",
cryptosystem="public.txt",
)
# 为目标图像创建 vector embedding 并一次性加密
target_embedding = cloud.represent(img_path="dataset/target.jpg")[0]
# 比较 encrypted embedding 和 plain embedding
encrypted_similarity = cloud.encrypted_compare(
source_embedding_encrypted,
target_embedding
)
```
## 本地验证
一旦云端计算出加密的相似度,只有本地系统能够对其进行解密,因为它持有密码系统的私钥。这使得本地系统能够确定源图像和目标图像是属于同一个人还是不同的人。
```
# on prem 使用私钥加载 cryptosystem
onprem = CipherFaceLite(
model_name="Facenet",
algorithm_name = "Paillier",
cryptosystem="private.txt",
)
# on prem 恢复距离
decrypted_similarity = onprem.restore(encrypted_similarity)
# 验证
is_verified = onprem.verify(decrypted_similarity)
if is_verified is True:
print("they are same person")
else:
print("they are different persons")
```
在此设置中,云端系统执行相似度计算,利用了大部分计算能力。持有私钥的本地系统仅负责解密相似度,以确定图像是属于同一个人还是不同的人。
# 全同态加密
您需要初始化 CipherFace 对象以使用 FHE。
## 本地加密
当您初始化一个 CipherFace 对象时,它会建立一个 FHE 密码系统。目前,CipherFace 支持 [`VGG-Face`](https://sefiks.com/2018/08/06/deep-face-recognition-with-keras/)、[`Facenet`](https://sefiks.com/2018/09/03/face-recognition-with-facenet-in-keras/) 和 [`Facenet512`](https://sefiks.com/2018/09/03/face-recognition-with-facenet-in-keras/) 面部识别模型,以及欧几里得和余弦距离度量。
```
# 构建 cryptosystem
onprem = CipherFace(
model_name="Facenet",
distance_metric="euclidean",
)
# 导出已构建的 cryptosystem 的密钥
onprem.export_private_key("private.txt")
onprem.export_public_key("public.txt")
# 为第一张图像创建 vector embedding 并一次性加密
source_embedding_encrypted = onprem.securely_embed(img_path="dataset/img1.jpg")
```
本地系统应为其面部数据库生成 embedding 并提前加密。此过程只需执行一次即可提取加密的 embedding。一旦加密,这些 embedding 就可以安全地存储在云端。
## 云端加密距离计算
云端也能生成向量 embedding 并对其进行加密,因为加密只需要公钥。此外,它能够计算最近生成的加密 embedding 与本地端创建的加密 embedding 之间的加密距离。
```
# cloud 使用公钥加载 cryptosystem
onprem = CipherFace(
model_name="Facenet",
distance_metric="euclidean",
cryptosystem="public.txt",
)
# 为目标图像创建 vector embedding 并一次性加密
target_embedding_encrypted = cloud.securely_embed(img_path="dataset/target.jpg")[0]
encrypted_distance = cloud.encrypted_compare(
target_embedding_encrypted,
source_embedding_encrypted
)
```
## 本地验证
一旦云端计算出加密距离,只有本地系统能够对其进行解密,因为它持有密码系统的私钥。这使得本地系统能够确定源图像和目标图像是属于同一个人还是不同的人。
```
# on prem 使用私钥加载 cryptosystem
onprem = CipherFace(
model_name="Facenet",
distance_metric="euclidean",
cryptosystem="private.txt",
)
# on prem 恢复距离
decrypted_distance = onprem.restore(encrypted_distance)
# 验证
is_verified = onprem.verify(decrypted_distance)
if is_verified is True:
print("they are same person")
else:
print("they are different persons")
```
在此设置中,云端系统执行距离计算,利用了大部分计算能力。持有私钥的本地系统仅负责解密距离,以确定图像是属于同一个人还是不同的人。
## 引用
如果 CipherFace 对您的研究有帮助,请在您的出版物中引用它。以下是它的 BibTeX 条目:
```
@article{serengil2025cipherfacelite,
title={Encrypted Vector Similarity Computations Using Partially Homomorphic Encryption: Applications and Performance Analysis},
author={Serengil, Sefik and Ozpinar, Alper},
journal={arXiv preprint arXiv:2503.05850},
note={doi: 10.48550/arXiv.2503.05850. [Online]. Available: \url{https://arxiv.org/abs/2503.05850}},
year={2025}
}
```
```
@article{serengil2025cipherface,
title={CipherFace: A Fully Homomorphic Encryption-Driven Framework for Secure Cloud-Based Facial Recognition},
author={Serengil, Sefik and Ozpinar, Alper},
journal={arXiv preprint arXiv:2502.18514},
note={doi: 10.48550/arXiv.2502.18514. [Online]. Available: \url{https://arxiv.org/abs/2502.18514}},
year={2025}
}
```
## 许可证
CipherFace 是在 MIT 许可证下授权的 - 有关详细信息,请参阅 [`LICENSE`](https://github.com/serengil/cipherface/blob/master/LICENSE)。标签:DeepFace, FHE, LightPHE, ML隐私保护, PHE, Python框架, TenSEAL, 云计算安全, 人脸识别, 全同态加密, 加密计算, 同态加密, 安全多方计算, 密码学, 手动系统调用, 机器学习即服务, 生物识别安全, 计算机视觉, 逆向工具, 部分同态加密, 隐私计算