Groovy web console

subscribe to the feed Subscribe
to this
site
AES ecb encryption (via #groovywebconsole)
tweet this snippet Tweet
this
script

AES ecb encryption

Published 8 months ago by Yash with tags aes
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import javax.crypto.Cipher
import javax.crypto.SecretKey
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.PBEKeySpec
import javax.crypto.spec.SecretKeySpec
import java.security.Key
import java.security.spec.KeySpec


String encrypt(String strToEncrypt) {

    String encryptionKey = "1234567812345678"//Paste your key here
    Key aesKey = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES")
    if (!strToEncrypt) return strToEncrypt
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
    cipher.init(Cipher.ENCRYPT_MODE, aesKey)
    String encryptedStr = cipher.doFinal(strToEncrypt.getBytes("UTF-8")).encodeBase64()
    return encryptedStr
}

encrypt("abcd") //Your text here