In privacy-preserving machine learning and confidential computing, Python is often the first choice for prototyping. Libraries like LightPHE make homomorphic encryption (HE) straightforward for data science and AI workflows. However, when moving these concepts to production in sectors like banking, fintech, healthcare, and enterprise software, Java remains the backbone of core infrastructure. To bridge this gap, I built lightphe4j—a pure, lightweight Java port of LightPHE with native Elliptic Curve arithmetic integrated directly into the library. In this post, we’ll look at why partially homomorphic encryption (PHE) often beats fully homomorphic encryption (FHE) in production, how lightphe4j simplifies key tasks, and how to use it in enterprise Java applications.

Why Partially Homomorphic Encryption?
While Fully Homomorphic Encryption (FHE) allows arbitrary computations on encrypted data, it comes with heavy trade-offs: massive computational overhead, large key sizes, and significant memory requirements.
🙋♂️ You may consider to enroll my top-rated cryptography course on Udemy
For many practical enterprise use cases, you only need additive or multiplicative operations. Opting for Partially Homomorphic Encryption (PHE) gives you:
- Speed: Significantly faster execution times.
- Low Overhead: Dramatically smaller ciphertexts and key sizes.
- Resource Efficiency: Ideal for memory-constrained backend systems and microservices.
What Does lightphe4j Support?
lightphe4j brings a comprehensive set of homomorphic algorithms into the Java ecosystem:
- Additively Homomorphic: Paillier, Damgard-Jurik, Exponential ElGamal, Elliptic Curve ElGamal, Benaloh, Naccache-Stern, Okamoto-Uchiyama.
- Multiplicatively Homomorphic: RSA, ElGamal.
- Somewhat Homomorphic: Boneh-Goh-Nissim (supports unlimited additions and a single multiplication).
- Logical / Bitwise: Goldwasser-Micali (XOR), Sander-Young-Yung (AND).
Additionally, it features full cross-language compatibility: key pairs exported in JSON format from the Python or Go version can be loaded directly into Java, and vice-versa.
Getting Started
lightphe4j requires Java 17 or newer and is available via Maven Central. Just add the following to your pom.xml:
<dependency>
<groupId>io.github.serengil</groupId>
<artifactId>lightphe4j</artifactId>
<version>0.0.25</version>
</dependency>
Practical Examples
1. Offloading Confidential Computations to the Cloud (Paillier)
Suppose a client needs to compute salary updates on untrusted cloud infrastructure without revealing individual compensation figures or holding the private key on the processing node.
import io.github.serengil.lightphe.LightPHE;
import io.github.serengil.lightphe.model.Ciphertext;
import java.math.BigInteger;
public class PayrollExample {
public static void main(String[] args) {
// Initialize an additively homomorphic scheme
LightPHE cs = LightPHE.builder()
.algorithmName("Paillier")
.build();
long baseSalary = 10000; // USD
long bonus = 500; // USD
// Encrypt plaintexts (Can be done client-side or on-premises)
Ciphertext c1 = cs.encrypt(baseSalary);
Ciphertext c2 = cs.encrypt(bonus);
// Homomorphic Addition: perform on untrusted cloud infrastructure
Ciphertext cTotal = c1.add(c2);
// Homomorphic Scalar Multiplication: apply a 5% raise
double multiplier = 1.05;
Ciphertext cAdjusted = c1.multiply(multiplier);
// Decryption: performed securely on-premises using the private key
BigInteger totalResult = cs.decrypt(cTotal);
BigInteger adjustedResult = cs.decrypt(cAdjusted);
System.out.println("Total Salary: $" + totalResult); // Output: 10500
System.out.println("Adjusted Base: $" + adjustedResult); // Output: 10500
}
}
2. Privacy-Preserving Vector Embeddings & Similarity Computation
In modern AI and search systems, comparing vector embeddings without leaking the underlying sensitive data is critical. lightphe4j supports parallelized matrix operations over multi-core CPUs via Java’s Fork-Join pool.
import io.github.serengil.lightphe.LightPHE;
import io.github.serengil.lightphe.model.EncryptedTensor;
import java.util.List;
public class VectorEmbeddingExample {
public static void main(String[] args) {
LightPHE cs = LightPHE.builder()
.algorithmName("Paillier")
.build();
// Plaintext feature embeddings
List<Number> embedding1 = List.of(1.005, 2.05, 3.6, 4.0, 4.02, 3.5);
List<Number> weights = List.of(1.03, 2.04, 3.05, 7.02, 2.01, 1.06);
// Encrypt tensor across available cores
EncryptedTensor encryptedTensor = cs.encryptTensor(embedding1);
// Compute dot product on encrypted tensor using plain weights
EncryptedTensor encryptedDotProduct = encryptedTensor.matmul(weights);
// Decrypt result back on-premises
List<Double> result = cs.decryptTensor(encryptedDotProduct);
System.out.println("Decrypted Dot Product: " + result.get(0));
}
}
3. Cross-Platform Key Management (Python <-> Java)
Since lightphe4j shares a common serialization format with lightphe (Python or Golang), keys generated by a data science team in Python can be loaded straight into a Java enterprise service.
import io.github.serengil.lightphe.LightPHE;
import java.nio.file.Path;
public class KeyManagementExample {
public static void main(String[] args) {
// On-Premises: Generate and export keys
LightPHE onPrem = LightPHE.builder().algorithmName("Paillier").build();
onPrem.exportKeys(Path.of("secret.json"));
onPrem.exportKeys(Path.of("public.json"), true); // Public key only
// Untrusted Cloud Node: Load ONLY the public key
LightPHE cloudService = LightPHE.builder()
.algorithmName("Paillier")
.keyFile(Path.of("public.json"))
.build();
// cloudService can now encrypt and evaluate, but CANNOT decrypt
}
}
Ecosystem Overview
LightPHE is designed to provide consistent APIs across multiple languages depending on your stack requirements:
| Language | Repository | Target Environment |
| Python | serengil/lightphe | AI Prototyping & Data Science |
| Java | serengil/lightphe4j | Enterprise Backends & Financial Systems |
| Go | serengil/lightphe-go | High-Throughput Microservices |
| Typescript | serengil/lightphe-ts | Web Apps & Node.js Edge Services |
Conclusion
By extending the LightPHE ecosystem to Java with lightphe4j, developers can now transition homomorphic encryption designs straight from Python or Go research models into high-performance enterprise systems.
Check out the repository on GitHub, star the project if you find it helpful, and feel free to submit issues or contributions!
- GitHub Repository: serengil/lightphe4j
- Maven Central:
io.github.serengil:lightphe4j
Support this blog financially if you do like!
