Import Java.security.*;import Javax.crypto.cipher;import Javax.crypto.secretkey;import Javax.crypto.secretkeyfactory;import javax.crypto.spec.deskeyspec;/** * Copyright GuangZhou Cotel Co., Ltd. * All rig HT reserved. * DES encryption and decryption classes. * @author <a href= "mailto:[email protected]" mce_href= "mailto:[email protected]" >AmigoXie</a > * @version 1.0 * Creation date:2007-7-31-morning 11:59:28 */public class Des {/** encrypt, decrypt key. */private static FI nal String password_crypt_key = "Kehrdooxwhcwtfesxvdvgqzq"; /** encryption algorithm, available des,desede,blowfish. */private final static String algorithm = "DES"; public static void Main (string[] args) throws Exception {String Md5password = "202cb962ac59075b964b07152d234b70"; String str = des.encrypt (Md5password); System.out.println ("str:" + str); str = des.decrypt (str); System.out.println ("str:" + str); }/** * des encryption of data. * @param data pending DES encryption * @return Returns the number of DES encryptedAccording to * @throws Exception * @author <a href= "mailto:[email protected]" mce_href= "mailto:[email protected] ">AmigoXie</a> * Creation date:2007-7-31-PM 12:06:24 */Public final static string decrypt (String da TA) throws Exception {return new String (Decrypt (Hex2byte (Data.getbytes ()), Password_crypt_key.getby TES ())); /** * Decrypts data that has been encrypted with DES. * @param data des encrypted data * @return return decrypted @throws Exception * @author <a href= "mailto:[email protecte D] "mce_href=" mailto:[email protected] ">AmigoXie</a> * Creation date:2007-7-31-PM 12:07:54 */P Ublic final static string encrypt (string data) throws Exception {return Byte2hex (Encrypt (Data.getbytes (), Passwor D_crypt_key. GetBytes ())); /** * uses the specified key to perform DES encryption on the data. * @param data to be encrypted * @param key des encrypted key * @return return des encrypted data * @throws Exception * @author <a href = "Mailto:[eMail protected] "mce_href=" mailto:[email protected] ">AmigoXie</a> * Creation date:2007-7-31-PM 1 2:09:03 */private static byte[] Encrypt (byte[] data, byte[] key) throws Exception {//des algorithm requires a trustworthy random number source securerandom sr = new SecureRandom (); Create Deskeyspec object from raw key data deskeyspec DKs = new Deskeyspec (key); Create a key factory and use it to convert deskeyspec to//a Secretkey object secretkeyfactory keyfactory = Secretkeyfactory.getinstance (A LGORITHM); Secretkey SecureKey = Keyfactory.generatesecret (DKS); The Cipher object actually completes the cryptographic operation Cipher Cipher = cipher.getinstance (algorithm); Initialize the Cipher object with a key Cipher.init (Cipher.encrypt_mode, SecureKey, SR); Now, get the data and encrypt//formally perform the cryptographic Operation return cipher.dofinal (data); /** * The data is des decrypted with the specified key. * @param data to be decrypted * @param key des decrypted key * @return return des decrypted data * @throws Exception * @author <a href = "mailto:[email protected]" mce_href= "mailto:[email protected" >AmigoXie</a> * Creation date:2007-7-31-PM 12:10:34 * * Privat e Static byte[] Decrypt (byte[] data, byte[] key) throws Exception {//des algorithm requires a trustworthy random number source SecureRandom sr = New SecureRandom (); Create a Deskeyspec object from the original key data deskeyspec DKs = new Deskeyspec (key); Create a key factory and use it to convert the Deskeyspec object to//a Secretkey object secretkeyfactory keyfactory = secretkeyfactory.getinstance (algorithm); Secretkey SecureKey = Keyfactory.generatesecret (DKS); The Cipher object actually completes the decryption operation Cipher Cipher = cipher.getinstance (algorithm); Initialize the Cipher object with a key Cipher.init (Cipher.decrypt_mode, SecureKey, SR); Now, get the data and decrypt//formally perform the decryption Operation return cipher.dofinal (data); } public static byte[] Hex2byte (byte[] b) {if ((b.length% 2)! = 0) throw new Illegalargumentexcepti On ("Length is not even"); byte[] B2 = new BYTE[B.LENGTH/2]; for (int n = 0; n < b.leNgth; n + = 2) {String item = new string (b, n, 2); B2[N/2] = (byte) integer.parseint (item, 16); } return B2; } public static string Byte2hex (byte[] b) {string hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) {stmp = (java.lang.Integer.toHexString (b[n] & 0XFF)); if (stmp.length () = = 1) HS = HS + "0" + stmp; else HS = hs + stmp; } return Hs.touppercase (); }}
Des encryption and Decryption class-java