Encryptor

class Encryptor(val encryptFn: (String) -> String, val decryptFn: (String) -> String, val maxColLengthFn: (Int) -> Int)

Base cipher class responsible for the encryption and decryption of data.

Samples

import org.springframework.security.crypto.encrypt.AesBytesEncryptor
import org.springframework.security.crypto.keygen.KeyGenerators
import org.springframework.security.crypto.util.EncodingUtils
import java.util.*
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import kotlin.math.ceil

fun main() { 
   //sampleStart 
   return AesBytesEncryptor(
    password.toString(),
    salt,
    KeyGenerators.secureRandom(AES_256_GCM_BLOCK_LENGTH),
    AesBytesEncryptor.CipherAlgorithm.GCM
).run {
    Encryptor(
        { base64Encoder.encodeToString(encrypt(it.toByteArray())) },
        { String(decrypt(base64Decoder.decode(it))) },
        { inputLen ->
            base64EncodedLength(AES_256_GCM_BLOCK_LENGTH + inputLen + AES_256_GCM_TAG_LENGTH)
        }
    )
} 
   //sampleEnd
}

Constructors

Link copied to clipboard
constructor(encryptFn: (String) -> String, decryptFn: (String) -> String, maxColLengthFn: (Int) -> Int)

Properties

Link copied to clipboard

Decrypt ciphertext to a plaintext string.

Link copied to clipboard

Encrypt a plaintext string to ciphertext.

Link copied to clipboard

Convert the expected input length into the maximum encoded length to be stored.

Functions

Link copied to clipboard
fun decrypt(str: String): String

Returns a decrypted value using decryptFn.

Link copied to clipboard
fun encrypt(str: String): String

Returns an encrypted value using encryptFn.

Link copied to clipboard
fun maxColLength(inputByteSize: Int): Int

Returns the maximum column length needed to store an encrypted value, using the specified inputByteSize, and determined by maxColLengthFn.