http://www.javaidea.net/list.jsp?topic=5
作者:家居貓
BASE64 編碼是一種常用的字元編碼,在很多地方都會用到。JDK 中提供了非常方便的 BASE64Encoder 和 BASE64Decoder,用它們可以非常方便的完成基於 BASE64 的編碼和解碼。下面是本人編的兩個小的函數,分別用於 BASE64 的編碼和解碼:
// 將 s 進行 BASE64 編碼
public static String getBASE64(String s) {
if (s == null) return null;
return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}
// 將 BASE64 編碼的字串 s 進行解碼
public static String getFromBASE64(String s) {
if (s == null) return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
------
回複此文章 |
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
------
回複此文章 |
http://dev2dev.bea.com.cn/techdoc/webser/20030573.html
作者:TIM PIJPOPS
使用報文摘要
Java中提供了計算報文摘要的另一個簡單的方法,那就是使用java.security.MessageDigest類。下列代碼片斷顯示了如何將MD5報文摘要演算法(128位的摘要)應用到密碼字串:
MassageDigest md=
MessageDigest.getInstance("MD5");
md.update(originalPwd.getByetes());
byte[] digestedBytes=md.digest();
也使用報文摘要建立校正和、文本的唯一ID(也叫做數位指紋)。在簽寫ARJ檔案會發生:校正和是根據ARJ檔案的內容計算出來的,然後被加密,並且用base64的加密格式存放在manifest.mf檔案中。base64是編碼任意位元據的一種方法,得到的結果僅包含可列印字元(注意,base64編碼資料佔用的空間比轉換前多三分之一)。由於報文摘要演算法輸出的結果是位元組數組,可以使用base64編碼將雜湊位元組轉換成字串,以便能將該字串存放在資料庫的varchar欄位中。現在有許多base64編碼器,但是最簡單的方法是使用weblogic.jar庫中的編碼器:weblogic.apache.xerces.utils.Base64。該類的作用微乎其微,如下面的代碼例子所示:
String digestedPwdString =
new String(Base64.encode(digestedPwdBytes));
------
回複此文章 |
http://minnigerode.org/CA-SF/dave/BasicJBossAAC.html
Feb. 2004
M. David Minnigerode
minniger@minnigerode.org
import javax.mail.internet.*;
import java.security.*;
public String getEncodedHash(String clearText){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream out = MimeUtility.encode(baos,"base64");
MessageDigest md = MessageDigest.getInstance("SHA");
if(clearText == null) clearText = "";
byte [] in = clearText.getBytes();
byte [] digested = md.digest(in);
out.write(digested);
out.close();
return new String(baos.toByteArray(), "ISO-8859-1");
}