Some code examples of encrypted decryption

zhaozj2021-02-16  111

Package util; import java.io. *; import java.security. *; import javax.crypto. *; import javax.crypto.spec.secretkeyspec; / *** crypttool packaged some encryption tool, including 3Des, MD5, etc. . ** @Author beansoft * @version 1.0 * 2004-05-12 * / public class crypttool {public crypttool () {} / ** * Generate a 3DES key. * * @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 copher.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.getibytes);} / ** * 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 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 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 Digest Return Alg.digest (src);} / ** * MD5 Summary Calculation. * * @Param SRC String * @Throws Exception * @Return string * / public static strin g 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 encoding (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;}} / ** * Pair of given characters Perform URL encoding. * * @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;} / ** * Perform URL Decoding for a given character * @Param Value Decoded string * @return decoded string * / public string urldecode (String " Value) { Try {return java.net.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;

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

New Post(0)