首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在测试中调用IllegalArgumentException -security-crypto的加密器时

在测试中调用IllegalArgumentException -security-crypto的加密器时
EN

Stack Overflow用户
提问于 2019-06-13 06:57:53
回答 1查看 284关注 0票数 0

我正在尝试为下面的代码编写一个JUnit测试用例。我使用的是spring-security-crypto库的加密器。当我尝试运行代码时,我能够成功地加密字符串,但是当我运行测试用例时,我得到了一个错误。

代码

代码语言:javascript
复制
public String standardEncryption(String value) {
        if (!isNullOrEmpty(value)) {
            return Encryptors.text(password, salt).encrypt(value);
        }
        return value;
    }

Junit测试用例

代码语言:javascript
复制
@Test
    public void can_standardEncryption() {
        String value = someNumericString(10);

        String result = encryption.standardEncryption(value);

        assertThat(result).isNotNull();
        assertThat(result.equals(value)).isFalse();
        assertThat(textEncryptor.decrypt(result)).isEqualTo(value);
        assertThat(result.equals(textEncryptor.encrypt(value))).isFalse();
    }

错误

代码语言:javascript
复制
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 something(something.java:25)
    at something(something.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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)
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56571343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档