java MD5/DES

1. MD5/SHA/SHA-256/SHA-384/SHA-512

VertifyMD5.java

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//import bouncycastle.*;
//import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class VertifyMD5 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	//Security.addProvider(new BouncyCastleProvider());
	String aaa = "abc";
	String mac128byte = MD5Encode(aaa);
	//byte[] data = Base64.encode("abc");
	System.out.println(mac128byte);
	
	}
	static public String MD5Encode(String strSrc) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("SHA-512");
			String result = "";
			byte[] temp;
			temp = md5.digest(strSrc.getBytes("UTF8"));
			
			System.out.println("temp--------->temp:"+temp.length);
			for (int i = 0; i < temp.length; i++) {
				result += Integer.toHexString(
						(0x000000ff & temp[i]) | 0xffffff00).substring(6);
			}

			return result;

		} catch (NoSuchAlgorithmException e) {

			e.printStackTrace();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}

2.DES

DesEncrypt.java

import java.security.Key; 
import java.security.SecureRandom; 
import javax.crypto.Cipher; 
import javax.crypto.KeyGenerator; 
public class DesEncrypt { 
     
    public Key  getKey(String strKey) { 
        try { 
            KeyGenerator _generator = KeyGenerator.getInstance("DES"); 
            _generator.init(new SecureRandom(strKey.getBytes())); 
            Key tmpkey = _generator.generateKey(); 
            _generator = null; 
            return tmpkey;
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
        return null;
    } 

    public String getEncString(String strMing, Key key) { 
        byte[] byteMi = null; 
        byte[] byteMing = null; 
        String strMi = ""; 
        try { 
            return byte2hex(getEncCode(strMing.getBytes(),key)); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            byteMing = null; 
            byteMi = null; 
        } 
            return strMi; 
    } 


    public String getDesString(String strMi, Key key) { 
        byte[] byteMing = null; 
        byte[] byteMi = null; 
        String strMing = ""; 
        try { 
            return new String(getDesCode(hex2byte(strMi.getBytes()),key)); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            byteMing = null; 
            byteMi = null; 
        } 
        return strMing; 
    } 
    //cipher class for encode and decode
    private byte[] getEncCode(byte[] byteS, Key key) { 
        byte[] byteFina = null; 
        Cipher cipher; 
        try { 
            cipher = Cipher.getInstance("DES"); 
            cipher.init(Cipher.ENCRYPT_MODE, key); 
            byteFina = cipher.doFinal(byteS); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            cipher = null; 
        } 
        return byteFina; 
    } 

    private byte[] getDesCode(byte[] byteD, Key key) { 
        Cipher cipher; 
        byte[] byteFina = null; 
        try { 
            cipher = Cipher.getInstance("DES"); 
            cipher.init(Cipher.DECRYPT_MODE, key); 
            byteFina = cipher.doFinal(byteD); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            cipher = null; 
        } 
        return byteFina; 
    } 


    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(); 
    } 



    public static byte[] hex2byte(byte[] b) { 
        if ((b.length % 2) != 0) 
            throw new IllegalArgumentException("not odds"); 
        byte[] b2 = new byte[b.length / 2]; 
        for (int n = 0; n < b.length; n += 2) { 
            String item = new String(b, n, 2); 
            b2[n / 2] = (byte) Integer.parseInt(item, 16); 
        } 
        return b2; 
    } 

    public static void main(String[] args) { 
        //System.out.println("hello"); 
        DesEncrypt des = new DesEncrypt(); 
        Key key1 = des.getKey("adfsdf");
        System.out.println(key1);
        String strEnc = des.getEncString("122", key1); 
        System.out.println(strEnc); 
        DesEncrypt des2 = new DesEncrypt(); 
        Key key2 = des2.getKey("adfsdf");
        System.out.println(key1);
        String strDes = des2.getDesString(strEnc, key1); 
        System.out.println(strDes); 
        //new DesEncrypt(); 
    } 
    
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值