8月11
工作中遇到的课题,要求简单的实现对所取到的手机号码进行加密。
老大只要求简单实现,比如移位……

以servlet实现


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.security.*;
import javax.crypto.*;

public class MyPhoneNum
   extends HttpServlet {

 private static String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish

 static boolean debug = false;

 static{
   Security.addProvider(new com.sun.crypto.provider.SunJCE());
 }

 //生成密钥, 注意此步骤时间比较长
 public static byte[] getKey() throws Exception{
   KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
   SecretKey deskey = keygen.generateKey();
   if (debug)
     System.out.println("生成密钥:"+byte2hex(deskey.getEncoded()));
   return deskey.getEncoded();
 }

 //加密
 public static byte[] encode(byte[] input,byte[] key) throws Exception{
   SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm);
   if (debug){
     System.out.println("加密前的二进串:"+byte2hex(input));
     System.out.println("加密前的字符串:"+new String(input));
   }
   Cipher c1 = Cipher.getInstance(Algorithm);
   c1.init(Cipher.ENCRYPT_MODE,deskey);
   byte[] cipherByte=c1.doFinal(input);
   if (debug)
     System.out.println("加密后的二进串:"+byte2hex(cipherByte));
   return cipherByte;
 }

 //解密
 public static byte[] decode(byte[] input,byte[] key) throws Exception{
   SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm);
   if (debug)
     System.out.println("解密前的信息:"+byte2hex(input));    
   Cipher c1 = Cipher.getInstance(Algorithm);
   c1.init(Cipher.DECRYPT_MODE,deskey);
   byte[] clearByte=c1.doFinal(input);
   if (debug){
     System.out.println("解密后的二进串:"+byte2hex(clearByte));
     System.out.println("解密后的字符串:"+(new String(clearByte)));
   }
   return clearByte;
 }

 //md5()信息摘要, 不可逆
 public static byte[] md5(byte[] input) throws Exception{
   java.security.MessageDigest alg=java.security.MessageDigest.getInstance("MD5"); //or "SHA-1"
   if (debug){
     System.out.println("摘要前的二进串:"+byte2hex(input));
     System.out.println("摘要前的字符串:"+new String(input));
   }
   alg.update(input);
   byte[] digest = alg.digest();
   if (debug)
     System.out.println("摘要后的二进串:"+byte2hex(digest));
   return digest;
 }


 //字节码转换成16进制字符串
 public static String byte2hex(byte bytes[]){
   StringBuffer retString = new StringBuffer();
   for (int i = 0; i < bytes.length; ++i)
   {
     retString.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1).toUpperCase());
   }
   return retString.toString();
 }

 //将16进制字符串转换成字节码
 public static byte[] hex2byte(String hex) {
   byte[] bts = new byte[hex.length() / 2];
   for (int i = 0; i < bts.length; i++) {
      bts[i] = (byte) Integer.parseInt(hex.substring(2*i, 2*i+2), 16);
   }
   return bts;
 }  

 private void performTask(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response) {
   String MISC_MSISDN = request.getHeader("X-Up-Calling-Line-ID");
   if(MISC_MSISDN==null) MISC_MSISDN = "13900000000";    
   
   
   try {      
     String getCode = this.getParameter(request,"code","0");
     String getKey = this.getParameter(request,"key","0");
     String getMd5 = this.getParameter(request,"md5","0");
     MISC_MSISDN = this.getParameter(request,"MISC_MSISDN",MISC_MSISDN);*/
     /*PrintWriter out1 = response.getWriter();
     out1.println(getCode);
     out1.println(getKey);
     out1.println(getMd5);
     out1.close();*/
     if(getCode.equals("0")){
       byte[] key = getKey();
       if (!getKey.equals("0")){
         key = hex2byte(getKey);
       }
       String strCode = byte2hex(encode(MISC_MSISDN.getBytes(),key));
       String strKey = byte2hex(key);
       String md5Num = byte2hex(md5(MISC_MSISDN.getBytes()));
       PrintWriter out = response.getWriter();
       out.println("Code:"+strCode);
       out.println("Key:"+strKey);
       out.println("md5 Phone Number:"+md5Num);
       out.close();
     }else{
       byte[] byteCode = hex2byte(getCode);
       byte[] byteKey = hex2byte(getKey);
       String strPhoneNum = new String(decode(byteCode,byteKey));
       PrintWriter out = response.getWriter();
       out.println("Phone Number:"+strPhoneNum);
       if (getMd5.equals("0")){
         out.println("None md5 check!");
       }else if (getMd5.equals(byte2hex(md5(strPhoneNum.getBytes())))){
         out.println("md5 check Ok!");
       }else{
         out.println("md5 check false!");
       }
       out.close();
     }
   }catch (Exception e) {
     System.out.println(e.getMessage());
   }
   

 }

 public void doGet(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response) {
   performTask(request, response);
 }

 public void doPost(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response) {
   performTask(request, response);
   }

 private java.lang.String getParameter(javax.servlet.http.HttpServletRequest request, java.lang.String parameterName, java.lang.String defaultValue) throws java.lang.Exception {
   java.lang.String[] parameterValues = null;
   java.lang.String paramValue = null;
   
   parameterValues = request.getParameterValues(parameterName);
   if (parameterValues != null)
     paramValue = parameterValues[0];

   if (paramValue == null)
     paramValue = defaultValue;

     return paramValue;
 }
}

最后编辑: 毫无逻辑 编辑于2006/08/11 23:51

3 条评论 to “Java 简单的加密解密实现方法”

这还 说:
2008/07/16 16:48
fear
MeanBoy 说:
2008/01/23 15:12
这样的类的确很恐怖。楼主应该好好学一下如何写出优美的代码。而不是只追求实现。
corejava 说:
2007/08/02 14:19
这还简单 ,shit
整一大堆
我就要个string加密进去
解密出来,
你倒是给我个入口和出口啊
毫无逻辑 回复于 2007/08/03 10:39
这位粗口的访客,您真的是做java开发的吗?
分页: 1/1 第一页 1 最后页
//add 2008-08-17 //add finished
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]