⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md5util.java

📁 文件的加密解密代码主要是针对于DES和3DES MD5加密解密算法的功能实现
💻 JAVA
字号:
package com.zrx.Util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest ;
import java.security.NoSuchAlgorithmException;
/**
* Encoder类用于MD5加密
* @author yunfeiyang
*/
public class MD5Util
{ /**
   * 实例化Encoder类
*/
    public MD5Util()
    {

    }
private MessageDigest digest;
    /**
     *MD5加密类
     * @see java.security.MessageDigest
     */
    public static final String MD5="MD5";
    /**
     * SHA类型的加密方式
     */
    public static final String SHA="SHA";
   
    /**
     * 加密类型,默认为MD5
     */
    private String type=MD5Util.MD5; 
   
    /**
     * 获取加密类型
     * @return 加密类型
     */
    public String getType()
    {
        return this.type;
    }
    /**
     * 设置加密类型
     * @param type 加密类型
     */
    public void setType(String type)
    {
     this.type=type;
           }
    /**
     *加密文件
     * @param file 需要加密的文件
     * @return 加密后的特征值
     * @throws java.io.FileNotFoundException 文件不存在时抛出此异常
     * @throws java.security.NoSuchAlgorithmException 加密方法不存在时抛出此异常
     * @throws java.io.IOException 文件读取出错时抛出此异常
     */
    public String encoder(File file) throws FileNotFoundException, NoSuchAlgorithmException, IOException
    {
        FileInputStream fis=null;
        if(file==null || file.isDirectory()|| !file.exists())
        {
            throw new FileNotFoundException("文件不存在");
        }
        else
        {
          fis=new FileInputStream(file);
        }
      byte[] result=new byte[2048];
      digest=MessageDigest.getInstance(getType());
      int length=-1;
      while((length=fis.read(result))!=-1)
      {
          digest.update(result,0,length);
      }
     fis.close();
     return bytesToString(digest.digest());
    }
   /**
    * 将字节数组转换为十六进制字符串
    * @param data 输入的字节数组
    * @return 转换后的字符串
    */
   public String bytesToString(byte[] data) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
                'e', 'f'};
        char[] temp = new char[data.length * 2];
        for (int i = 0; i < data.length; i++) {
            byte b = data[i];
            temp[i * 2] = hexDigits[b >>> 4 & 0x0f];
            temp[i * 2 + 1] = hexDigits[b & 0x0f];
        }
        return new String(temp);

    }


    /**
     *加密字符串,产生特征值
     * @param message 加密的消息
     * @return 加密后的散列值
     * @throws Exception
     * @throws java.security.NoSuchAlgorithmException
     * @see java.security.NoSuchAlgorithmException
     */
    //加密字符串,产生特征值
public String encode(String message) throws Exception, NoSuchAlgorithmException
{ 
        if(message==null || message.equals(""))
        {
            throw new Exception("消息为空");
        }
   else
        {
            byte [] result;
            digest=MessageDigest.getInstance(getType());
            digest.update(message.getBytes());
    result= digest.digest();
            StringBuffer sb=new StringBuffer();
            for (int i=0;i<result.length;i++)
            {
                int val=((int)result[i])&0xFF;
                if(val<16)
                {
                    sb.append("0");
                }
                sb.append(Integer.toHexString(val).toUpperCase());
            }
            return sb.toString();
        }
    
}
    /**
     *判断特征值是否与给定字符串的特征值匹配
     * @param code 加密后的特征值
     * @param source 给定的字符串字符串
     * @return 判断特征值是与给定字符串的特征值匹配
     * @throws java.security.NoSuchAlgorithmException
     * @throws Exception
     */
    public boolean checkKey(String code,String source) throws NoSuchAlgorithmException, Exception
    { 
                String temp=encode(source);
                if (temp.equals(code))
                 return true;
                 return false;
            
    }
    /**
     *检查特征值是否匹配给定文件的特征值
     * @param code 给定的特征值
     * @param file 待检验的文件
     * @return 特征值是否匹配
     * @throws java.io.FileNotFoundException
     * @throws java.security.NoSuchAlgorithmException
     * @throws java.io.IOException
     */
    public boolean checkKey(String code,File file)throws FileNotFoundException, NoSuchAlgorithmException, IOException
    {

       String temp=this.encoder(file);
           if(temp.equals(code))
          return true;
       return false;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -