Despicable
Despicable

Reputation: 3957

java decrytpion & c# encryption Illegal key size issue

We were encrypting/decrypting our organization's document from this code by a c# application

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;
            }

and it is working fine.Now I am creating a Java application which is supposed to decrypt these documents too.Initially I tried to decrypt documents by this code of 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();
       }

Now here is my problem.I passed this as init vector

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};

and this as a key

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};


But when I run this program then it throws an exception given below

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)

Now I am stuck here and don't know what to do.Any help would be greatly appreciated.

EDIT 1:
I have seen this question and jars are placed in jre/lib/security and jdk/lib/security.But still issue is pending and throwing illegal key size exception

Upvotes: 1

Views: 592

Answers (2)

ruruskyi
ruruskyi

Reputation: 2087

It's not stated clearly in README that "JCE Unlimited Strength Jurisdiction Policy Files" should be copied to jre inside your JDK, otherwise it would not work. Path for files should be: /path/to/jdk1.7.0_xx/jre/lib/security

Upvotes: 0

quotidian-ennui
quotidian-ennui

Reputation: 126

Depending on the key size for AES then it will be subject to export controls. I suggest that you download the unlimited jce jurisdiction policy files and install them into your Java environment.

AES256 will require the unlimited the policy files.

Upvotes: 1

Related Questions