JAVA-DES encryption and decryption (episode)
Code directly, everyone knows
Package com. cdsmartlink. des; import java. security. *; import javax. crypto. cipher; import javax. crypto. secretKey; import javax. crypto. secretKeyFactory; import javax. crypto. spec. DESKeySpec;/*** All right reserved. * DES encryption and decryption class. * @ author liuyazhuang * @ version 1.0 * Creation date:-13:59:28 */public class Des {/** encrypt and decrypt keys. */private static final String PASSWORD_CRYPT_KEY = kEHrDooxWHCWtfeSx VDvgqZq;/** encryption algorithm, which can be 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);}/*** performs DES encryption on the data. * @ param data the data to be encrypted by DES * @ return returns the data encrypted by DES. * @ Throws Exception * @ author liuyazhuang * Creation date:-14:27:25 pm */public final static String decrypt (String data) throws Exception {return new String (decrypt (hex2byte (data. getBytes (), PASSWORD_CRYPT_KEY.getBytes ();}/*** decrypts data encrypted with DES. * @ param data DES encrypted data * @ return returns decrypted data * @ throws Exception * @ author liuyazhuang * Creation date:-14:37:54 pm */public fi Nal static String encrypt (String data) throws Exception {return byte2hex (encrypt (data. getBytes (), PASSWORD_CRYPT_KEY. getBytes ();}/*** use the specified key to encrypt the data using DES. * @ param data to be encrypted * @ param key DES encrypted key * @ return returns DES encrypted data * @ throws Exception * @ author liuyazhuang * Creation date: -14:58:15 */private static byte [] encrypt (byte [] data, byte [] key) throws Exception {// DES algorithm requires Trusted random number source SecureRandom sr = new SecureRandom (); // create the DESKeySpec object DESKeySpec dks = new DESKeySpec (key) from the original key data; // create a key factory, then use it to convert DESKeySpec into a // SecretKey object SecretKeyFactory keyFactory = SecretKeyFactory. getInstance (ALGORITHM); SecretKey securekey = keyFactory. generateSecret (dks); // The Cipher object actually completes the encryption operation Cipher cipher = Cipher. getInstance (ALGORITHM); // use the key to initialize the cgorer object Cipher. init (Cipher. ENCRYPT_MODE, Securekey, sr); // now, get the data and encrypt it. // perform the encryption Operation return cipher. doFinal (data);}/*** use the specified key to decrypt the data using DES. * @ param data to be decrypted * @ param key DES the decrypted key * @ return returns DES decrypted data * @ throws Exception * @ author liuyazhuang * Creation date: -15:28:36 */private static byte [] decrypt (byte [] data, byte [] key) throws Exception {// The DES algorithm requires a trusted random number source SecureRandom sr = new SecureRandom (); // create a DE from the original key data SKeySpec object DESKeySpec dks = new DESKeySpec (key); // create a key factory, and then 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); // use the key to initialize the cgorer object Cipher. init (Cipher. DECRYPT_MODE, securekey, sr); // now, obtain and decrypt the data. // perform the return cipher operation. do Final (data);} public static byte [] hex2byte (byte [] B) {if (B. length % 2 )! = 0) throw new IllegalArgumentException (the length is not an even number); 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 ();}}