Simple encryption / decryption method of packaging, with encode (), decode (), md5 () Date: 2004-07-30 Author: [posted] Source:
Package steeven;
/ * Purpose: Simple encryption / decryption methods of packaging: steeven@kali.com.cn Date: 12/05/2001 thanks: http://www-900.ibm.com/developerWorks/java/l-security/index. SHTML
Description: This Class Need JCE, Download Here: http://java.sun.com/security/index.html
* /
Import java.security. *; import javax.crypto. *; public class crypt {
Private static string algorithm = "des"; // Defines an encryption algorithm, available in des, despe, blowfish
Static Boolean Debug = False;
Static {security.addprovider (new com.sun.crypto.provider.sunjce ());
/ / Generate a key, pay attention to this step time Public static Byte [] getKey () throws exception {keygenerator keygen = keygenerator.getInstance (algorithm); secretkey design = keygen.generateKey (); if (debug) system.out. Println ("Generate Keys:" Byte2Hex (DESKEY.GETENCODED ()))); return deskey.getencoded ();}
// encryption public static byte [] encode (byte [] input, byte [] key) throws Exception {SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key, Algorithm); if (debug) {System.out.println ("Two-in-bits before encryption:" Byte2HEX (Input)); System.out.println ("String before encryption:" new string (input));} Cipher C1 = Cipher.GetInstance (Algorithm); C1.init (cipher.encrypt_mode, deskey); byte [] cipherbyte = c1.dofinal (input); if (debug) System.out.println ("Encrypted Bit:" Byte2HEX (Cipherbyte); return Cipherbyte;}
// decryption public static byte [] decode (byte [] input, byte [] key) throws Exception {SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key, Algorithm); if (debug) System.out.println ( "Information before decryption:" byte2hex (input); cipher c1 = cipher.getInstance (algorithm); c1.init (cipher.decrypt_mode, deskey); byte [] clearbyte = c1.dofinal (input); if (debug) ) {System.out.println ("Decrypted binary:" byte2HEX (Clearbyte)); system.out.println ("Decoupted strings:" (New String (Clearbyte));} return Clearbyte;} // md5 () information summary, irreversible public static Byte [] MD5 (byte [] INPUT) THROWS Exception {java.security.Security.MessageDigest.GetInstance ("MD5"); // OR "SHA-1" {system.out.println ("Summary before:" Byte2HEX (Input)); System.out.Println ("Summary before summary:" new string INPUT);} Alg.Update (Input); Byte [] Digest = alg.digest (); if (debug) System.out.Println ("Summary) (" Summary): Byte2HEX (Digest)); Return Digest;}
// Bytecode Convert to 16 Binding Strings Public Static String Byte2HEX (Byte [] B) {String HS = ""; String STMP = ""; for (int N = 0; n stmp = (java.lang. Integer.tohexString (B [N] & 0xFF)); if (stmp.Length () == 1) HS = HS "0" stmp; else HS = HS STMP; if (n} return HS.touppercase () }
Public static void main (string [] args) throws exception {debug = true; // Byte [] key = getKey (); byte [] key = "Good learning" .getbytes (); decode (Encode ("test encryption" .getbytes (), key), key); MD5 ("test encryption" .getbytes ());}}