AEM 6 SP1 - Servlet for Encryption Decryption

Goal


A servlet to return encrypt/decrypt words

Package Install

Encryption Request: http://localhost:4502/bin/experienceaem/encrypt?words=experience,aem


Decryption Request: http://localhost:4502/bin/experienceaem/decrypt?words={10d97d9138c4bc55f321b1120c44ff1b0aa33f1f39aac42b9d487d06e581dd83},{cd1449b90a6277816a638f45665f604f1d32a56ceadfd1dde56f9254812d28e8}


Solution


Create and deploy EncryptDecryptServlet with following code

package apps.experienceaem;

import com.adobe.granite.crypto.CryptoSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.*;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import java.io.IOException;

@Component(label = "Experience AEM Encryption Decryption Servlet")
@Service
@Properties({
        @Property(name = "sling.servlet.methods", value = { "GET" }, propertyPrivate = true),
        @Property(name = "sling.servlet.paths", value = {
                "/bin/experienceaem/encrypt",
                "/bin/experienceaem/decrypt"
        })
})
public class EncryptDecryptServlet extends SlingAllMethodsServlet {
    private static final Logger LOG = LoggerFactory.getLogger(EncryptDecryptServlet.class);

    @Reference
    protected CryptoSupport cryptoSupport;

    @Override
    protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");

        String words = request.getParameter("words");
        String uri = request.getRequestURI();

        if(StringUtils.isEmpty(words)){
            throw new ServletException("Need words");
        }

        String[] wordsArr = words.split(",");

        try{
            JSONWriter jw = new JSONWriter(response.getWriter());

            jw.object();

            for(String word : wordsArr){
                jw.key(word).value(uri.endsWith("encrypt") ? cryptoSupport.protect(word)
                                                           : cryptoSupport.unprotect(word));
            }

            jw.endObject();
        }catch(Exception e){
            LOG.error("Error encrypting/decrypting", e);
        }
    }
}



2 comments:

  1. This is not working properly for me. I receive:

    Caused by: com.adobe.granite.crypto.CryptoException: Failed decrypting cipher text
    at com.adobe.granite.crypto.internal.CryptoSupportImpl.decrypt(CryptoSupportImpl.java:96)
    at com.adobe.granite.crypto.internal.CryptoSupportImpl.unprotect(CryptoSupportImpl.java:157)
    ... 110 common frames omitted
    Caused by: com.rsa.jsafe.JSAFE_PaddingException: Invalid padding.
    at com.rsa.jsafe.JSAFE_SymmetricCipher.decryptFinal(Unknown Source)
    at com.adobe.granite.crypto.internal.jsafe.JSafeCryptoSupport.getPlainText(JSafeCryptoSupport.java:325)
    at com.adobe.granite.crypto.internal.jsafe.JSafeCryptoSupport.getPlainText(JSafeCryptoSupport.java:307)
    at com.adobe.granite.crypto.internal.CryptoSupportImpl.decrypt(CryptoSupportImpl.java:94)
    ... 111 common frames omitted

    ReplyDelete
  2. Creating a servlet for encrypting and decrypting words is a practical tool for secure applications! For top-tier industrial solutions, check out the Industry Leading Degassing Solution.






    ReplyDelete