`
lihua-he
  • 浏览: 101753 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

md5加密与base64加密解密

阅读更多
//测试类

public class Md5Test {

public static void main(String[] args) throws Exception {

String pswmd5 = Base64.encode(MD5Crypter.encryptiso("加密测试"));

//MD5加密
System.out.println("MD5加密:"+pswmd5);

//base64加密/解密
CommonCrypter a = new CommonCrypter();
        a.init();
System.out.println("base64加密:"+CommonCrypter.encrypt("加密测试"));
        System.out.println("base64解密:"+CommonCrypter.decrypt("9lNpivBEsuGeYhZ/dNL4rQ=="));

}

}


//md5类
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.log4j.Logger;
/**
* @author user
*
*/
public class MD5Crypter {

    private static Logger logger = Logger.getLogger("com.hintsoft.util.encoding.MD5Crypter");
    /**
     * 将数据使用MD5转码
     * @param message
     * @return
     */
    public static byte[] encryptUTF(String message) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
            md.reset();
            return md.digest(message.getBytes("UTF-8"));

        } catch (NoSuchAlgorithmException e) {

            logger.info("Password Md5 Error!");
        } catch (UnsupportedEncodingException e) {
            logger.info("Password Md5 Error!");
        }
        return null;

    }
   

    /**
     * 将数据使用MD5加密
     * @param message
     * @return
     */
    public static byte[] encryptiso(String message) {

        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
            md.reset();
            return md.digest(message.getBytes("iso8859-1"));

        } catch (NoSuchAlgorithmException e) {

            logger.info("Password Md5 Error!");
        } catch (UnsupportedEncodingException e) {
            logger.info("Password Md5 Error!");
        }
        return null;

    }
   
    /**
     * 将数据使用MD5转码
     * @param message
     * @return
     */
    public static byte[] encrypt(byte[] message) {

        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
            md.reset();
            return md.digest(message);

        } catch (NoSuchAlgorithmException e) {

            logger.info("Password Md5 Error!");
        }
       
        return null;

    }


}


//Base64类

package entity;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class CommonCrypter {

    private static  Cipher enCipher;

    private static  Cipher deCipher;

    private static CommonCrypter _instance = null;

    public static CommonCrypter getInstance() {
        if (_instance == null) {
            _instance = new CommonCrypter();
        }
        return _instance;
    }

    public void init() throws Exception {
        String keybyte = "hi%$so78";
        String ivbyte = "12up56^&";
        byte[] secKey = keybyte.getBytes();
        byte[] secIv = ivbyte.getBytes();
        // 创建MD5散列对象
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 散列密钥
        md.update(secKey);
        // 获得DES密钥
        DESKeySpec dks = new DESKeySpec(md.digest());
        // 获得DES加密密钥工厂
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 生成加密密钥
        SecretKey key = keyFactory.generateSecret(dks);
        // 创建初始化向量对象
        IvParameterSpec iv = new IvParameterSpec(secIv);
        AlgorithmParameterSpec paramSpec = iv;
        // 为加密算法指定填充方式,创建加密会话对象
        enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        // 为加密算法指定填充方式,创建解密会话对象
        deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        // 初始化加解密会话对象
        enCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        deCipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

    }

    /**
     * 加密数据
     *
     * @param data
     *            待加密二进制数据
     * @return 经BASE64编码过的加密数据
     * @throws Exception
     */
    public static String encrypt(String str) {
        if (str == null || str.equals("")) {
            return null;
        }
       
        try {
            byte[] data = str.getBytes();
            byte[] enc = null;
           
            synchronized(enCipher){
                enc = enCipher.doFinal(data);
            }

            return new BASE64Encoder().encode(enc);
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        }
        return null;
    }

    /**
     * 解密数据
     *
     * @param data
     *            待解密字符串(经过BASE64编码)
     * @return 解密后的二进制数据
     * @throws Exception
     */
    public static String decrypt(String str) throws Exception {
        if (str == null || str.equals("")) {
            return null;
        }
       
        try {
            byte[] dec  = new BASE64Decoder().decodeBuffer(str);
            byte[] result = null;
            synchronized(deCipher) {
                result = deCipher.doFinal(dec);
            }
            return new String(result);
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }
   
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics