MD5加密(带密钥)


package com.whc.noteserver.test;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


import org.apache.commons.codec.digest.DigestUtils;


public class MD5 {
    /**
     * @param text明文
     * @param key密钥
     * @return 密文
     */
    // 带秘钥加密
    public static String md5(String text, String key) throws Exception {
        // 加密后的字符串
        String md5str = DigestUtils.md5Hex(text + key);
        System.out.println("MD5加密后的字符串为:" + md5str);
        return md5str;
    }


    // 不带秘钥加密
    public static String md52(String text) throws Exception {
        // 加密后的字符串
        String md5str = DigestUtils.md5Hex(text);
        System.out.println("MD52加密后的字符串为:" + md5str + "\t长度:" + md5str.length());
        return md5str;
    }


    /**
     * MD5验证方法
     *
     * @param text明文
     * @param key密钥
     * @param md5密文
     */
    // 根据传入的密钥进行验证
    public static boolean verify(String text, String key, String md5) throws Exception {
        String md5str = md5(text, key);
        if (md5str.equalsIgnoreCase(md5)) {
            System.out.println("MD5验证通过");
            return true;
        }
        return false;
    }

    public static String readFileByBytes(String fileName){
        StringBuffer sBuffer = new StringBuffer();
    try {
        File file = new File(fileName);
        InputStream inputStream = null;
        if(file.isFile() && file.exists()) {
            System.out.println("进入if");
            byte[] tempbytes = new byte[1024];
            int byteread = 0;
            
                inputStream = new FileInputStream(file);
                System.out.println("进入while循环之前");
                while((byteread = inputStream.read(tempbytes)) != -1) {
                    System.out.println("进入循环");
                    String string = new String(tempbytes,0,byteread);
                    sBuffer.append(string);
                }
            
           }
        } catch (IOException e) {
            
        }finally {
            return sBuffer.toString();
        }
     
        
    }
    
    // 测试
        public static void main(String[] args) throws Exception{
            // String str =
            // "181115.041650.10.88.168.148.2665.2419425653_1";181115.040908.10.88.181.118.3013.1655327821_1
            /*
             * String str = "181115.040908.10.88.181.118.3013.1655327821_1";
             * System.out.println("加密的字符串:" + str + "\t长度:" + str.length()); MD5.md52(str);
             */
           
        }

}



  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Visual Studio .NET VB编程语言 MD5(DES) 8位密钥加密或解密文件function源代码 压缩包 : MD5 skey8位加密(文件).zip 列表 MD5 skey8位加密(文件)/ MD5 skey8位加密(文件)/bin/ MD5 skey8位加密(文件)/Form1.Designer.vb MD5 skey8位加密(文件)/Form1.resx MD5 skey8位加密(文件)/Form1.vb MD5 skey8位加密(文件)/MD5 skey8位加密(文件).vbproj MD5 skey8位加密(文件)/MD5 skey8位加密(文件).vbproj.user MD5 skey8位加密(文件)/My Project/ MD5 skey8位加密(文件)/My Project/Application.Designer.vb MD5 skey8位加密(文件)/My Project/Application.myapp MD5 skey8位加密(文件)/My Project/AssemblyInfo.vb MD5 skey8位加密(文件)/My Project/Resources.Designer.vb MD5 skey8位加密(文件)/My Project/Resources.resx MD5 skey8位加密(文件)/My Project/Settings.Designer.vb MD5 skey8位加密(文件)/My Project/Settings.settings MD5 skey8位加密(文件)/obj/ MD5 skey8位加密(文件)/obj/Debug/ MD5 skey8位加密(文件)/obj/Debug/CoreCompileInputs.cache MD5 skey8位加密(文件)/obj/Debug/DesignTimeResolveAssemblyReferences.cache MD5 skey8位加密(文件)/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).exe MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).pdb MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).vbproj.FileListAbsolute.txt MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).vbproj.GenerateResource.Cache MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).vbprojResolveAssemblyReference.cache MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).xml MD5 skey8位加密(文件)/obj/Debug/TempPE/ MD5 skey8位加密(文件)/obj/Debug/TempPE/My Project.Resources.Designer.vb.dll MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.exe MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.Form1.resources MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.pdb MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.Resources.resources MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.vbproj.FileListAbsolute.txt MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.vbproj.GenerateResource.Cache MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.xml MD5 skey8位加密(文件)/obj/Release/ MD5 skey8位加密(文件).sln MD5 skey8位加密(文件).v11.suo
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值