Scala / Play / REPL:配置错误(play.api.libs.Crypto)

时间:2013-11-26 19:29:15

标签: java scala encryption playframework playframework-2.0

我不明白,为什么我在Scala Play REPL中收到此错误,但Play项目似乎一切正常。这是在我运行play console

之后
scala> import play.api.libs.Crypto
import play.api.libs.Crypto

scala> Crypto.encryptAES("password")
@6gd0e7jca: Configuration error
    at play.api.libs.Crypto$$anonfun$encryptAES$2.apply(Crypto.scala:156)
    at play.api.libs.Crypto$$anonfun$encryptAES$2.apply(Crypto.scala:156)
    at scala.Option.getOrElse(Option.scala:120)
    at play.api.libs.Crypto$.encryptAES(Crypto.scala:155)
    at .<init>(<console>:19)
    at .<clinit>(<console>)
    at .<init>(<console>:7)
    at .<clinit>(<console>)
    at $print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
    at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
    at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
    at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
    at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
    at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
    at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
    at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
    at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
    at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
    at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:889)
    at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
    at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77)
    at sbt.Console.sbt$Console$$console0$1(Console.scala:23)
    at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
    at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
    at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)

我想用它来加密控制台中的字符串。这个错误究竟意味着什么?

Play Framework 2.2.1 / Scala 2.10.3 / Java 8 64bit

1 个答案:

答案 0 :(得分:2)

您需要一个正在运行的Play应用程序来使用该方法加密任何内容,因为它需要使用应用程序机密来加密数据。

两种解决方案:

  1. 使用encryptKey的其他版本,并传递您的应用程序密码,例如:

    Crypto.encryptAES("password", "yourapplicationsecret".substring(0, 16))
    
  2. 不是运行play console,而是运行play test:console,然后执行以下代码以确保正在运行的应用程序:

    import play.api.test._
    Helpers.running(FakeApplication()) { Crypto.encryptAES("password") }