Groovy web console

subscribe to the feed Subscribe
to this
site

Ideatolife-Zain

Published 5 months ago by Shuja with tags aes-encryptioj
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
import java.security.spec.AlgorithmParameterSpec; 

String encrypt(String strToEncrypt) {
 
String encryptionKey = "12345678123456781234567812345678"//Paste your key here
Key aesKey = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES")
AlgorithmParameterSpec ivSpec = new IvParameterSpec(new byte[16]); 
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