服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

我的常用工具包

import javax.crypto.secretkey;
import javax.crypto.spec.secretkeyspec;
import java.net.url;
import java.net.httpurlconnection;
import java.io.printwriter;
import java.io.bufferedreader;
import java.io.inputstreamreader;

/**
 * created by intellij idea.
 * user: zhengzhg
 * mail: snake_country@sina.com
 * date: 2004-10-13
 * time: 15:30:28
 * to change this template use file | settings | file templates.
 * 常用工具包。包括生成各种密码随机串,加密解密,编码解码,执行url等
 */

public class crypttool {
    /**
     * 生成密码.
     * @param count 密码位数
     * @param letters 是否包含字符
     * @param numbers 是否包含数字
     * @return string password
     */
    public static string getpassword(int count, boolean letters, boolean numbers) {
        return org.apache.commons.lang.randomstringutils.random(count, letters, numbers);
    }

    /**
     * 生成字符数字混合的密码.
     * @param count 密码位数
     * @return string password
     */
    private static string getpassword(int count) {
        return getpassword(count, true, true);
    }


    /**
     * 生成纯数字密码.
     * @param count 密码位数
     * @return string password
     */
    public static string getpasswordofnumber(int count) {
        return getpassword(count, false, true);
    }

    /**
     * 生成纯字符密码.
     * @param count 密码位数
     * @return string password
     */
    public static string getpasswordofcharacter(int count) {
        return getpassword(count, true, false);
    }

    /**
     * 生成3des密钥.
     * @param key_byte seed key
     * @throws exception
     * @return javax.crypto.secretkey generated des key
     */
    public static javax.crypto.secretkey gendeskey(byte[] key_byte) throws exception {
        secretkey k = new secretkeyspec(key_byte, "desede");

        return k;
    }

    /**
     * 3des 解密(byte[]).
     * @param key secretkey
     * @param crypt byte[]
     * @throws exception
     * @return byte[]
     */
    public static byte[] desdecrypt(javax.crypto.secretkey key, byte[] crypt) throws exception {
        javax.crypto.cipher cipher = javax.crypto.cipher.getinstance("desede");
        cipher.init(javax.crypto.cipher.decrypt_mode, key);

        return cipher.dofinal(crypt);
    }

    /**
     * 3des 解密(string).
     * @param key secretkey
     * @param crypt byte[]
     * @throws exception
     * @return byte[]
     */
    public static string desdecrypt(javax.crypto.secretkey key, string crypt) throws exception {
        return new string(desdecrypt(key, crypt.getbytes()));
    }

    /**
     * 3des加密(byte[]).
     * @param key secretkey
     * @param src byte[]
     * @throws exception
     * @return byte[]
     */
    public static byte[] desencrypt(javax.crypto.secretkey key, byte[] src) throws exception {
        javax.crypto.cipher cipher = javax.crypto.cipher.getinstance("desede");
        cipher.init(javax.crypto.cipher.encrypt_mode, key);

        return cipher.dofinal(src);
    }

    /**
     * 3des加密(string).
     * @param key secretkey
     * @param src byte[]
     * @throws exception
     * @return byte[]
     */
    public static string desencrypt(javax.crypto.secretkey key, string src) throws exception {
        return new string(desencrypt(key, src.getbytes()));
    }

    /**
     * md5 摘要计算(byte[]).
     * @param src byte[]
     * @throws exception
     * @return byte[] 16 bit digest
     */
    public static byte[] md5digest(byte[] src) throws exception {
        java.security.messagedigest alg = java.security.messagedigest.getinstance("md5");
        // md5 is 16 bit message digest

        return alg.digest(src);
    }

    /**
     * md5 摘要计算(string).
     * @param src string
     * @throws exception
     * @return string
     */
    public static string md5digest(string src) throws exception {
        return new string(md5digest(src.getbytes()));
    }

    /**
     * base64 编码.
     * @param src string inputed string
     * @return string returned string
     */
    public static string base64encode(string src) {
        sun.misc.base64encoder encoder = new sun.misc.base64encoder();

        return encoder.encode(src.getbytes());
    }

    /**
     * base64 编码(byte[]).
     * @param src byte[] inputed string
     * @return string returned string
     */
    public static string base64encode(byte[] src) {
        sun.misc.base64encoder encoder = new sun.misc.base64encoder();

        return encoder.encode(src);
    }

    /**
     * base64 解码.
     * @param src string inputed string
     * @return string returned string
     */
    public static string base64decode(string src) {
        sun.misc.base64decoder decoder = new sun.misc.base64decoder();

        try {
            return new string(decoder.decodebuffer(src));
        } catch (exception ex) {
            return null;
        }
    }

    /**
     * base64 解码(to byte[]).
     * @param src string inputed string
     * @return string returned string
     */
    public static byte[] base64decodetobytes(string src) {
        sun.misc.base64decoder decoder = new sun.misc.base64decoder();

        try {
            return decoder.decodebuffer(src);
        } catch (exception ex) {
            return null;
        }
    }

    /**
     * 对给定字符进行 url 编码gb2312.
     * @param src string
     * @return string
     */
    public static string urlencode(string src) {
        return urlencode(src, "gb2312");
    }

    /**
     * 对给定字符进行 url 解码gb2312
     * @param value 解码前的字符串
     * @return 解码后的字符串
     */
    public static string urldecode(string value) {
        return urldecode(value, "gb2312");
    }

    /**
     * 对给定字符进行 url 编码.
     * @param src string
     * @param coder 字符编码格式(gb2312/gbk)
     * @return string
     */
    public static string urlencode(string src, string coder) {
        try {
            src = java.net.urlencoder.encode(src, coder);

            return src;
        } catch (exception ex) {
            ex.printstacktrace();
        }

        return src;
    }

    /**
     * 对给定字符进行 url 解码
     * @param value 解码前的字符串
     * @param coder 字符编码格式(gb2312/gbk)
     * @return 解码后的字符串
     */
    public static string urldecode(string value, string coder) {
        try {
            return java.net.urldecoder.decode(value, coder);
        } catch (exception ex) {
            ex.printstacktrace();
        }

        return value;
    }

    /**
     * 执行给定url
     * @param urlstring 给定的url
     * @return 返回值
     */
    public static string executeurl(string urlstring) throws exception {
        stringbuffer document = new stringbuffer();
        url url = new url(urlstring);
        urlconnection conn = url.openconnection();
        bufferedreader reader = new bufferedreader(new inputstreamreader(conn.getinputstream()));

        string line = null;
        while ((line = reader.readline()) != null)
            document.append(line + "/n");

        reader.close();
       
        return document.tostring();
    }
}

扫描关注微信公众号