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

女生的网名这么多〃 提交于 2019-12-02 18:13:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/19192117/is-it-possible-to-use-the-grails-jasypt-plugin-outside-the-gorm-layer-for-simple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!