Is it possible to use the Grails Jasypt plugin outside the GORM layer for simple String encryption and decryption?

StackOverflow https://stackoverflow.com/questions/19192117

  •  30-06-2022
  •  | 
  •  

Question

I use the excellent JASYPT plugin to encrypt and decrypt certain database columns. Works great. But I have a usecase for encryption/decryption for simple Strings that are not going to the database and I'd love to use my already set up Jasypt configuration with my secret and the digest to do it rather than bring in another plugin or crypto configuration, but it seems the documentation only show how use it for GORM and domain classes.

https://bitbucket.org/tednaleid/grails-jasypt/wiki/Home

Ideally I'd keep things really simple like this

String encrypted = myJasyptConfig().encrypt(myString)
//then later
String decrypted = myJasyptConfig().decrypt(encrypted)

Possible?

Was it helpful?

Solution

The plugin has jasypt dependencies and they are exported to app (plugin dependencies are transitively available to the app by default).

I think you can use the StandardPBEStringEncryptor as is based on your config.

Add the below method as an action in a sample controller (inject grailsApplication) of your app and hit it.

def standard(){
    def jasyptConfig = grailsApplication.config.jasypt
    org.jasypt.encryption.pbe.StandardPBEStringEncryptor stringEncryptor = 
           new org.jasypt.encryption.pbe.StandardPBEStringEncryptor(jasyptConfig)

    def encrypted = stringEncryptor.encrypt("Hello World")
    def decrypted = stringEncryptor.decrypt(encrypted)

    render([encrypted: encrypted, decrypted: decrypted] as JSON)
}

or just run the above method in grails console.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top