MD5加密解密类Java.docx

上传人:b****3 文档编号:4934896 上传时间:2022-12-11 格式:DOCX 页数:14 大小:16.97KB
下载 相关 举报
MD5加密解密类Java.docx_第1页
第1页 / 共14页
MD5加密解密类Java.docx_第2页
第2页 / 共14页
MD5加密解密类Java.docx_第3页
第3页 / 共14页
MD5加密解密类Java.docx_第4页
第4页 / 共14页
MD5加密解密类Java.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

MD5加密解密类Java.docx

《MD5加密解密类Java.docx》由会员分享,可在线阅读,更多相关《MD5加密解密类Java.docx(14页珍藏版)》请在冰豆网上搜索。

MD5加密解密类Java.docx

MD5加密解密类Java

packagecom.zyg.security.md5;  

 

importjava.io.UnsupportedEncodingException;  

importjava.security.MessageDigest;  

importjava.security.NoSuchAlgorithmException;  

importjava.security.SecureRandom;  

importjava.util.Arrays;  

 

publicclassMyMD5Util{  

      

   privatestaticfinalStringHEX_NUMS_STR="0123456789ABCDEF";  

   privatestaticfinalIntegerSALT_LENGTH=12;  

      

   /**  

    *将16进制字符串转换成字节数组  

    *@paramhex  

    *@return  

    */ 

   publicstaticbyte[]hexStringToByte(Stringhex){  

       intlen=(hex.length()/2);  

       byte[]result=newbyte[len];  

       char[]hexChars=hex.toCharArray();  

       for(inti=0;i

           intpos=i*2;  

           result[i]=(byte)(HEX_NUMS_STR.indexOf(hexChars[pos])<<4   

                           |HEX_NUMS_STR.indexOf(hexChars[pos+1]));  

       }  

       returnresult;  

   }  

 

      

   /** 

    *将指定byte数组转换成16进制字符串 

    *@paramb 

    *@return 

    */ 

   publicstaticStringbyteToHexString(byte[]b){  

       StringBufferhexString=newStringBuffer();  

       for(inti=0;i

           Stringhex=Integer.toHexString(b[i]&0xFF);  

           if(hex.length()==1){  

               hex='0'+hex;  

           }  

           hexString.append(hex.toUpperCase());  

       }  

       returnhexString.toString();  

   }  

      

   /** 

    *验证口令是否合法 

    *@parampassword 

    *@parampasswordInDb 

    *@return 

    *@throwsNoSuchAlgorithmException 

    *@throwsUnsupportedEncodingException 

    */ 

   publicstaticbooleanvalidPassword(Stringpassword,StringpasswordInDb)  

           throwsNoSuchAlgorithmException,UnsupportedEncodingException{  

       //将16进制字符串格式口令转换成字节数组  

       byte[]pwdInDb=hexStringToByte(passwordInDb);  

       //声明盐变量  

       byte[]salt=newbyte[SALT_LENGTH];  

       //将盐从数据库中保存的口令字节数组中提取出来  

       System.arraycopy(pwdInDb,0,salt,0,SALT_LENGTH);  

       //创建消息摘要对象  

       MessageDigestmd=MessageDigest.getInstance("MD5");  

       //将盐数据传入消息摘要对象  

       md.update(salt);  

       //将口令的数据传给消息摘要对象  

       md.update(password.getBytes("UTF-8"));  

       //生成输入口令的消息摘要  

       byte[]digest=md.digest();  

       //声明一个保存数据库中口令消息摘要的变量  

       byte[]digestInDb=newbyte[pwdInDb.length-SALT_LENGTH];  

       //取得数据库中口令的消息摘要  

       System.arraycopy(pwdInDb,SALT_LENGTH,digestInDb,0,digestInDb.length);  

       //比较根据输入口令生成的消息摘要和数据库中消息摘要是否相同  

       if(Arrays.equals(digest,digestInDb)){  

           //口令正确返回口令匹配消息  

           returntrue;  

       }else{  

           //口令不正确返回口令不匹配消息  

           returnfalse;  

       }  

   }  

 

 

   /** 

    *获得加密后的16进制形式口令 

    *@parampassword 

    *@return 

    *@throwsNoSuchAlgorithmException 

    *@throwsUnsupportedEncodingException 

    */ 

   publicstaticStringgetEncryptedPwd(Stringpassword)  

           throwsNoSuchAlgorithmException,UnsupportedEncodingException{  

       //声明加密后的口令数组变量  

       byte[]pwd=null;  

       //随机数生成器  

       SecureRandomrandom=newSecureRandom();  

       //声明盐数组变量  

       byte[]salt=newbyte[SALT_LENGTH];  

       //将随机数放入盐变量中  

       random.nextBytes(salt);  

 

       //声明消息摘要对象  

       MessageDigestmd=null;  

       //创建消息摘要  

       md=MessageDigest.getInstance("MD5");  

       //将盐数据传入消息摘要对象  

       md.update(salt);  

       //将口令的数据传给消息摘要对象  

       md.update(password.getBytes("UTF-8"));  

       //获得消息摘要的字节数组  

       byte[]digest=md.digest();  

 

       //因为要在口令的字节数组中存放盐,所以加上盐的字节长度  

       pwd=newbyte[digest.length+SALT_LENGTH];  

       //将盐的字节拷贝到生成的加密口令字节数组的前12个字节,以便在验证口令时取出盐  

       System.arraycopy(salt,0,pwd,0,SALT_LENGTH);  

       //将消息摘要拷贝到加密口令字节数组从第13个字节开始的字节  

       System.arraycopy(digest,0,pwd,SALT_LENGTH,digest.length);  

       //将字节数组格式加密后的口令转化为16进制字符串格式的口令  

       returnbyteToHexString(pwd);  

   }  

packagecom.zyg.security.md5;

importjava.io.UnsupportedEncodingException;

importjava.security.MessageDigest;

importjava.security.NoSuchAlgorithmException;

importjava.security.SecureRandom;

importjava.util.Arrays;

publicclassMyMD5Util{

 

 privatestaticfinalStringHEX_NUMS_STR="0123456789ABCDEF";

 privatestaticfinalIntegerSALT_LENGTH=12;

 

 /**

 *将16进制字符串转换成字节数组

 *@paramhex

 *@return

 */

 publicstaticbyte[]hexStringToByte(Stringhex){

  intlen=(hex.length()/2);

  byte[]result=newbyte[len];

  char[]hexChars=hex.toCharArray();

  for(inti=0;i

   intpos=i*2;

   result[i]=(byte)(HEX_NUMS_STR.indexOf(hexChars[pos])<<4

       |HEX_NUMS_STR.indexOf(hexChars[pos+1]));

  }

  returnresult;

 }

 

 /**

 *将指定byte数组转换成16进制字符串

 *@paramb

 *@return

 */

 publicstaticStringbyteToHexString(byte[]b){

  StringBufferhexString=newStringBuffer();

  for(inti=0;i

   Stringhex=Integer.toHexString(b[i]&0xFF);

   if(hex.length()==1){

    hex='0'+hex;

   }

   hexString.append(hex.toUpperCase());

  }

  returnhexString.toString();

 }

 

 /**

 *验证口令是否合法

 *@parampassword

 *@parampasswordInDb

 *@return

 *@throwsNoSuchAlgorithmException

 *@throwsUnsupportedEncodingException

 */

 publicstaticbooleanvalidPassword(Stringpassword,StringpasswordInDb)

   throwsNoSuchAlgorithmException,UnsupportedEncodingException{

  //将16进制字符串格式口令转换成字节数组

  byte[]pwdInDb=hexStringToByte(passwordInDb);

  //声明盐变量

  byte[]salt=newbyte[SALT_LENGTH];

  //将盐从数据库中保存的口令字节数组中提取出来

  System.arraycopy(pwdInDb,0,salt,0,SALT_LENGTH);

  //创建消息摘要对象

  MessageDigestmd=MessageDigest.getInstance("MD5");

  //将盐数据传入消息摘要对象

  md.update(salt);

  //将口令的数据传给消息摘要对象

  md.update(password.getBytes("UTF-8"));

  //生成输入口令的消息摘要

  byte[]digest=md.digest();

  //声明一个保存数据库中口令消息摘要的变量

  byte[]digestInDb=newbyte[pwdInDb.length-SALT_LENGTH];

  //取得数据库中口令的消息摘要

  System.arraycopy(pwdInDb,SALT_LENGTH,digestInDb,0,digestInDb.length);

  //比较根据输入口令生成的消息摘要和数据库中消息摘要是否相同

  if(Arrays.equals(digest,digestInDb)){

   //口令正确返回口令匹配消息

   returntrue;

  }else{

   //口令不正确返回口令不匹配消息

   returnfalse;

  }

 }

 /**

 *获得加密后的16进制形式口令

 *@parampassword

 *@return

 *@throwsNoSuchAlgorithmException

 *@throwsUnsupportedEncodingException

 */

 publicstaticStringgetEncryptedPwd(Stringpassword)

   throwsNoSuchAlgorithmException,UnsupportedEncodingException{

  //声明加密后的口令数组变量

  byte[]pwd=null;

  //随机数生成器

  SecureRandomrandom=newSecureRandom();

  //声明盐数组变量

  byte[]salt=newbyte[SALT_LENGTH];

  //将随机数放入盐变量中

  random.nextBytes(salt);

  //声明消息摘要对象

  MessageDigestmd=null;

  //创建消息摘要

  md=MessageDigest.getInstance("MD5");

  //将盐数据传入消息摘要对象

  md.update(salt);

  //将口令的数据传给消息摘要对象

  md.update(password.getBytes("UTF-8"));

  //获得消息摘要的字节数组

  byte[]digest=md.digest();

  //因为要在口令的字节数组中存放盐,所以加上盐的字节长度

  pwd=newbyte[digest.length+SALT_LENGTH];

  //将盐的字节拷贝到生成的加密口令字节数组的前12个字节,以便在验证口令时取出盐

  System.arraycopy(salt,0,pwd,0,SALT_LENGTH);

  //将消息摘要拷贝到加密口令字节数组从第13个字节开始的字节

  System.arraycopy(digest,0,pwd,SALT_LENGTH,digest.length);

  //将字节数组格式加密后的口令转化为16进制字符串格式的口令

  returnbyteToHexString(pwd);

 }

}

 

测试类——Client,代码如下:

viewplaincopytoclipboardprint?

packagecom.zyg.security.md5;  

 

importjava.io.UnsupportedEncodingException;  

importjava.security.NoSuchAlgorithmException;  

importjava.util.HashMap;  

importjava.util.Map;  

 

publicclassClient{  

   privatestaticMapusers=newHashMap();  

      

   publicstaticvoidmain(String[]args){  

       StringuserName="zyg";  

       Stringpassword="123";  

       registerUser(userName,password);  

          

       userName="changong";  

       password="456";  

       registerUser(userName,password);  

          

       StringloginUserId="zyg";  

       Stringpwd="1232";  

       try{  

           if(loginValid(loginUserId,pwd)){  

               System.out.println("欢迎登陆!

");  

           }else{  

               System.out.println("口令错误,请重新输入!

");  

           }  

       }catch(NoSuchAlgorithmExceptione){  

           //TODOAuto-generatedcatchblock  

           e.printStackTrace();  

       }catch(UnsupportedEncodingExceptione){  

           //TODOAuto-generatedcatchblock  

           e.printStackTrace();  

       }   

   }  

      

   /** 

    *注册用户 

    *  

    *@paramuserName 

    *@parampassword 

    */ 

   publicstaticvoidregisterUser(StringuserName,Stringpassword){  

       StringencryptedPwd=null;  

       try{  

           encryptedPwd=MyMD5Util.getEncryptedPwd(password);  

              

           users.put(userName,encryptedPwd);  

              

       }catch(NoSuchAlgorithmExceptione){  

           //TODOAuto-generatedcatchblock  

           e.printStackTrace();  

       }catch(UnsupportedEncodingExceptione){  

           //TODOAuto-generatedcatchblock  

           e.printStackTrace();  

       }  

   }  

      

   /** 

    *验证登陆 

    *  

    *@paramuserName 

    *@parampassword 

    *@return 

    *@throwsUnsupportedEncodingException  

    *@throwsNoSuchAlgorithmException  

    */ 

   publicstaticbooleanloginValid(StringuserName,Stringpassword)   

               throwsNoSuchAlgorithmException,UnsupportedEncodingException{  

       S

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1