Created
November 29, 2019 07:36
-
-
Save PegasisForever/6e0ca23569f8f66120f2f5cd2a7aeb82 to your computer and use it in GitHub Desktop.
an AES utility
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 modes.server | |
import java.security.MessageDigest | |
import javax.crypto.Cipher | |
import javax.crypto.spec.SecretKeySpec | |
object AES { | |
private val encryptCipher: Cipher | |
private val decryptCipher: Cipher | |
init { | |
val seed = Config.AESSeed | |
val sha = MessageDigest.getInstance("SHA-256") | |
.digest(seed.toByteArray(charset("UTF-8"))) | |
val secretKey = SecretKeySpec(sha, "AES") | |
encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding") | |
encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey) | |
decryptCipher = Cipher.getInstance("AES/ECB/PKCS5PADDING") | |
decryptCipher.init(Cipher.DECRYPT_MODE, secretKey) | |
} | |
fun encrypt(toEncrypt: String) = encryptCipher.doFinal(toEncrypt.toByteArray()) | |
fun decrypt(toDecrypt: ByteArray) = String(decryptCipher.doFinal(toDecrypt)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment