Created
April 18, 2020 07:07
-
-
Save jisungbin/dbe0a40c940a940d738b594e4dddffd9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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