2016-10-18 18 views
1

Nimbus JWTを使用して、署名され暗号化された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も同じことをします。ここで何かが恋しい?

答えて

3

https://github.com/pac4j/pac4j/issues/355のユーザー「leleuj」と同様に、「Java Cryptography Extension(JCE)Unlimited Strength Jurisdiction Policy Files」が必要です。あなたは必要 :

  1. を実行している任意のJavaプロセスへは、あなたのlocal_policyのバックアップを作成停止
  2. それを解凍し
  3. (V8がjce-8あるためV7は、jce-7であるため)、インストールしたJavaのバージョンをダウンロードしてください.jarとしたUS_export_policy.jar(両方とも[JAVA_HOME]/JRE/libに/セキュリティである)
  4. [JAVA_HOME]/JRE/libに/セキュリティ
で新しいものをコピーします3210
関連する問題