mystic-crypt: a Java cryptography library
Encryption, password hashing, digital signatures, key agreement and post-quantum algorithms for Java 25, built on the JDK and Bouncy Castle. It reads PEM and PKCS#8 keys and OpenPGP key homes, and it ships a command line tool.
Getting started
Requires JDK 25 or newer, at build and at runtime. Add the library with the current release 13.11 from Maven Central.
Gradle
dependencies {
implementation "io.github.astrapi69:mystic-crypt:13.11"
}
Maven
<dependency>
<groupId>io.github.astrapi69</groupId>
<artifactId>mystic-crypt</artifactId>
<version>13.11</version>
</dependency>
Command line
The published -all jar runs the command line tool. Pass secrets through standard input
rather than as arguments, because an argument is visible to other processes and lands in the shell history:
java -jar mystic-crypt-13.11-all.jar hash --password-stdin < password.txt
Quick start
Every example below is compiled and run by ReadmeExamplesTest.
Encrypt and decrypt data
AES-256-GCM with a fresh random nonce per call, prepended to the ciphertext:
SecretKey key = SecretKeyFactoryExtensions.newSecretKey(AesAlgorithm.AES.getAlgorithm(), 256);
byte[] encrypted = new BaseByteArrayEncryptor(key)
.encrypt("attack at dawn".getBytes(StandardCharsets.UTF_8));
byte[] decrypted = new BaseByteArrayDecryptor(key).decrypt(encrypted);
Store a password
Argon2id with a random salt; the salt and all parameters travel inside the returned string:
PasswordEncryptor passwordEncryptor = PasswordEncryptor.getInstance();
String stored = passwordEncryptor.hashPasswordArgon2id("correct horse battery staple");
boolean ok = passwordEncryptor.matchArgon2id("correct horse battery staple", stored);
Sign and verify
Ed25519 signatures:
KeyPair keyPair = Ed25519Signer.newKeyPair();
byte[] document = "release 11.0.0".getBytes(StandardCharsets.UTF_8);
byte[] signature = new Ed25519Signer(keyPair.getPrivate()).sign(document);
boolean valid = new Ed25519Verifier(keyPair.getPublic()).verify(document, signature);
Agree on a shared key
X25519 Diffie-Hellman, with the raw shared secret run through HKDF rather than used directly:
KeyPair alice = X25519KeyExchange.newKeyPair();
KeyPair bob = X25519KeyExchange.newKeyPair();
SecretKey aliceSecret = X25519KeyExchange.deriveSharedSecret(alice.getPrivate(), bob.getPublic(), 32);
SecretKey bobSecret = X25519KeyExchange.deriveSharedSecret(bob.getPrivate(), alice.getPublic(), 32);
// aliceSecret and bobSecret are equal
Post-quantum key encapsulation
ML-KEM (FIPS 203). HybridKemKeyExchange combines X25519 and ML-KEM and stays
secure as long as either half is unbroken:
KeyPair recipient = MlKemKeyExchange.newKeyPair(KeyPairGeneratorAlgorithm.ML_KEM_768);
MlKemKeyExchange.Encapsulation sent =
MlKemKeyExchange.encapsulate(recipient.getPublic(), KeyPairGeneratorAlgorithm.ML_KEM_768);
SecretKey received = MlKemKeyExchange.decapsulate(recipient.getPrivate(),
sent.getCiphertext(), KeyPairGeneratorAlgorithm.ML_KEM_768);
// sent.getSharedSecret() and received are equal
Hash
byte[] digest = Sha3Hasher.hashUtf8("hello", MessageDigestAlgorithm.SHA3_256);
Features
Symmetric encryption and files
AES-GCM and ChaCha20-Poly1305 with a fresh nonce per call, for byte arrays, strings, files, streams and serializable objects. Chainable encryptors, hex-encoded variants, key-committing AEAD against the "invisible salamanders" attack. Files and text can be encrypted with a passphrase (AES-GCM, PBKDF2-HMAC-SHA256) from the command line.
Password hashing
Argon2id (recommended), PBKDF2-HMAC-SHA256, bcrypt and scrypt for interop. Salt and parameters are encoded with the hash; comparisons are constant-time.
Keys, certificates and key stores
RSA hybrid encryption, Ed25519 and ML-DSA signatures, X25519 key agreement with HKDF, key pair generation, PEM, DER and PKCS#8 key files from OpenSSL or Java, X.509 certificates, and PKCS12, JKS and JCEKS key stores. Read-only reading of an OpenPGP key home, never through gpg. Ed25519 key blinding and SLIP-0010 seed derivation.
Post-quantum
ML-KEM (FIPS 203), ML-DSA (FIPS 204), SLH-DSA (FIPS 205), and a hybrid X25519 plus ML-KEM combiner for the transition period.
Hashing
SHA-2, SHA-3, BLAKE2b and BLAKE2s including keyed (MAC) mode, HMAC as one call, file and object checksums.
Secret sharing and PAKE
Shamir's Secret Sharing, Feldman verifiable secret sharing, J-PAKE and SRP-6a, AES key wrap, PKCS#11 and HSM provider configuration.
The capability matrix lists every algorithm with the classes involved, and what is deliberately not implemented, and why.
Command line
The published -all artifact is a runnable jar:
java -jar mystic-crypt-13.11-all.jar --help
java -jar mystic-crypt-13.11-all.jar hash --password-stdin < password.txt
java -jar mystic-crypt-13.11-all.jar checksum --file build.gradle --algorithm SHA-256
| Command | Description |
|---|---|
hash | Hash a password with Argon2id or PBKDF2 and print the encoded hash |
verify | Verify a password against an encoded hash (exit code 0 = match) |
keygen | Generate a key pair and print or write it as PEM |
kem | Run an ML-KEM or hybrid X25519+ML-KEM encapsulation between two parties |
checksum | Compute the checksum of a file |
der2pem | Convert a DER-encoded private key to PEM |
cert | Create a self-signed X.509 certificate |
keystore | Manage a PKCS12, JKS or JCEKS key store: list, create, add-keypair, import-cert, export-cert, delete |
sign | Sign a file or stdin with an Ed25519, ML-DSA or SLH-DSA private key (PEM) |
verify-signature | Verify such a signature; exit code 0 = valid, 1 = invalid, 2 = error |
obfuscate / disentangle | Obfuscate text with a substitution map, and recover it |
encrypt / decrypt | Encrypt a file or a text with a passphrase (AES-GCM, PBKDF2-HMAC-SHA256), and decrypt it again |
share | Split a secret into shares and combine them back (Shamir secret sharing) |
keyx | Exchange a shared secret with another person: new on the recipient's side, send on the sender's, receive back on the recipient's |
convert | Say what a key or certificate file is, and convert it between PEM and DER or between PKCS#1 and PKCS#8 |
release-key | Read the state of an OpenPGP release key in a GnuPG home: status, with --home, --key and --json. Only reads |
A desktop application that uses this library is mystic-crypt-ui, a separate project.
Frequently asked questions
Which Java version does mystic-crypt need?
JDK 25 or newer, both to build and to run. The published jar targets JDK 25 bytecode and will not load on an older JVM.
Which key and keystore formats does it read and write?
PEM, DER and PKCS#8 private and public keys from OpenSSL or Java, X.509 certificates, and PKCS12, JKS and JCEKS key stores. A GnuPG key home is read-only: the OpenPGP key reader reads pubring.kbx and the secret-key files through Bouncy Castle, never through gpg.
Is mystic-crypt post-quantum?
Yes. It includes ML-KEM (FIPS 203), ML-DSA (FIPS 204) and SLH-DSA (FIPS 205), and a hybrid X25519 plus ML-KEM combiner. ML-KEM and ML-DSA are provided by the JDK since JDK 24; this library runs on JDK 25 at the earliest, and there they work without Bouncy Castle registered. SLH-DSA is not in the JDK and needs Bouncy Castle registered as a security provider. The project does not claim FIPS 140 validation for any module.
How safe is decrypting Java objects since 13.8?
Objects decrypted by PrivateKeyDecryptor, PrivateKeyGenericDecryptor and GenericObjectDecryptor pass an ObjectInputFilter. It admits only the classes the caller names, String by default, and bounds depth, references, array length and size. Callers that decrypt other types name them in the constructors that take a class collection. Java serialization is still the format; replacing it is a later step.
Can I pass a password on the command line?
The commands read secrets from standard input with their -stdin option, or ask for them in a terminal without echo. Passing a secret as an argument still works in 13.x, prints a warning, and is removed in 14.0.
How do I verify a release?
Import the signing key, then run gpg --verify on the .asc file next to the jar. The fingerprints to compare are in the Security section of this page.
Does the library or this page send data anywhere?
The library opens no network connections; it contains no socket or HTTP client code. This page loads no scripts, fonts, images or trackers from other hosts and sets no cookies. A link leaves the page only when you follow it.
Security
Report a vulnerability through GitHub's private vulnerability reporting, not as a public issue. SECURITY.md names the supported lines (currently 13.x) and what happens after a report.
Known advisories and fixes
Up to 13.7, PrivateKeyDecryptor, PrivateKeyGenericDecryptor and
GenericObjectDecryptor built whatever classes the ciphertext named, without a filter
(GHSA-87j9-xjf5-f387). 13.8 fixes this. The
changelog describes the fix and what callers must change.
Before 13.7, generated passwords, salts and GCM nonces came from the default generator of randomizer,
which randomizer before 10.5 seeded with the current time
(GHSA-rmpr-hqmh-r8gp). 13.7 fixes this. Since 13.9
these values come from the JDK's SecureRandom directly, and the library no longer depends on
randomizer at runtime. Key generation was not affected: it always used the JDK's source.
Verify a release signature
Since 13.6 the releases are signed by a signing subkey. Import the key once, then verify the signature next to the jar:
gpg --keyserver hkps://keys.openpgp.org --recv-keys 5B2FA6B11E298BC09287F423D8C403518C49CA75
gpg --verify mystic-crypt-13.11.jar.asc mystic-crypt-13.11.jar
The primary key fingerprint must be 5B2F A6B1 1E29 8BC0 9287 F423 D8C4 0351 8C49 CA75,
and for a release signed by the subkey its fingerprint must be
6D67 F844 2A6F CC96 BD7C 1B5E 9FCF 7C97 10E2 BD8D.
The README explains the trust warning gpg prints.
Documentation
| Document | What it covers |
|---|---|
| Capability matrix | Every algorithm, its class, and what is out of scope, with reasons |
| Testing strategy | How the library is tested, the coverage and mutation numbers, and the defects the process caught |
| Coverage exceptions | Every uncovered line and surviving mutant, with its reason |
| Key agreement, explained | From book ciphers to the Double Ratchet |
| Changelog | What changed in each release |
| GitHub releases | Release notes and the tags |
| Javadoc | API reference |
| Wiki | Task-oriented guides |
The crypt family
mystic-crypt is the top of a three-repository stack: crypt-api holds the algorithm constants and interfaces, crypt-data the factories, readers, writers and models, and mystic-crypt the ready-to-use encryptors.
Quality
The release gate is 100% branch coverage plus PIT mutation testing, and every uncovered line and surviving mutant has a stated reason. The measured numbers, with the commit they were measured on, are in docs/TESTING.md.
Contributing
Pull requests are welcome: fork astrapi69/mystic-crypt and open a PR against develop. Please add tests for your change; the conventions for contributors describe what a good test looks like here.
./gradlew build # tests + coverage + formatting check
./gradlew spotlessApply # fix formatting
./gradlew pitest # mutation testing (opt-in, minutes)
Questions, bug reports and feature requests belong on the issues page.
Support this project
If mystic-crypt is useful to you, a star on GitHub helps others find it. Sharing it or reporting a bug helps just as much. Financial support is possible through GitHub Sponsors, Liberapay, Ko-fi and PayPal.