2015-09-03 14 views
2

我已经在使用JDK8的xws-security(EncryptionProcessor.java)中针对其他系统成功实施了对GCM加密的支持。不过,我解密有问题。第一个问题如下 java.security.InvalidAlgorithmParameterException:不支持的参数:javax.crypto.spec.IvParameterSpec。我通过从IvParameterSpec()来GCMParameterSpec()如下(从DecryptionProcessor.java的代码片段)xws-security(webservices-rt)中的GCM加密和解密

  try { 
     String dataAlgorithm = JCEMapper.translateURItoJCEID(tmp); 
     decryptor = Cipher.getInstance(dataAlgorithm); 

     //decryptor = Cipher.getInstance("DESede/CBC/ISO10126Padding"); 

     int ivLen = decryptor.getBlockSize(); 
     byte[] ivBytes = new byte[ivLen]; 

     System.arraycopy(cipherInput, 0, ivBytes, 0, ivLen); 
     if (dataAlgorithm.matches(".*[gG][cC][mM].*$")) { // TK 03/09/2015 - probably needs more places for decrypting body stuff 
      GCMParameterSpec iv = new GCMParameterSpec(ivLen * Byte.SIZE, ivBytes); 
      decryptor.init(Cipher.DECRYPT_MODE, symmetricKey, iv); 
     } 
     else { 
      IvParameterSpec iv = new IvParameterSpec(ivBytes); 
      decryptor.init(Cipher.DECRYPT_MODE, symmetricKey, iv); <===== old line 761 
     } 

     cipherOutput = decryptor.doFinal(cipherInput, ivLen, cipherInput.length-ivLen); 
     } catch (Exception e) { 
     log.log(Level.SEVERE, "WSS1232.failedto.decrypt.attachment", e); 
     throw new XWSSecurityException(e); 
     } 

我现在用下面的错误上调用doFinal最终改变初始化向量(IV)解决了这个问题()

javax.crypto.AEADBadTagException: Tag mismatch! 
    at com.sun.crypto.provider.GaloisCounterMode.decryptFinal(GaloisCounterMode.java:524) 
    at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1023) 
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:960) 
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824) 
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436) 
    at javax.crypto.Cipher.doFinal(Cipher.java:2223) 
    at com.sun.xml.wss.impl.apachecrypto.DecryptionProcessor.decryptAttachment(DecryptionProcessor.java:775) 

任何意见/在此建议,将不胜感激

+0

我不知道XWS安全,但谷歌表示它是基于XML安全(这里是加密)和http://www.w3.org/TR/xmlenc-core1/#sec-AES-GCM说** xmlenc使用96位(12字节)IV **和128位标签。这可能不是巧合,这是SP800-38D的首选尺寸。这个标签大小恰好与AES数据块相同,但并未因此选择它。 –

回答

0

固定解密SWA附件 - 感谢dave_thompson_085的提示。代码调整如下

 try { 
     String dataAlgorithm = JCEMapper.translateURItoJCEID(tmp); 
     decryptor = Cipher.getInstance(dataAlgorithm); 

     //decryptor = Cipher.getInstance("DESede/CBC/ISO10126Padding"); 

     int ivLen = decryptor.getBlockSize(); 
     byte[] ivBytes = null; // = new byte[ivLen]; 

     if (dataAlgorithm.matches(".*[gG][cC][mM].*$")) { // TK 03/09/2015 - probably needs more places for decrypting body stuff 
      ivLen = 12; // 12 for GCM - also see wss4j-2.1.2/ws-security-common/src/main/java/org/apache/wss4j/common/util/AttachmentUtils.java 
      ivBytes = new byte[ivLen]; 
      System.arraycopy(cipherInput, 0, ivBytes, 0, ivLen); 
      GCMParameterSpec iv = new GCMParameterSpec(16 * Byte.SIZE, ivBytes); 
      decryptor.init(Cipher.DECRYPT_MODE, symmetricKey, iv); 
     } 
     else { 
      ivBytes = new byte[ivLen]; 
      System.arraycopy(cipherInput, 0, ivBytes, 0, ivLen); 
      IvParameterSpec iv = new IvParameterSpec(ivBytes); 
      decryptor.init(Cipher.DECRYPT_MODE, symmetricKey, iv); 
     } 

     cipherOutput = decryptor.doFinal(cipherInput, ivLen, cipherInput.length-ivLen); 
    } catch (Exception e) { 
     log.log(Level.SEVERE, "WSS1232.failedto.decrypt.attachment", e); 
     throw new XWSSecurityException(e); 
    } 

现在有一个与GCM XML元素解密类似的问题。稍后会跟进。