AES JSF Encryption

1

I have the following problem, I have an AES encryption algorithm. When I run it through main it works, but when I run it on a jsf page and decrypt the text it returns several ????? when it contains special character in the String. I'm using the inputText of Primefaces 6 to display the return on screen.

 public  String encrypt(String Data, String pKey) throws Exception 
  {
    Key key = generateKey(pKey);
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    String encryptedValue = new BASE64Encoder().encode(encVal);
    return encryptedValue;
  }

    public String decrypt(String encryptedData, String pKey) throws Exception 
    {
        Key key = generateKey(pKey);
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

and below the xhtml page

<p:outputLabel value="Original Text: "/>
            <p:inputText value="#{criptografiaCtr.texto}"/>
            <p:outputLabel value="Key 16 Bits: "/>
            <p:inputText value="#{criptografiaCtr.chave}"/>
            <p:outputLabel value="Encrypted Text : "/>
            <p:inputText id="textoCri" value="#{criptografiaCtr.textoCriptografado}"/>
            <p:outputLabel value="Decrypted Text: "/>
            <p:inputText id="textoDes" value="#{criptografiaCtr.textoDesCriptografado}"/>
          </p:panelGrid>
          <p:commandButton value="Encryption" >
            <p:ajax event="click" listener="#{criptografiaCtr.criptografar()}" update="textoCri" process="@form"/>
          </p:commandButton> 
          <h:commandButton value="Decrypted">
            <f:ajax event="click" listener="#{criptografiaCtr.descriptografa()}" execute="@form" render="textoDes"/>
          </h:commandButton>
    
asked by anonymous 23.09.2016 / 17:32

1 answer

3

You need to configure the encoding, here's how to do it:

Data.getBytes("UTF-8");
generateKey(new String(pKey, ""UTF-8));

Hugs.

    
23.09.2016 / 19:14