生成签名和加密的JWT

时间:2016-10-18 09:28:21

标签: java encryption aes jwt hmac

我正在尝试使用Nimbus JWT生成已签名和加密的JWT令牌。

private void generateToken() throws JOSEException, NoSuchAlgorithmException, UnsupportedEncodingException {
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(256);
    SecretKey secretKey = keyGen.generateKey();

    JWSSigner signer = new MACSigner(secretKey);
    JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().subject("subject").build();

    SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);
    signedJWT.sign(signer);

    JWEObject jweObject = new JWEObject(
            new JWEHeader.Builder(JWEAlgorithm.DIR, EncryptionMethod.A256GCM).contentType("JWT").build(),
            new Payload("hello world")
    );
    jweObject.encrypt(new DirectEncrypter(secretKey));
}

运行代码时,我收到以下错误消息

com.nimbusds.jose.JOSEException: Couldn't create AES/GCM/NoPadding cipher: Illegal key size
    at com.nimbusds.jose.crypto.AESGCM.encrypt(AESGCM.java:123)
    at com.nimbusds.jose.crypto.ContentCryptoProvider.encrypt(ContentCryptoProvider.java:187)
    at com.nimbusds.jose.crypto.DirectEncrypter.encrypt(DirectEncrypter.java:141)
    at com.nimbusds.jose.JWEObject.encrypt(JWEObject.java:370)
    at de.example.generateToken(TokenImpl.java:108)
    at de.example.TokenImpl.<init>(TokenImpl.java:68)
    at de.example.TokenTest.create(TokenTest.java:33)
    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:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    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:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.security.InvalidKeyException: Illegal key size
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1039)
    at javax.crypto.Cipher.implInit(Cipher.java:805)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
    at javax.crypto.Cipher.init(Cipher.java:1396)
    at javax.crypto.Cipher.init(Cipher.java:1327)
    at com.nimbusds.jose.crypto.AESGCM.encrypt(AESGCM.java:119)

生成的密钥虽然是256位AES密钥,但我真的没有弄错。来自雨云的example也是如此。我在这里想念一下吗?

1 个答案:

答案 0 :(得分:4)

就像用户“leleuj”在https://github.com/pac4j/pac4j/issues/355上所说,你需要:“Java密码学扩展(JCE)无限强度管辖权政策文件”,如果你还没有安装它们。 你需要:

  1. 下载已安装java的版本(v7为jce-7,v8为jce-8
  2. 解压缩
  3. 停止运行任何java进程
  4. 备份local_policy.jar和US_export_policy.jar(两者都在[java_home] / jre / lib / security中)
  5. 在[java_home] / jre / lib / security
  6. 中复制新的