6

I have a couple of library, C#, PHP and Android where they all encrypt/decrypt a string in the same way so they are all compatible with each other, i.e. C# writes and encrypts data to a database and PHP can successfully decrypt it and return the original string.

I now need to do the same thing with a standard Java application, so I've taken the code from my Android library and need libraries but I am getting an exception. As far as I know the code wasn't Android specific so it shouldn't be a problem.

Below is my encryption function

public static String encrypt(String plainPasword)
    {
            String password = "";
            try
            {
                SecretKeySpec key = new SecretKeySpec("hcxilkqbbhczfeultgbskdmaunivmfuo".getBytes("US-ASCII"), "AES");
                IvParameterSpec iv = new IvParameterSpec("ryojvlzmdalyglrj".getBytes("US-ASCII"));

                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

                cipher.init(Cipher.ENCRYPT_MODE, key, iv);

                byte[] encoded = cipher.doFinal(plainPasword.getBytes());
                password = new String(Base64.encodeBase64(encoded));

            }
            catch (Exception ex)
            {
                System.err.println("Encryption Exception: " + ex.toString());
            }
            return password;
    }

When I call Encryption.encrypt("myString") I get the following exception:

Encryption Exception: java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding

As I said this code is working fine on Android and it shouldn't make any difference where it is running from.

Update

I found that I needed PKCS5Padding instead of 7 thanks to a link on a comment. I am now though getting the following exception:

Encryption Exception: java.security.InvalidKeyException: Illegal key size
halfer
  • 19,824
  • 17
  • 99
  • 186
Boardy
  • 35,417
  • 104
  • 256
  • 447
  • 1
    http://stackoverflow.com/questions/10193567/java-security-nosuchalgorithmexception-cannot-find-any-provider-supporting-aes – srkavin Sep 19 '14 at 20:50
  • Thanks when I googled I didn't find anything although now that I've changed it is now telling me that the key is an invalid size – Boardy Sep 19 '14 at 20:54
  • You will need Unlimited strength JCE policy files. http://deveshsharma.info/2012/10/09/fixing-java-security-invalidkeyexception-illegal-key-size-exception/ – srkavin Sep 19 '14 at 20:59
  • Tried that but still getting the same error – Boardy Sep 19 '14 at 21:05
  • 1
    PKCS#7 padding is a super set of PKCS#5 padding and is many times refered to when PKCS#7 padding it what is needed. Essentially PKCS#5 is only defined for a block size of 8-bytes and PKCS#7 just extends the definition to 255-bytes. Additionally if would be very uncommon to fine an implementation of PKCS#5 that did not also support PKCS#7, in practice the two can be used interchangeably.. See [PKCS#7 padding](https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7). – zaph Sep 01 '16 at 21:53

1 Answers1

19

First, in Java, the standard padding name is PKCS5Padding, not PKCS7Padding. Java is actually performing PKCS #7 padding, but in the JCA specification, PKCS5Padding is the name given.

Next, you are trying to use AES-256, so you'll need to install the Unlimited Strength Jurisdiction policy files.

Hopefully this is just an example and you aren't using the same IV for every message, right?

erickson
  • 265,237
  • 58
  • 395
  • 493
  • It is just an example, I have copied the policies but I am still getting the same exceptin – Boardy Sep 19 '14 at 21:09
  • You probably got the wrong version or put them in the wrong place. You'll need to provide more detail or just carefully review what you did. – erickson Sep 19 '14 at 21:14
  • 1
    I found the problem. I had copied the files in to Program Files\Java\jre\... but I was using a jre bundled inside the JDK directory. I copied the files into this and its all working now. Thanks for your help. Its a bit strange though to me why Android doesn't have any of these problems, especially as I am using the same library – Boardy Sep 19 '14 at 21:59
  • 4
    You are *not* using the same library. Android implements the Java SE API, but does not use the same source code, not the same provider and the AES restrictions are not implemented either. – Maarten Bodewes Sep 21 '14 at 13:47