java MD5,DES加密,解密

package common;

import javax.crypto.*;

import java.io.*;
import java.security.*;

public class Encrypter
{

    private Cipher ecipher;
    private Cipher dcipher;

    // md5编码
    public static String md5Encrypt(String s) throws Exception
    {

        MessageDigest md5 = MessageDigest.getInstance("MD5");

        sun.misc.BASE64Encoder base64Encoder = new sun.misc.BASE64Encoder();

        return base64Encoder.encode(md5.digest(s.getBytes("utf-8")));

    }

    public Encrypter()
    {
        try
        {
            SecretKey key = KeyGenerator.getInstance("DES").generateKey();
            ecipher = Cipher.getInstance("DES");
            dcipher = Cipher.getInstance("DES");
            ecipher.init(Cipher.ENCRYPT_MODE, key);
            dcipher.init(Cipher.DECRYPT_MODE, key);

        }
        catch (javax.crypto.NoSuchPaddingException e)
        {
        }
        catch (java.security.NoSuchAlgorithmException e)
        {
        }
        catch (java.security.InvalidKeyException e)
        {
        }
    }

    // DES编码
    public String desEncrypt(String s)
    {
        try
        {
            byte[] utf8 = s.getBytes("UTF8");

            byte[] enc = ecipher.doFinal(utf8);

            return new sun.misc.BASE64Encoder().encode(enc);
        }
        catch (javax.crypto.BadPaddingException e)
        {
        }
        catch (IllegalBlockSizeException e)
        {
        }
        catch (UnsupportedEncodingException e)
        {
        }
        catch (java.io.IOException e)
        {
        }
        return null;
    }

    // DES解码
    public String desDecrypt(String s)
    {
        try
        {
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(s);

            // 解码
            byte[] utf8 = dcipher.doFinal(dec);

            return new String(utf8, "UTF8");
        }
        catch (javax.crypto.BadPaddingException e)
        {
        }
        catch (IllegalBlockSizeException e)
        {
        }
        catch (UnsupportedEncodingException e)
        {
        }
        catch (java.io.IOException e)
        {
        }
        return null;
    }

}


除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: java md5 DES
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.