Showing posts with label encoding. Show all posts
Showing posts with label encoding. Show all posts

Monday, December 21, 2015

Struts 1.2 in WebLogic 12.1.3, german characters encoding issue

"As of WebLogic Server 12.1.3, the default value of the encoding element for the jsp-descriptor element in weblogic.xml is UTF-8 for JSP pages. Prior to WebLogic Server 12.1.3, the default value for JSP encoding was ISO-8859-1."
If you have issues
1) Add <%@ page contentType="text/html;charset=UTF-8" %> line on top of every jsp page. 

 2) Add 
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 </properties>

 In pom.xml  

 3) Add 
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <mime-mapping>
 <extension>jsp</extension>
 <mime-type>text/html;charset=UTF-8</mime-type>
 </mime-mapping>
 In web.xml 

 4) Add 
 <jsp-config>
 <jsp-property-group > 
 <url-pattern>*.jsp</url-pattern>
 <page-encoding>UTF-8</page-encoding>
 </jsp-property-group>
 </jsp-config> in web.xml – 

5) Add <constant name="struts.i18n.encoding" value="UTF-8"/> In struts-config.xml 


 6) change text encoding to UTF-8 in the properties file. 

7) Add the below to your weblogic.xml.

<charset-params>
 <input-charset>
   <resource-path>/*</resource-path>
   <java-charset-name>ISO8859-1</java-charset-name>
 </input-charset>
</charset-params> 


(see oracle Support "Default JSP Encoding Changed to UTF-8 (Doc ID 2053210.1)" )

(it should be ISO8859-1, not ISO8859_1)

Also, to determine which encoding you are actually using, check in your html for "<meta http-equiv="content-type" content="text/html; charset=utf-8" /> " . And in the browser (IE in my case) you have a menu "view-encoding"...



Friday, December 18, 2015

Umlaut in emails

If in an email you see f�r instead of für, chances are that your weblogic/JavaMail is not using the right encoding (presumably UTF-8)

You can try several options:

-Dfile.encoding=UTF-8
change the template to use escaped (HTML) charachers
see https://issues.apache.org/jira/browse/GERONIMO-5022 :
message.setSubject(subject,"UTF-8");
message.setText(text, "UTF-8");
properties.setProperty("mail.mime.charset","UTF8");






Tuesday, April 9, 2013

Encoding and decoding in Java (encryption) and WLST

Faced with the need to encrypt passwords in a DB, I google and find this article, but then I realize it's not recommended (why?) to use sun.misc.BASE64Decoder, I should rather use Apache commons:

commons-codec-1.4.jar (in reality the latest release is not 1.4, but I don't care)

maven is:
<dependency>
 <groupId>commons-codec</groupId>
 <artifactId>commons-codec</artifactId>
 <version>1.4</version>
</dependency>
So I change implementation and I get this wonderful code:

package com.acme.osb.encryption;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import org.apache.commons.codec.binary.Base64;


public class PasswordEncoder {

    private static final char[] PASSWORD = "enfldsgbnlsngdlksdsgm".toCharArray();
    private static final byte[] SALT = {
        (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,
        (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,
    };

    public static void main(String[] args) throws Exception {
        String originalPassword = "secret";
        System.out.println("Original password: " + originalPassword);
        String encryptedPassword = encrypt(originalPassword, PASSWORD);
        System.out.println("Encrypted password: " + encryptedPassword);
        String decryptedPassword = decrypt(encryptedPassword, PASSWORD);
        System.out.println("Decrypted password: " + decryptedPassword);
    }

    private static String encrypt(String property, char[] pw) throws GeneralSecurityException, UnsupportedEncodingException {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(pw));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
    }

    private static String base64Encode(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

    private static String decrypt(String property, char[] pw) throws GeneralSecurityException, IOException {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(pw));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8");
    }

    private static byte[] base64Decode(String property) throws IOException {
        return Base64.decodeBase64(property);
    }

}




In WebLogic there is a simple way of encrypting/decrypting passwords, but for fear of depending from the SerializedSystemIni.dat file I am afraid of using it.
Here the official documentation.
Anyway just run . ./setDomainEnv.sh and then "java weblogic.security.Encrypt mypwtoencrypt". To decrypt, since WebLogic doesn't provide a "weblogic.security.Decrypt" utility, follow these steps

In WLST, to decode you can do the following:


from javax.crypto import Cipher
from javax.crypto import SecretKey
from javax.crypto import SecretKeyFactory
from javax.crypto.spec import PBEKeySpec
from javax.crypto.spec import PBEParameterSpec

from org.apache.commons.codec.binary import Base64


def decode(encodedPassword):
    SALT='\xde\x33\x10\x12\xde\x33\x10\x12'
    key='yoursecretkeyhere'
    keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES")
    key = keyFactory.generateSecret(PBEKeySpec(key))
    pbeCipher = Cipher.getInstance("PBEWithMD5AndDES")
    
    pbeCipher.init(Cipher.DECRYPT_MODE, key, PBEParameterSpec(SALT, 20))
    
    resultb=pbeCipher.doFinal(Base64.decodeBase64(encodedPassword))
    
    print String(resultb, "utf-8")







Thursday, July 7, 2011

OSB encoding in XQuery

All the XQuery files in $domain/osb/config/core are stored in UFT-8 encoding:

?xml version="1.0" encoding="UFT-8"?

When you edit the XQuery through the OSB console, the application will take care of encoding weird characters (â, é, ü...) into a sequence of bytes (3 bytes, I guess).

If you have to edit manually the XQuery, and deploy it directly, YOU are responsible to encode the file.

The simplest option is to use Notepad++ and do Encode/Convert to UTF-8

If you deploy the XQuery with improper encoding, the server will not even start and throw horrible UTF8Decoder exceptions (!!!!).


Another alternative for encoding could be using the "file" Linux utility, but I haven't tested this.

http://mindspill.net/computing/linux-notes/determine-and-change-file-character-encoding.html