是否可以使用GORM层外面的Grails Jasypt插件进行简单的字符串加密和解密? [英] Is it possible to use the Grails Jasypt plugin outside the GORM layer for simple String encryption and decryption?

查看:362
本文介绍了是否可以使用GORM层外面的Grails Jasypt插件进行简单的字符串加密和解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用优秀的JASYPT插件来加密和解密某些数据库列。工程伟大。但我有一个usecase的加密/解密的简单的字符串,不去数据库,我想使用我已经设置Jasypt配置与我的秘密和摘要做,而不是带来另一个插件或加密配置,但似乎文档只显示如何使用它为GORM和域类。

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

d保持事情很简单像这样

Ideally I'd keep things really simple like this

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


推荐答案

插件具有jasypt依赖项,并导出到应用程序(默认情况下,插件依赖项传递到应用程序)。

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

我认为您可以使用 StandardPBEStringEncryptor 基于您的配置。

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

在示例控制器中添加以下方法作为操作(注入 grailsApplication )。

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)
}

或者只需在 console

这篇关于是否可以使用GORM层外面的Grails Jasypt插件进行简单的字符串加密和解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆