Skip to content

Instantly share code, notes, and snippets.

@jisungbin
Created April 18, 2020 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jisungbin/dbe0a40c940a940d738b594e4dddffd9 to your computer and use it in GitHub Desktop.
Save jisungbin/dbe0a40c940a940d738b594e4dddffd9 to your computer and use it in GitHub Desktop.
package com.sungbin.autoreply.bot.three.utils
import android.util.Base64
import java.lang.Exception
import java.security.spec.AlgorithmParameterSpec
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
object AES256Encryption {
private val ivBytes = byteArrayOf(
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00
)
private const val secretKey = "KakaoTalkBotHub"
fun encode(str: String): String {
return try {
val textBytes = str.toByteArray(charset("UTF-8"))
val ivSpec: AlgorithmParameterSpec =
IvParameterSpec(ivBytes)
val newKey = SecretKeySpec(
secretKey.toByteArray(charset("UTF-8")),
"AES"
)
val cipher: Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec)
Base64.encodeToString(cipher.doFinal(textBytes), 0)
} catch (e: Exception){
str
}
}
fun decode(str: String): String {
val textBytes = Base64.decode(str, 0)
val ivSpec: AlgorithmParameterSpec =
IvParameterSpec(ivBytes)
val newKey = SecretKeySpec(
secretKey.toByteArray(charset("UTF-8")),
"AES"
)
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec)
return String(cipher.doFinal(textBytes), Charsets.UTF_8)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment