SECTION 01 Codec 總覽
Java JDK有一個 java.security 的 package, 提供了 MessageDigest 的編碼方式, Digest Algorithms 包括了 MD2, MD5, SHA-1, SHA-256, SHA-384, 及 SHA-512 等等. 但是我們常會需要用到其它的編碼演算, 此時的解決方法, 可以先來看看 commons codec 是否已經加入標準, 不然就去一些學術單位尋找, 真的沒有, 就只好自己寫了.
目前 commons codec 已經有提供 base64, hex, 及 metaphone, soundex 等編碼. 下載位置為
codec 1.1 SECTION 02 Base64 編解碼
base64 編解碼技術廣泛地利用在 Internet 之上, 主要就是將 bytecode 轉換成 ascii 碼, 最常常使用的則是屬于電子郵件附件的傳輸. 你可以查看你的郵件原碼, 如果有 Content-Transfer-Encoding: base64 , 就代表這封信有采用 base64 來編碼, 另外則是 QP ( quoted-printable ) 編碼方式, 基本上大多數的讀信軟件都有支持.
package com.softleader.sample.codec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.*;
public class Base64Test {
public static void main(String args[]) {
Base64 base64 = new Base64();
String str = "中文";
byte[] enbytes = null;
String encodeStr = null;
byte[] debytes = null;
String decodeStr = null;
try {
enbytes = base64.encode(str.getBytes());
encodeStr = new String(enbytes);
debytes = base64.decode(enbytes);
decodeStr = new String(debytes);
} catch (EncoderException ex) {
System.out.println("編碼錯誤");
} catch (DecoderException ex) {
System.out.println("解碼錯誤");
}
System.out.println("編碼前:"+str);
System.out.println("編碼后:"+encodeStr);
System.out.println("解碼后:"+decodeStr);
}
}
cmd>java com.softleader.sample.codec.Base64Test
執行結果
編碼前:中文
編碼后:pKSk5Q==
解碼后:中文
SECTION 03 Hex 編解碼
通常我們會對于 URL Form GET 時候進行 16 進位編碼, 將 byte[] 轉成 char[],
package com.softleader.sample.codec;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.*;
public class HexTest {
public static void main(String args[]) {
Hex hex = new Hex();
String str = "中文";
char[] enbytes = null;
String encodeStr = null;
byte[] debytes = null;
String decodeStr = null;
try {
enbytes = hex.encodeHex(str.getBytes());
encodeStr = new String(enbytes);
debytes = hex.decodeHex(enbytes);
decodeStr = new String(debytes);
} catch (Exception ex) {
;
}
System.out.println("編碼前:"+str);
System.out.println("編碼后:"+encodeStr);
System.out.println("解碼后:"+decodeStr);
}
}
cmd>java com.softleader.sample.codec.HexTest
執行結果
編碼前:中文
編碼后:a4a4a4e5
解碼后:中文
SECTION 04 Metaphone 及 Soundex 編碼
Metaphone 建立出相同的key給發音相似的單字, 比 Soundex 還要準確, 但是 Metaphone 沒有固定長度, Soundex 則是固定第一個英文字加上3個數字. 這通常是用在類似音比對, 也可以用在 MP3 的軟件開發.
package com.softleader.sample.codec;
import org.apache.commons.codec.language.*;
import org.apache.commons.codec.*;
public class LanguageTest {
public static void main(String args[]) {
Metaphone metaphone = new Metaphone();
RefinedSoundex refinedSoundex = new RefinedSoundex();
Soundex soundex = new Soundex();
for (int i=0; i<2; i++ ) {
String str=(i==0)?"resume":"resin";
String mString = null;
String rString = null;
String sString = null;
try {
mString = metaphone.encode(str);
rString = refinedSoundex.encode(str);
sString = soundex.encode(str);
} catch (Exception ex) {
;
}
System.out.println("Original:"+str);
System.out.println("Metaphone:"+mString);
System.out.println("RefinedSoundex:"+rString);
System.out.println("Soundex:"+sString +"\n");
}
}
}
cmd>java com.softleader.sample.codec.LanguageTest
執行結果
Original:resume
Metaphone:RSM
RefinedSoundex:R903080
Soundex:R250
Original:resin
Metaphone:RSN
RefinedSoundex:R90308
Soundex:R250
可以看到 Soundex 靈敏度較低