1

I am getting an error I can't explain.

public static String encrypt(String plain, SaltPasswordPair saltPassword) {
    String password = saltPassword.getPassword();
    String salt = saltPassword.getSalt();

    TextEncryptor encryptor = Encryptors.text(password, salt);
    String encrypted = encryptor.encrypt(plain);

    return encrypted;
}

This is my code. No matter what password length (4, 16, any other) or salt length or whatever value I pass (I also tried 1:1 copy&paste from the examples from here[Stackoverflow] and a few other pages), I get this exception:

java.lang.IllegalArgumentException: Unable to initialize due to invalid secret key
at org.springframework.security.crypto.encrypt.CipherUtils.initCipher(CipherUtils.java:120)
at org.springframework.security.crypto.encrypt.AesBytesEncryptor.encrypt(AesBytesEncryptor.java:115)
at org.springframework.security.crypto.encrypt.HexEncodingTextEncryptor.encrypt(HexEncodingTextEncryptor.java:36)
at de.thyaris.hydrogen.core.sec.Security.encrypt(Security.java:32)
at de.thyaris.hydrogen.core.sec.SecurityTest.testCryption(SecurityTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Caused by: java.security.InvalidKeyException: Illegal key size
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1039)
at javax.crypto.Cipher.implInit(Cipher.java:805)
at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
at javax.crypto.Cipher.init(Cipher.java:1396)
at javax.crypto.Cipher.init(Cipher.java:1327)
at org.springframework.security.crypto.encrypt.CipherUtils.initCipher(CipherUtils.java:113)
... 27 more

Am I missing something? Crypto version is 4.2.2.RELEASE

EDIT: Is there a way to fix this without adding external JARs to my JDK directory?

tsatke
  • 161
  • 1
  • 19
  • maybe that ? http://stackoverflow.com/questions/12619986/what-is-the-correct-way-to-configure-a-spring-textencryptor-for-use-on-heroku – Maxime Leprince Mar 29 '17 at 23:23
  • Thank you, but is there a way to do this without adding jar files to my JDK directory? I use CI/CD with docker, do you know images that provide these jar files in the JDK directory (I also need maven 3 and the jdk 8)? And do I need these JARs to run the deployed application? – tsatke Mar 30 '17 at 07:05

1 Answers1

0

private String cryptKey = "qkjll5@2md3gs5Q@FDFqf";

By default Java supports only 128-bit encryption (128bits == 16Bytes == 16 Chars)

Removing the 128-bit key restriction in Java

Lyncean Patel
  • 2,433
  • 16
  • 15
  • Please read the comments below my question and my question at all (especially the part after *EDIT:*) – tsatke Mar 30 '17 at 07:29