邮件开发——base64账号密码转换
1 package com.hq.base64; 2 3 import java.io.BufferedReader; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 import java.io.UnsupportedEncodingException; 10 import java.util.Properties; 11 12 import javax.activation.DataHandler; 13 import javax.activation.DataSource; 14 import javax.activation.FileDataSource; 15 import javax.mail.Address; 16 import javax.mail.Authenticator; 17 import javax.mail.Message; 18 import javax.mail.MessagingException; 19 import javax.mail.PasswordAuthentication; 20 import javax.mail.Session; 21 import javax.mail.Transport; 22 import javax.mail.Message.RecipientType; 23 import javax.mail.internet.AddressException; 24 import javax.mail.internet.InternetAddress; 25 import javax.mail.internet.MimeBodyPart; 26 import javax.mail.internet.MimeMessage; 27 import javax.mail.internet.MimeMultipart; 28 import javax.mail.internet.MimeUtility; 29 30 31 32 33 import sun.misc.BASE64Encoder; 34 35 public class Base64Util { 36 37 /** 38 * @param args add by zxx ,Dec 30, 2008 39 * @throws IOException 40 */ 41 public static void main(String[] args) throws IOException { 42 BASE64Encoder encoder = new BASE64Encoder(); 43 System.out.println("please input user name:"); 44 String username = new BufferedReader( 45 new InputStreamReader(System.in)) 46 .readLine(); 47 System.out.println(encoder.encode(username.getBytes())); 48 System.out.println("please input password:"); 49 String password = new BufferedReader( 50 new InputStreamReader(System.in)) 51 .readLine(); 52 System.out.println(encoder.encode(password.getBytes())); 53 } 54 }
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到。