AES JSF encryption

Asked

Viewed 140 times

1

I have a problem with Seguinte, I have an AES encryption algorithm. When I run it by main it works, but when I run it on a jsf page and decrypt the text it returns several ???? when it contains Special Cracker in String. I’m using the Primefaces 6 inputText to display the return on the 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>
  • configure the encoding, eg: Data.getBytes("UTF-8") in key too, can do, generateKey(new String(pKey, ""UTF-8))

  • @Dilneicunha worked right, thanks. If Voce want to play as an answer, because I will mark as the answer that solved my problem. :)

1 answer

3


you need to configure the encoding, follow how to do:

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

hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.