Crypttool encapsulates some encryption tools - practical

zhaozj2021-02-16  57

Package com.bonjovi.utils.encrypt;

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

Import javax.crypto. *; import javax.crypto.spec.secretKeyspec;

/ *** Crypttool encapsulates some encryption tool methods, including 3Des, MD5, etc. ** @Author Bonjovi * @version 1.0 * 2004-05-12 * / public class crypttool {

Public crypttool () {}

/ ** * 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 { // javax.crypto.spec.DESKeySpec deskeyspec = new javax.crypto.spec.DESKeySpec (// key_byte);. // javax.crypto.SecretKeyFactory skf = javax.crypto.SecretKeyFactory // getInstance ( "DES", "SunJCE "); // return (javax.crypto.SecretKey) skf.generateSecret (deskeyspec); // KeyGenerator kg = KeyGenerator.getInstance (" DESede "); SecretKey k = null; 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 , 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 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 summary 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 Digest

Return 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 for a given character. * * @Param src string * @return string * / public static string urlencode (string src) {Try {src = java.net.urlencoder.Encode (src, "GB2312" );

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

Return Src;

/ ** * MLL decoding for a given character * @Param Value Decoded string * @return decoded strings * / public string urldecode (string value) {Try {returnjava.net.urdecoder.Decode (Value , "GB2312");} catch (exception ex) {ex.printstacktrace ();

Return Value;

/ ** Test crypt * / public static void main (String [] args) {byte src_byte [] = "1234567812345678" .getBytes (); System.out.println (src_byte.length); byte key_byte [] = "123456781234567812345678" .getbytes (); // 3des 24 BYTES Key

Try {// Generate DES Key Javax.crypto.secretKey Deskey; // Generate DES Key // javax.crypto.keyGenerator Key = javax.crypto.keygenerator.getInstance (// "des"); // Key.init (56); // deskey = key.generateKey ();

Deskey = gendeskey; system.out.println ("generator des key ok"); // des enchanted byte [] encrypt, decrypt; // encryption encrypt = desencrypt (deskey, src_byte); system.out.println ("Encrypt =" New String (Encrypt)); // Decryption Decrypt = Desdecrypt (Deskey, Encrypt); System.out.Println ("Decrypt =" New String (Decrypt));

// String S = "12345678"; // // Encrypted // s = desencrypt (deskey, s); // system.out.println ("encrypt =" s); // // Decryption // s = Desdecrypt (Deskey, s); // system.out.println ("decrypt =" s);

} Catch (exception ex) {ex.printstacktrace ();

System.out.println ("Base64 Test:" Base64Decode (Base64Encode)));

}

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

New Post(0)