HiTLS4J is a Java Cryptography Extension (JCE) provider that wraps the native openHiTLS cryptographic library. It provides a complete implementation of the JCE API, allowing Java applications to use the cryptographic algorithms provided by openHiTLS through standard Java security interfaces.
HiTLS4J integrates the openHiTLS cryptographic library with Java applications through JNI (Java Native Interface). It implements a JCE provider that can be registered with the Java Security framework, enabling the use of various cryptographic algorithms through standard Java APIs.
HiTLS4J provides the following cryptographic functionalities:
- AES: Supports ECB, CBC, CTR, and GCM modes with various padding options
- SM4: Supports ECB, CBC, CTR, GCM, CFB, OFB, and XTS modes with various padding options
- RSA: Encryption/decryption with PKCS#1 padding
- SM2: Chinese standard for elliptic curve-based asymmetric encryption
- SHA Family: SHA-1, SHA-224, SHA-256, SHA-384, SHA-512
- SHA3 Family: SHA3-224, SHA3-256, SHA3-384, SHA3-512
- SM3: Chinese cryptographic hash function
- HMAC: With all supported hash algorithms (SHA family, SHA3 family, SM3)
- RSA: With SHA-224, SHA-256, SHA-384, SHA-512, and SM3 hash algorithms
- RSA-PSS: Probabilistic Signature Scheme with various hash algorithms
- DSA: Digital Signature Algorithm with various hash algorithms
- ECDSA: Elliptic Curve Digital Signature Algorithm
- SM2: Chinese standard for elliptic curve-based digital signatures
- RSA: Key pair generation
- DSA: Key pair generation
- EC: Key pair generation for various curves (secp256r1, secp384r1, secp521r1, sm2p256v1)
- Symmetric Keys: Generation for AES and SM4
- Java 17 or higher
- openHiTLS library installed on the system
- GCC compiler for building the JNI component
- Install the openHiTLS library on your system
- Set the
JAVA_HOMEenvironment variable to your JDK installation - Ensure GCC is available in your PATH
-
Clone the repository:
git clone https://github.com/yourusername/hitls4j.git cd hitls4j -
Configure the openHiTLS root directory in
pom.xml:<properties> <openhitls.root>/path/to/openhitls</openhitls.root> </properties>
-
Build the project:
mvn clean package
import java.security.Security;
import org.openhitls.crypto.jce.provider.HiTls4jProvider;
// Register the provider
Security.addProvider(new HiTls4jProvider());import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.openhitls.crypto.jce.provider.HiTls4jProvider;
// Create key and IV
byte[] keyBytes = new byte[16]; // 128-bit key
byte[] ivBytes = new byte[16]; // 16-byte IV
// ... initialize key and IV with secure random data
// Create key specification
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec iv = new IvParameterSpec(ivBytes);
// Create and initialize cipher
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", HiTls4jProvider.PROVIDER_NAME);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
// Encrypt data
byte[] plaintext = "Hello, world!".getBytes();
byte[] ciphertext = cipher.doFinal(plaintext);
// Decrypt data
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] decrypted = cipher.doFinal(ciphertext);import java.security.MessageDigest;
import org.openhitls.crypto.jce.provider.HiTls4jProvider;
// Create message digest
MessageDigest md = MessageDigest.getInstance("SHA-256", HiTls4jProvider.PROVIDER_NAME);
// Compute hash
byte[] data = "Hello, world!".getBytes();
byte[] hash = md.digest(data);import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.openhitls.crypto.jce.provider.HiTls4jProvider;
// Create key
byte[] keyBytes = new byte[32]; // 256-bit key
// ... initialize key with secure random data
SecretKeySpec key = new SecretKeySpec(keyBytes, "HMACSHA256");
// Create and initialize HMAC
Mac mac = Mac.getInstance("HMACSHA256", HiTls4jProvider.PROVIDER_NAME);
mac.init(key);
// Compute HMAC
byte[] data = "Hello, world!".getBytes();
byte[] hmac = mac.doFinal(data);import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Signature;
import org.openhitls.crypto.jce.provider.HiTls4jProvider;
// Generate key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", HiTls4jProvider.PROVIDER_NAME);
keyGen.initialize(2048);
KeyPair keyPair = keyGen.generateKeyPair();
// Create and initialize signature
Signature signature = Signature.getInstance("SHA256withRSA", HiTls4jProvider.PROVIDER_NAME);
signature.initSign(keyPair.getPrivate());
// Sign data
byte[] data = "Hello, world!".getBytes();
signature.update(data);
byte[] signatureBytes = signature.sign();
// Verify signature
signature.initVerify(keyPair.getPublic());
signature.update(data);
boolean valid = signature.verify(signatureBytes);import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Signature;
import java.security.spec.ECGenParameterSpec;
import org.openhitls.crypto.jce.provider.HiTls4jProvider;
// Generate key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", HiTls4jProvider.PROVIDER_NAME);
keyGen.initialize(new ECGenParameterSpec("secp256r1"));
KeyPair keyPair = keyGen.generateKeyPair();
// Create and initialize signature
Signature signature = Signature.getInstance("SHA256withECDSA", HiTls4jProvider.PROVIDER_NAME);
signature.initSign(keyPair.getPrivate());
// Sign data
byte[] data = "Hello, world!".getBytes();
signature.update(data);
byte[] signatureBytes = signature.sign();
// Verify signature
signature.initVerify(keyPair.getPublic());
signature.update(data);
boolean valid = signature.verify(signatureBytes);AES(with modes: ECB, CBC, CTR, GCM)SM4(with modes: ECB, CBC, CTR, GCM, CFB, OFB, XTS)RSASM2
SHA-1SHA-224,SHA-256,SHA-384,SHA-512SHA3-224,SHA3-256,SHA3-384,SHA3-512SM3
HMACSHA1HMACSHA224,HMACSHA256,HMACSHA384,HMACSHA512HMACSHA3-224,HMACSHA3-256,HMACSHA3-384,HMACSHA3-512HMACSM3
SHA224withRSA,SHA256withRSA,SHA384withRSA,SHA512withRSA,SM3withRSASHA224withRSA/PSS,SHA256withRSA/PSS,SHA384withRSA/PSS,SHA512withRSA/PSS,SM3withRSA/PSSSHA256withECDSA,SHA384withECDSA,SHA512withECDSASM3withSM2
RSADSAEC(with curves: secp256r1, secp384r1, secp521r1, sm2p256v1)AESSM4
This project is licensed under the terms of the license included in the repository.
- This project is based on the openHiTLS cryptographic library
- Thanks to all contributors who have helped with the development of this project