INNER CODE UNIT · Kotlin
symmetricKeyEncryptionStep1
terl/lazysodium-java · sample-app/src/main/kotlin/Main.kt:94
private fun symmetricKeyEncryptionStep1() {
printSection("Running symmetric key encryption")
val key: Key = lazySodium.cryptoSecretBoxKeygen()
val msg = "This message needs top security"
printStep(
"1",
"Encrypting a message with a symmetric key",
("We will be using the random key '" + key.getAsHexString().toString() + "' and will be " +
"encrypting the message '" + msg + "'.")
)
val nonce: ByteArray = lazySodium.nonce(SecretBox.NONCEBYTES)
val encrypted: String = lazySodium.cryptoSecretBoxEasy(msg, nonce, key)
log()
logt("The encrypted string is $encrypted.")
logt("You should also store the nonce " + lazySodium.toHexStr(nonce) + ".")
logt("The nonce can be stored in a public location.")
symmetricKeyEncryptionStep2(key, nonce, encrypted)
}