程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> Base64編碼的Java語言實現

Base64編碼的Java語言實現

編輯:JAVA編程入門知識

  import Java.io.*;
  
  public class MIMEBase64 {
  /*
  這是個簡單的Base64編碼程序
  作者:Roc Chen [email protected]
  Base64 使用US-ASCII子集的65個字符, 每個字符用6位表示
  因此"m"的Base64值為38, 二進制形式是 100110.
  
  對於文本串,編碼過程如下。例如"men":
  
  先轉成US-ASCII值.
  
  "m"十進制 109
  "e"十進制 101
  "n"十進制 110
  
  二進制 :
  
  m 01101101
  e 01100101
  n 01101110
  
  三個8位連起來是24位
  011011010110010101101110
  
  然後分成4個6位
  011011 010110 010101 101110
  
  現在得到4個值,十進制為
  27 22 21 46
  
  對應的 Base64 字符是 :
  b W V u
  
  編碼總是基於3個字符,從而產生4個Base64字符。
  假如只剩1或2個字符,使用非凡字符"="補齊Base64的4字。
  如,編碼"me"
  
  01101101 01100101
  0110110101100101
  011011 010110 0101
  111111 (與,補足6位)
  011011 010110 010100
  b W U
  b W U = ("=" 補足4字符)
  
  於是 "bWU=" 就是"me"的Base64值.
  
  再如編碼 "m"
  
  01101101
  011011 01
  111111
  011011 010000
  b Q = =
  於是 "bQ==" 就是"m"的Base64值.
  
  值得注重的是,MIME規定一行最多76個字符.
  
  */
  
  static String BaseTable[] = {
  "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
  "Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f",
  "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
  "w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"
  };
  
  
  public static void encode(String filename, BufferedWriter out) {
  try {
  File f = new File(filename);
  FileInputStream fin = new FileInputStream(filename);
  
  // 讀文件到BYTE數組
  byte bytes[] = new byte[(int)(f.length())];
  int n = fin.read(bytes);
  
  if (n < 1) return; // 沒有內容
  
  byte buf[] = new byte[4]; // base64 字符數組
  
  int n3byt = n / 3; // 3 bytes 組數
  int nrest = n % 3; // 分組後剩余 bytes
  int k = n3byt * 3; //
  int linelength = 0; // 行長
  int i = 0; // 指針
  
  // 3-bytes 分組 ...
  while ( i < k ) {
  buf[0] = (byte)(( bytes[i] & 0xFC) >> 2);
  buf[1] = (byte)(((bytes[i] & 0x03) << 4)
  ((bytes[i+1] & 0xF0) >> 4));
  buf[2] = (byte)(((bytes[i+1] & 0x0F) << 2)
  ((bytes[i+2] & 0xC0) >> 6));
  buf[3] = (byte)( bytes[i+2] & 0x3F);
  send(out, BaseTable[buf[0]]);
  send(out, BaseTable[buf[1]]);
  send(out, BaseTable[buf[2]]);
  send(out, BaseTable[buf[3]]);
  /*
  以上代碼可以優化,但會難以理解
  buf[0]= (byte)(b[i] >> 2);
  buf[1]= (byte)(((b[i] & 0x03) << 4)(b[i+1]>> 4));
  buf[2]= (byte)(((b[i+1] & 0x0F)<< 2)(b[i+2]>> 6));
  buf[3]= (byte)(b[i+2] & 0x3F);
  send(out,BaseTable[buf[0]]+BaseTable[buf[1]]+
  BaseTable[buf[2]]+BaseTable[buf[3]]);
  */
  
  if ((linelength += 4) >= 76) {
  send(out, " ");
  linelength = 0;
  }
  i += 3;
  }
  
  // 處理尾部 ...
  if (nrest==2) {
  // 2 bytes left
  buf[0] = (byte)(( bytes[k] & 0xFC) >> 2);
  buf[1] = (byte)(((bytes[k] & 0x03) << 4)
  ((bytes[k+1] & 0xF0) >> 4));
  buf[2] = (byte)(( bytes[k+1] & 0x0F) << 2);
  }
  else if (nrest==1) {
  // 1 byte left
  buf[0] = (byte)((bytes[k] & 0xFC) >> 2);
  buf[1] = (byte)((bytes[k] & 0x03) << 4);
  }
  if (nrest > 0) {
  // 發送尾部
  if ((linelength += 4) >= 76) send(out, "
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved