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

📄 md5.txt

📁 md5加解密 System.out.println("解密后的二进串:" + byte2hex(clearByte)+" ") System.out.println("解密后的字符串
💻 TXT
字号:
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import junit.framework.TestCase;

/**
 * <p>EncryptUnEncrypt.java</p>
 * <p>@title: 加密解密算法</p>
 * <p>@description: 可用于加密解密</p>
 * <p>@author: ex_huxinsheng</p>
 * <p>@version: 1.0</p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: PAIC</p>
 */
public class EncryptUnEncrypt extends TestCase {

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

    // 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())+"\n");
        return deskey.getEncoded();
    }

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

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

    // md5()信息摘要, 不可逆
    public static byte[] md5(byte[] input) throws Exception {
        try {
            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)+"\n");
            }
            alg.update(input);
            byte[] digest = alg.digest();
            if (debug)
                System.out.println("摘要后的二进串:" + byte2hex(digest)+"\n");
            return digest;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    // 字节码转换成16进制字符串
    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        try {
            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;
//                if (n < b.length - 1)
//                    hs = hs + ":";
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return hs;
    }

    public void testEncrypt() throws Exception {
        debug = true;
        byte[] key = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
                'b', 'c', 'd', 'e', 'f' };
        String name="胡新胜";
        String age="12";
        String address="深圳市盐田区";
        System.out.println("findUserInfo.do?userName="+name+"&age="+age+"&address="+address);
        System.out.println();
        byte ename [] = encode(name.getBytes(), key);
        byte eage [] = encode(age.getBytes(), key);
        byte eaddress [] = encode(address.getBytes(), key);
        System.out.println("加密后的:   findUserInfo.do?userName="+byte2hex(md5(ename))+"&age="+byte2hex(md5(eage))+"&address="+byte2hex(md5(eaddress))+"\")");
        System.out.println();
        System.out.println("解密后的:   findUserInfo.do?userName="+new String(decode(ename,key))+"&age="+new String(decode(eage,key))+"&address="+new String(decode(eaddress,key))+"\")");
        
    }
} 

⌨️ 快捷键说明

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