2013-01-23 84 views
1

我們加密/用C#應用程序
的Java decrytpion&C#加密非法密鑰大小問題

var msData = new MemoryStream(); 
      CryptoStream cs = null; 
      if (inputFileStream.CanSeek) 
       inputFileStream.Seek(0, SeekOrigin.Begin); 
      try 
      { 
       long inputFileLength = inputFileStream.Length; 
       var byteBuffer = new byte[4096]; 
       long bytesProcessed = 0; 
       int bytesInCurrentBlock = 0; 

       var csRijndael = new RijndaelManaged(); 
       switch (action) 
       { 
        case CryptoAction.Encrypt: 
         cs = new CryptoStream(msData, csRijndael.CreateEncryptor(this.Key, this.IV), CryptoStreamMode.Write); 
         break; 

        case CryptoAction.Decrypt: 
         cs = new CryptoStream(msData, csRijndael.CreateDecryptor(this.Key, this.IV), CryptoStreamMode.Write); 
         break; 
       } 

       while (bytesProcessed < inputFileLength) 
       { 
        bytesInCurrentBlock = inputFileStream.Read(byteBuffer, 0, 4096); 
        cs.Write(byteBuffer, 0, bytesInCurrentBlock); 
        bytesProcessed += bytesInCurrentBlock; 
       } 
       cs.FlushFinalBlock(); 

       return msData.ToArray(); 
      } 
      catch(Exception ex) 
      { 
       new Com.Library.SqlLog().InsertSqlLog(Auth.currentMemberInfo.LoginId, MethodInfo.GetCurrentMethod().Name, ex); 
       return null; 
      } 

,它是工作fine.Now我創建一個Java應用程序,它是從這個代碼解密我們組織的文檔應該too.Initially解密這些文件我試圖通過這個代碼的Java

try{ 


    AlgorithmParameterSpec paramSpec = new IvParameterSpec(IV); 
    Cipher cipher = Cipher.getInstance("AES"); 
    SecretKeySpec key = new SecretKeySpec(keyGen, "AES"); 
    cipher.init(Cipher.DECRYPT_MODE, key, paramSpec); 
    byte[] output = new BASE64Decoder().decodeBuffer(new String(convertDocToByteArra("//Path/somePDF.pdf"))); 

    byte[] decrypted = cipher.doFinal(output); 

    convertByteArrayToDoc(decrypted); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

現在這裏來解密文件是我的疑難問題。通過這個作爲初始化向量

static byte IV[] = new byte[] {0x0E,0x15,(byte)0xC4,(byte)0xBB, 
      (byte)0xEE,(byte)0xDF,0x72,0x0C,0x4F,0x7E,(byte)0xBE,0x67,0x4A,(byte)0xB6,0x45,(byte)0xDE}; 

並將此作爲一個重要

static byte[] keyGen = new byte[]{0x78,(byte)0x9C,(byte)0xF1,0x01,0x12,0x31,(byte)0xCD,0x1E,0x1F,0x16,0x54,0x19,0x1D,(byte)0xFF,(byte)0xC7, 
      0x00,0x51,(byte)0xBF,(byte)0xFD,0x31,(byte)0xE1,(byte)0xA1,(byte)0xDC,(byte)0xC9,0x23,0x49,(byte)0xAD,0x11,0x16,0x17,0x1D,0x1F}; 


但是當我運行這個程序,然後將它拋出以下

java.security.InvalidKeyException: Illegal key size 
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1023) 
    at javax.crypto.Cipher.implInit(Cipher.java:789) 
    at javax.crypto.Cipher.chooseProvider(Cipher.java:848) 
    at javax.crypto.Cipher.init(Cipher.java:1347) 
    at javax.crypto.Cipher.init(Cipher.java:1281) 
    at DecryptAspEncryptedDocs.main(DecryptAspEncryptedDocs.java:80) 

現在給出一個例外,我堅持在這裏做不知道該怎麼做。任何幫助將不勝感激。

編輯1:
我見過this question和罐子放在JRE/lib/security中和JDK/lib目錄/ security.But仍然問題掛起並拋出illegal key size異常

+0

適用於我(當我將模式更改爲CBC時)。 –

+0

@GregS我提供了AES/CBC/NoPadding,但它再次說「非法密鑰大小」 – Despicable

+0

正如我所說,它適用於我,我知道我已經安裝了無限制的加密策略。儘管有你的信仰,但你似乎沒有。也許您安裝了多個Java版本,或者您錯誤地安裝了策略文件。 –

回答

1

取決於密鑰大小對於AES,那麼它將受到出口控制。我建議你下載無限制的jce權限策略文件並將它們安裝到你的Java環境中。

AES256將要求無限制的策略文件。

+0

是的,我通過檢查這個問題做了這件事http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters – Despicable

0

在README中沒有明確指出「JCE Unlimited Strength Jurisdiction Policy Files」應該被複制到JDK中的jre,否則它將無法工作。文件路徑應爲:/path/to/jdk1.7.0_xx/jre/lib/security