Groovy web console

subscribe to the feed Subscribe
to this

Ideatolife-Zain-AES Encryption

Published 5 months ago by Shuja with tags aes-encryption
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

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