java.security.MessageDigest



最近研究jive,把jive源码移植到我的myeclipse中,在使jive跑起来的过程中来理解jive的技术点,首先是用户登录,了解了登录模块需要的加密算法。

java.security.MessageDigest简介: 

常用场景: 
    一般我们数据库登录时,需要对用户的密码进行加密操作,不直接将原密码保存到数据库中,用户登录时候也是采用加密算法进行比较 

本案只介绍使用java.security.MessageDigest,对数据进行加密操作。该方法是单向的加密算法 

详细代码如下所表示: 
//基本的公用类 

package com.xue.security;  
   
 import java.io.IOException;  
 import java.security.MessageDigest;  
 import java.security.NoSuchAlgorithmException;  
   
 public class SecurityTest  
 {  
     // MD5是16位,SHA是20位(这是两种报文摘要的算法) 定义两种加密算法的类  
     private static final String MD5_ALGORITHM = "MD5";  
       
     private static final String SHA_1_ALGORITHM = "SHA-1";  
       
     /** 十六进制 */  
     private static final int HEX_NUM = 16;  
       
     /** 十六进制码 */  
     private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",  
         "e", "f"};  
   
   
   
 } 

//对数据进行加密的方法: 

/**对语句进行单向加密 
     * <功能详细描述> 
     * @param message   需要加密的信息 
     * @return 
     * @throws NoSuchAlgorithmException 
     * @throws IOException [参数说明] 
     *  
     * @return byte[] [返回类型说明] 
     * @exception throws [违例类型] [违例说明] 
     * @see [类、类#方法、类#成员] 
     */  
    public byte[] encrypt(String message)  
        throws NoSuchAlgorithmException, IOException  
    {  
          
        // 加密  
        MessageDigest encrypt = MessageDigest.getInstance(SecurityTest.MD5_ALGORITHM);  
          
        // 添加需要加密的信息  
        encrypt.update(message.getBytes());  
          
        // 对信息信息加密  
        byte[] encryptMD5 = encrypt.digest();  
          
        //获得加密算法  
        System.out.println(encrypt.getAlgorithm());  
          
        //得到加密算法的长度  
        System.out.println(encrypt.getDigestLength());  
        return encryptMD5;  
         
    }  
 




//对数据进行base64转发,有可能某些byte是不可打印的字符。 
存在几种处理方式, 
方式一:转化为base64 

/** 将数据转化为BASE64码进行保存 
    * <功能详细描述> 
    * @param encryptMD5 
    * @return [参数说明] 
    *  
    * @return String [返回类型说明] 
    * @exception throws [违例类型] [违例说明] 
    * @see [类、类#方法、类#成员] 
    */  
   public String transform(byte[] encryptMD5)  
   {  
       //对数据进行base64转发,有可能某些byte是不可打印的字符。   
       BASE64Encoder basEncoder = new BASE64Encoder();   
       return basEncoder.encode(encryptMD5);  
   } 

//将byte转化为16进制 
有两种方式转化为16进制 
第一种: 

/** 
     * 转化方式二 
     * 2进制转16进制方式二 <功能详细描述> 
     *  
     * @param b 
     * @return [参数说明] 
     *  
     * @return String [返回类型说明] 
     * @exception throws [违例类型] [违例说明] 
     * @see [类、类#方法、类#成员] 
     */  
    public static String byte2hex(byte[] b)  
    {  
        String hs = "";  
        String stmp = "";  
        for (int n = 0; n < b.length; n++)  
        {  
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));  
              
            if (stmp.length() == 1)  
            {  
                hs = hs + "0" + stmp;  
            }  
            else  
            {  
                hs = hs + stmp;  
            }  
        }  
          
        return hs.toUpperCase();  
          
    } 

第二种: 

/** 
      * 转化方式二 
      * 将数据转化为16进制进行保存,因为有些byte是不能打印的字符 字节数组的转化 
      *  
      * @param b 字节数组 
      * @return [参数说明] 
      *  
      * @return String [返回类型说明] 
      * @exception throws [违例类型] [违例说明] 
      * @see [类、类#方法、类#成员] 
      */  
     public static String byteArrayToHexString(byte[] b)  
     {  
         StringBuffer result = new StringBuffer(128);  
         for (int i = 0; i < b.length; i++)  
         {  
             result.append(byteToHexString(b[i]));  
         }  
         return result.toString();  
     }  
       
     /** 
      * 转化方式二 
      * 将数据转化为16进制进行保存,因为有些byte是不能打印的字符 单字节转化 
      *  
      * @param b 
      * @return [参数说明] 
      *  
      * @return String [返回类型说明] 
      * @exception throws [违例类型] [违例说明] 
      * @see [类、类#方法、类#成员] 
      */  
     public static String byteToHexString(byte b)  
     {  
         int n = b;  
         if (0 > n)  
         {  
             n = 256 + n;  
         }  
         int d1 = n / HEX_NUM;  
         int d2 = n % HEX_NUM;  
         return HEX_DIGITS[d1] + HEX_DIGITS[d2];  
     } 


最后将Byte数组转化为String保存到DB中 


比较两个加密byte数组是否相同 

/** 将数据转化为BASE64码进行保存 
    * <功能详细描述> 
    * @param encryptMD5 
    * @return [参数说明] 
    *  
    * @return String [返回类型说明] 
    * @exception throws [违例类型] [违例说明] 
    * @see [类、类#方法、类#成员] 
    */  
   public String transform(byte[] encryptMD5)  
   {  
       //对数据进行base64转发,有可能某些byte是不可打印的字符。   
       BASE64Encoder basEncoder = new BASE64Encoder();   
       return basEncoder.encode(encryptMD5);  
   }


测试函数 

public static void main(String[] args)  
        throws NoSuchAlgorithmException, IOException  
    {  
        SecurityTest t = new SecurityTest();  
        //得到加密数组  
        byte[] data = t.encrypt("我的测试信息!");  
        //转化为字符串,采用base编码方式  
        String baseResult = SecurityTest.transform(data);  
          
        String hexResult = SecurityTest.byte2hex(data);  
          
        System.out.println("baseResult"+baseResult);  
        System.out.println("hexResult"+hexResult);  
          
    }

结果: 
baseResult:+HwBij+rZkL3Ihsxx2xFsA== 
hexResult:F87C018A3FAB6642F7221B31C76C45B0 










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值