为什么org.jasypt.exceptions.EncryptionOperationNotPossibleException

时间:2017-02-18 06:24:19

标签: java servlets encryption jasypt

以下是我的Encryptor类方法:

    public String encrypt(String str){ 
        if(str == null || str.equals("")){
            str = "";
        }
        else{
            StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
            encryptor.setPassword(mpCryptoPassword);
            str = encryptor.encrypt(str);
        } 
    return str;
    }
    public String decrypt(String str){
        if(str == null || str.equals("")){
            str = "";
        }
        else{
            StandardPBEStringEncryptor decryptor = new StandardPBEStringEncryptor();
            decryptor.setPassword(mpCryptoPassword);
            str = decryptor.decrypt(str);
        }
    return str;
    }

我正在尝试加密如下:

String encryptedToken   = "";
//preparing token email + time
Date now            = new Date();               
String prepareToken = "email="+email+"&tokenTime="+now.getTime();

//encrypt url value
Encryptor enc       = new Encryptor();
encryptedToken      = enc.encrypt(prepareToken);

我正在解密:

Encryptor enc           = new Encryptor();
String decryptedToken   = enc.decrypt(encryptedToken);

然后它的投掷错误:

  

org.jasypt.exceptions.EncryptionOperationNotPossibleException           at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.decrypt(StandardPBEByteEncryptor.java:1055)           at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:725)           在com.cryptography.Encryptor.decrypt(Encryptor.java:57)           在com.users.forgot.ResetPassword.doGet(ResetPassword.java:72)           在javax.servlet.http.HttpServlet.service(HttpServlet.java:622)           在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)           在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)           在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)           at filters.charsetFilter.filter.CharsetFilter.doFilter(CharsetFilter.java:61)           在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)           在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)           在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)           在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)           在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)           在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)           在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)           在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:583)           在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:447)           在java.lang.Thread.run(Thread.java:744)

更新
我注意到它的工作有时但是当加密字符串中有+符号作为其获取+作为空间

时不工作

1 个答案:

答案 0 :(得分:0)

我发现的问题是加号字符串加号+。当我解密时,它从URL获取+作为空白区域。

encryptedToken = encryptedToken.replaceAll(" ","+");

解决这个问题