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);
        }
    }
}



4 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. I appreciate the simple and effective details shared here. Read this profile for extra knowledge Coreball. Coreball Game Online is a straightforward yet challenging game.

    ReplyDelete
  3. Looking for expert accounting assignment help? Our qualified writers provide accurate, plagiarism-free solutions in financial accounting, management accounting, cost accounting, auditing, and taxation. We deliver step-by-step calculations, clear explanations, and well-structured assignments tailored to your academic needs. Get professional guidance to boost your grades and meet deadlines with ease. Contact us today at +61 48900136 for reliable and affordable accounting help!

    ReplyDelete
  4. Your explanation really helped me understand better, thank you. Read this profile to explore more Dinosaur Game. The Dinosaur Game is endless, so you keep trying to beat your score.

    ReplyDelete