반응형
companion object {
    private val alg: String = "AES/CBC/PKCS5Padding"
    private val key: String by lazy {
        "your key".substring(0, 16)
    }

    private fun setCipher (mode: Int): Cipher {
        val cipher = Cipher.getInstance(alg)
        cipher?.init(
            mode,
            SecretKeySpec(key.toByteArray(), "AES"),
            IvParameterSpec(key.toByteArray())
        )
        return cipher!!
    }

    fun encrypt(text: String): String {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return Base64.getEncoder()
                .encodeToString(
                    setCipher(Cipher.ENCRYPT_MODE).doFinal(text.toByteArray())
                )
        }
        return ""
    }

    fun decrypt(encryptText : String): String {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return String(
                setCipher(Cipher.ENCRYPT_MODE).doFinal(Base64.getDecoder().decode(encryptText))
            )
        }
        return ""
    }

진행중 ...

반응형

+ Recent posts