base64加密解密

来源:互联网 发布:java中的输入输出流 编辑:程序博客网 时间:2024/05/20 23:39

import java.io.IOException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Password {
 private static String key="ketter";

 public static String Encode(String msg){
  msg=msg+key;
  BASE64Encoder encode= new BASE64Encoder();
  String encodeString= encode.encodeBuffer(msg.getBytes());
  return encodeString;
 }

 public static String Decode(String msg){
  BASE64Decoder decode= new BASE64Decoder();
  try {
   byte[] b=decode.decodeBuffer(msg);
   String decodeString=new String(b);
   String result=decodeString.substring(0,decodeString.indexOf(key));
   return result;
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;  
 }

}