A Homomorphic Encryption-Driven Framework for Secure Cloud-Based Facial Recognition
Project description
CipherFace
CipherFace is a hybrid homomorphic encryption-driven python framework for secure cloud-based facial recognition supporting both partially homomorphic encryption and fully homomorphic encryption. It combines DeepFace, LightPHE and TenSEAL libraries.
Installation 
The easiest way to install CipherFace is to download it from PyPI. It's going to install the library itself and its prerequisites as well.
$ pip install cipherface
Alternatively, you can also install deepface from its source code. Source code may have new features not published in pip release yet.
$ git clone https://github.com/serengil/cipherface.git
$ cd cipherface
$ pip install -e .
Once you installed the library, then you will be able to import it and use its functionalities.
from cipherface import CipherFace, CipherFace Lite
Partially Homomorphic Encryption
You need to initialize CipherFaceLite to use PHE. Currently, Paillier, Damgard-Jurik, Okamoto-Uchiyama cryptosystems are supported in CipherFaceLite.
On Prem Encryption
When you initialize a CipherFaceLite object, it sets up an PHE cryptosystem. Currently, it supports the VGG-Face, Facenet, and Facenet512 facial recognition models; cosine similarity.
# build a cryptosystem
onprem = CipherFaceLite(
model_name="Facenet",
algorithm_name = "Paillier",
)
# export keys of built cryptosystem
onprem.export_private_key("private.txt")
onprem.export_public_key("public.txt")
# create vector embedding for 1st image and encrypt in one shot
source_embedding_encrypted = onprem.securely_embed(img_path="dataset/img1.jpg")
The on-prem system should generate embeddings for its facial database and encrypt them in advance. This process only needs to be done once to extract the encrypted embeddings. Once encrypted, these embeddings can be securely stored in the cloud.
Encrypted Similarity Calculation On Cloud
The cloud can also generate vector embeddings. Additionally, it can compute the encrypted distance between a recently generated plain embedding and an encrypted embedding created on the on-prem side.
# cloud loads cryptosystem with public key
onprem = CipherFaceLite(
model_name="Facenet",
algorithm_name = "Paillier",
cryptosystem="public.txt",
)
# create vector embedding for target image and encrypt in one shot
target_embedding = cloud.represent(img_path="dataset/target.jpg")[0]
# compare encrypted embedding and plain embedding
encrypted_similarity = cloud.encrypted_compare(
source_embedding_encrypted,
target_embedding
)
On Prem Verification
Once the cloud calculates the encrypted similarity, only the on-prem system can decrypt it since it holds the private key of the cryptosystem. This allows the on-prem system to determine whether the source and target images belong to the same person or different individuals.
# on prem loads cryptosystem with private key
onprem = CipherFaceLite(
model_name="Facenet",
algorithm_name = "Paillier",
cryptosystem="private.txt",
)
# on prem restores distance
decrypted_similarity = onprem.restore(encrypted_similarity)
# verification
is_verified = onprem.verify(decrypted_similarity)
if is_verified is True:
print("they are same person")
else:
print("they are different persons")
In this setup, the cloud system performs the similarity calculation, utilizing most of the computational power. The on-prem system, holding the private key, is only responsible for decrypting the similarity to determine whether the images belong to the same person or different individuals.
Fully Homomorphic Encryption
You need to initialize CipherFace object to use FHE.
On Prem Encryption
When you initialize a CipherFace object, it sets up an FHE cryptosystem. Currently, CipherFace supports the VGG-Face, Facenet, and Facenet512 facial recognition models, as well as Euclidean and cosine distance metrics.
# build a cryptosystem
onprem = CipherFace(
model_name="Facenet",
distance_metric="euclidean",
)
# export keys of built cryptosystem
onprem.export_private_key("private.txt")
onprem.export_public_key("public.txt")
# create vector embedding for 1st image and encrypt in one shot
source_embedding_encrypted = onprem.securely_embed(img_path="dataset/img1.jpg")
The on-prem system should generate embeddings for its facial database and encrypt them in advance. This process only needs to be done once to extract the encrypted embeddings. Once encrypted, these embeddings can be securely stored in the cloud.
Encrypted Distance Calculation On Cloud
The cloud can also generate vector embeddings and encrypt them since encryption only requires a public key. Additionally, it can compute the encrypted distance between a recently generated encrypted embedding and an encrypted embedding created on the on-prem side.
# cloud loads cryptosystem with public key
onprem = CipherFace(
model_name="Facenet",
distance_metric="euclidean",
cryptosystem="public.txt",
)
# create vector embedding for target image and encrypt in one shot
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 Verification
Once the cloud calculates the encrypted distance, only the on-prem system can decrypt it since it holds the private key of the cryptosystem. This allows the on-prem system to determine whether the source and target images belong to the same person or different individuals.
# on prem loads cryptosystem with private key
onprem = CipherFace(
model_name="Facenet",
distance_metric="euclidean",
cryptosystem="private.txt",
)
# on prem restores distance
decrypted_distance = onprem.restore(encrypted_distance)
# verification
is_verified = onprem.verify(decrypted_distance)
if is_verified is True:
print("they are same person")
else:
print("they are different persons")
In this setup, the cloud system performs the distance calculation, utilizing most of the computational power. The on-prem system, holding the private key, is only responsible for decrypting the distances to determine whether the images belong to the same person or different individuals.
Contribution
Pull requests are more than welcome! If you are planning to contribute a large patch, please create an issue first to get any upfront questions or design decisions out of the way first.
Before creating a PR, you should run the unit tests and linting locally by running make test && make lint command. Once a PR sent, GitHub test workflow will be run automatically and unit test and linting jobs will be available in GitHub actions before approval.
Support
There are many ways to support a project - starring⭐️ the GitHub repo is just one 🙏
If you do like this work, then you can support it financially on Patreon, GitHub Sponsors or Buy Me a Coffee.
Citation
Please cite CipherFace in your publications if it helps your research. Here is its BibTex entry:
@misc{serengil2025cipherface,
title = {CipherFace: A Fully Homomorphic Encryption-Driven Framework for Secure Cloud-Based Facial Recognition},
author = {Serengil, Sefik and Ozpinar, Alper},
year = {2025},
publisher = {arXiv},
url = {https://arxiv.org/abs/2502.18514},
doi = {10.48550/arXiv.2502.18514}
}
Licence
CipherFace is licensed under the MIT License - see LICENSE for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cipherface-0.0.2.tar.gz.
File metadata
- Download URL: cipherface-0.0.2.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.31.0 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.50.2 importlib-metadata/6.8.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e22f5eee0df6c770acfcc0e57758dc2442ce7de3dceb32498a2d547de8dc2c07
|
|
| MD5 |
541751e95eb42706d99ab297f065fd7b
|
|
| BLAKE2b-256 |
886d4d2b7bceb1240ad04c1be3164686ee1038f689d45f7a00efef896c52f40a
|
File details
Details for the file cipherface-0.0.2-py3-none-any.whl.
File metadata
- Download URL: cipherface-0.0.2-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.31.0 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.50.2 importlib-metadata/6.8.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a985698de61c087052f6d1c1d43ecd2ec48de53fa4a95dd7b17b6f0c0fb36586
|
|
| MD5 |
b89a9eb65f0b8f9e9afab90f147e4386
|
|
| BLAKE2b-256 |
aba52eb771858da281fef46f1e8e236f30af715ab4d1bee5ccfb4443c001b177
|