Cow toolkit

xiaoxiao2021-03-06  63

Cattle toolkit 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;

/ ** * create 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. * Common toolkit. Including all kinds of password random strings, encrypted decryption, coding decoding, executing URL, etc. * /

public class CryptTool {/ ** * generate password. * @param count the number of passwords * @param letters contain characters * @param numbers contain numbers * @return String password * / public static String getPassword (int count, boolean letters, Boolean number) {Return Org.apache.commons.lang.randomstringutils.randM (count, letters, numbers);

/ ** * Generate a brigade mixed password. * @Param Count Password bits * @return string password * / private static string getpassword (int count) {return getpassword (count, true, true);}

/ ** * Generate a pure digital password. * @Param country password number * @Return string password * / public static string getPasswordofNumber (int count) {Return GetPassword (count, false, true);}

/ ** * Generate a pure character password. * @Param Count Password bits * @Return string password * / public static string getPasswordOfcharacter (int count) {Return GetPassword (count, true, false);}

/ ** * 3DES key generation. * @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 decryption (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 decryption (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 encryption (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 copher = javax.crypto.cipher.getInstance ("DeSede"); cipher.init (javax.crypto.cipher.encrypt_mode, key);

Return Cipher.dofinal (SRC);

/ ** * 3DES encryption (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 abstract calculation (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 DigestReturn Alg.Digest (src);}

/ ** * MD5 digest calculation (String). * @Param src String * @throws Exception * @return String * / public static String md5Digest (String src) throws Exception {return new String (md5Digest (src.getBytes ())) }

. / ** * BASE64 coding * @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 coding (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 decoding * @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 decoded (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 encoded gb2312. * @Param src string * @return string * / public static string urlencode (SRC, "GB2312");}

/ ** * URL Decoding GB2312 * @Param VALUE Decoded * @return Decoded String (String Value) {Return Urldecode (Value, "GB2312"); }

/ ** * MLL encoded for a given character. * @Param src string * @Param CODER Character encoding format (GB2312 / GBK) * @return string * / public static string Urlencode (String src, string code) {Try {SRC = java.net.urlencoder.Encode (src, code);

Return Src;} catch (exception ex) {ex.printstacktrace ();

Return Src;

/ ** * MLL decoding for a given character * @Param Value Decoded string * @Param Coder character encoding format (GB2312 / GBK) * @return Decoded string * / public static string urldecode (String Value, String coder) {Try {returnjava.net.urdecoder.decode (value, coder);} catch (exception ex) {ex.printstacktrace ();}

Return Value;

/ * ** perform a given url * @param urlString given url * @return return value * / 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 ();}}

转载请注明原文地址:https://www.9cbs.com/read-89166.html

New Post(0)