【问题标题】:Encrypt Data in Play Session Cookie加密 Play Session Cookie 中的数据
【发布时间】:2014-02-21 00:17:46
【问题描述】:

我在 Play 中存储了一些小的用户数据! session cookie 以实现无状态服务器。有四个数据。出于安全目的,我想加密其中两个。我正在使用 Play Crypto 库。加密看起来像这样(假设 encryptKey 是一个有效的 16 字节字符串):

Redirect(successRedirectURL).withSession("id" -> (userProfile.get \ "_id").as[String],
                                         "name" -> Crypto.encryptAES(name, encryptKey),
                                         "imageUrl" -> { if (imageUrl.isEmpty) "" else imageUrl.get },
                                         "email" -> Crypto.encryptAES((userProfile.get \ "email").as[String]))

解密如下:

def getUserName[A](implicit request: SecuredRequest[A]): String = Crypto.decryptAES(request.session.get("name").get, encryptKey)
def getUserEmail[A](implicit request: SecuredRequest[A]): String = Crypto.decryptAES(request.session.get("email").get, encryptKey)

现在,用户名解密就好了。电子邮件没有。我直接输入了加密字符串以验证它不是会话机制并且​​我得到了相同的行为。我调用getUserEmail 时得到的堆栈跟踪是这样的:

play.api.Application$$anon$1: Execution exception[[BadPaddingException: Given final block not properly padded]]
    at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.1]
    at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$12$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:165) [play_2.10.jar:2.2.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$12$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:162) [play_2.10.jar:2.2.1]
    at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33) [scala-library.jar:na]
    at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:185) [scala-library.jar:na]
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811) ~[na:na]
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676) ~[na:na]
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313) ~[na:na]
    at javax.crypto.Cipher.doFinal(Cipher.java:2087) ~[na:1.7.0_10]
    at play.api.libs.Crypto$.decryptAES(Crypto.scala:229) ~[play_2.10.jar:2.2.1]
    at play.api.libs.Crypto.decryptAES(Crypto.scala) ~[play_2.10.jar:2.2.1]

我试图弄清楚如何将其设置为不进行填充,但我无法在我的 application.conf 中找出 application.crypto.providerapplication.crypto.aes.transformation 的值。有什么想法吗?

【问题讨论】:

    标签: scala cookies encryption playframework-2.0 session-cookies


    【解决方案1】:

    在您的加密中:

    Redirect(successRedirectURL).withSession("id" -> (userProfile.get \ "_id").as[String],
                                         "name" -> Crypto.encryptAES(name, encryptKey),
                                         "imageUrl" -> { if (imageUrl.isEmpty) "" else imageUrl.get },
                                         "email" -> Crypto.encryptAES((userProfile.get \ "email").as[String]))
    

    我发现 emailencryptAES 没有像 name 那样通过 encryptKey

    这是一个错字,还是与您的问题有关?

    试试:

    "email" -> Crypto.encryptAES((userProfile.get \ "email").as[String]), encryptKey)
    

    【讨论】:

      猜你喜欢
      • 2015-11-12
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多