java解密和; c#encryption非法密钥大小问题

时间:2013-01-23 04:51:57

标签: c# java cryptography aes

我们通过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;
            }

它工作正常。现在我正在创建一个应该解密这些文件的Java应用程序。最初我试图用这个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并且jars被放置在jre / lib / security和jdk / lib / security中。但是仍有问题正在等待并抛出illegal key size异常

2 个答案:

答案 0 :(得分:1)

根据AES的密钥大小,它将受出口控制的约束。我建议您下载无限制的jce管辖区域策略文件并将其安装到Java环境中。

AES256将要求无限制的策略文件。

答案 1 :(得分:0)

在README中没有明确说明&#34; JCE Unlimited Strength Jurisdiction Policy Files&#34;应该被复制到JDK中的jre,否则它将无法正常工作。文件路径应为:/path/to/jdk1.7.0_xx/jre/lib/security