Library for data encryption using symmetric encryption algorithms.
implementation("io.github.jhdcruz:kipher-symmetric:$version")
<dependency>
<groupId>io.github.jhdcruz</groupId>
<artifactId>kipher-symmetric</artifactId>
<version>$version</version>
</dependency>
Data encryption example using AES:
import io.github.jhdcruz.kipher.symmetric.aes.AesGCM
fun main() {
val encryptionUtils = AesGCM()
val data = "sample data".encodeToByteArray()
val tag = "sample aad".encodeToByteArray()
// named parameters are recommended, but optional
val encrypted = encryptionUtils.encrypt(
data = message,
tag = tag,
// optional `key` parameter
) // returns Map of [data, key, tag]
val decrypted = encryptionUtils.decrypt(encrypted)
// or, manually:
val decrypted = encryptionUtils.decrypt(
encrypted = encrypted.getValue("data"),
key = encrypted.getValue("key")
tag = encrypted.getValue("tag")
)
println(decryptedPass.toString())
}
import io.github.jhdcruz.kipher.symmetric.aes.AesGCM;
import java.util.Map;
public class Main {
public static void main(String[] args) {
AesGCM encryptionUtils = new AesGCM();
byte[] data = "Hello World".getBytes();
Map<String, byte[]> encrypted = encryptionUtils.encrypt(data);
byte[] val = encryptionUtils.decrypt(encrypted);
// or
byte[] val = encryptionUtils.decrypt(
encrypted.get("data"),
encrypted.get("key")
encrypted.get("tag")
);
System.out.println(new String(val)); // outputs "Hello World"
}
}
import io.github.jhdcruz.kipher.symmetric.aes.AesCBC
fun main() {
val encryptionUtils = AesCBC()
val data = "sample data".encodeToByteArray()
val secretKey: ByteArray = encryptionUtils.generateKey(128) // should be a valid one
val encrypted = encryptionUtils.encrypt(
data = message,
key = secretKey
)
val decrypted = encryptionUtils.decrypt(encrypted)
println(decryptedPass.toString(), Charsets.UTF_8) // outputs "sample data"
}
import io.github.jhdcruz.kipher.symmetric.SymmetricEncryption
import java.security.Provider
import java.security.Security
class Main {
fun main() {
// must be declared before using any symmetric ciphers methods!
val provider: Provider = Security.getProvider("SunJCE")
SymmetricEncryption.provider(provider)
}
}
encrypt
decrypt
Easy and straightforward methods, but relies on internal implementation.
You cannot decrypt a data that was encrypted by a different method or library. Unless they use the same internal implementation as this library.
encryptBare
decryptBare
Requires all the necessary data for the encryption/decryption process, such as IV, key, AADs, whatever that is applicable.
You can decrypt data that was encrypted by a different method or library. Unless they involve a different or custom implementation of the encryption/decryption process.
Method | Description |
---|---|
extract |
Get the encryption details from an encrypted data encrypted using kipher. |
concat |
Concatenates the data, iv, and aad (if applicable) into a single ByteArray. |