Java md5 密码加解密

传入同一密钥如:key123456 进行加密解密

package com.pub;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
  
public class PwdTools{
	/**
	 * 加密
	 * 
	 * @param content
	 *            待加密内容
	 * @param key
	 *            加密的密钥
	 * @return
	 */
	public static String encrypt(String content,String key){
		try{
			KeyGenerator kgen=KeyGenerator.getInstance("AES");			
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
            secureRandom.setSeed(key.getBytes());            
			kgen.init(128,secureRandom);
			SecretKey secretKey=kgen.generateKey();
			byte[] enCodeFormat=secretKey.getEncoded();
			SecretKeySpec secretKeySpec=new SecretKeySpec(enCodeFormat,"AES");
			Cipher cipher=Cipher.getInstance("AES");
			byte[] byteContent=content.getBytes("utf-8");
			cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
			byte[] byteRresult=cipher.doFinal(byteContent);
			StringBuffer sb=new StringBuffer();
			for(int i=0;i

md5加密,不可解密

    public static String getMd5(String plainText) {  
        try {  
            MessageDigest md = MessageDigest.getInstance("MD5");  
            md.update(plainText.getBytes());  
            byte b[] = md.digest();  
            int i;  
            StringBuffer buf = new StringBuffer("");  
            for (int offset = 0; offset < b.length; offset++) {  
                i = b[offset];  
                if (i < 0)  
                    i += 256;  
                if (i < 16)  
                    buf.append("0");  
                buf.append(Integer.toHexString(i));  
            }  
            //32位加密  
            return buf.toString();  
            // 16位的加密  
            //return buf.toString().substring(8, 24);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    } 

你可能感兴趣的:(Java后台)