Monday, 21 May 2018

in android java.security.InvalidKeyException: Illegal key size or default parameters




just follow up my previous post :  http://baiduhix.blogspot.com/2018/05/android-cipher-aes-256-cbc.html


when you try to use cipher in android to encrypt, you may get below error:

java.security.InvalidKeyException: Illegal key size or default parameters


this is because you are trying to do 256 encryption which default java doesn't support. you will need to change your jce:


For java8 user, you can download from here: (other java version please find your jce accordingly):

http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

then replace “local_policy.jar ”“US_export_policy.jar” into your jre lib.

if you are using mac your jre is here:


/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/

then you need to put “local_policy.jar ”“US_export_policy.jar” into:


/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/security



However, this doen't help. you still get the same error.

that because your android studio is not using this jre, it has a build in jre which is here:


/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/security

so replace “local_policy.jar ”“US_export_policy.jar” here.

Unfortunately , you still cannot run this code. you need to change your gradle:

testOptions { 
    unitTests.returnDefaultValues = true
  }

after that you can run the code. but you will get:  null for result. 

That because you are trying to run it in your android studio with junit. these code works on android.

No comments:

Post a comment