Package COM. jadyer. util. codec; import Java. security. key; import Java. security. nosuchalgorithmexception; import javax. crypto. cipher; import javax. crypto. keygenerator; import javax. crypto. secretkey; import javax. crypto. secretkeyfactory; import javax. crypto. spec. desedekeyspec; import Org. apache. commons. codec. binary. base64;/*** desede symmetric encryption algorithm * @ see desede, also known as triple-Des, known as 3DES * @ see for code comments, see my previous article http://blog.csdn.net/jadyer/article/details/7615951 */public class desedecodec {public static final string key_algorithm = "desede"; public static final string cipher_algorithm = "desede/ECB/pkcs5padding "; /*** generate the key * @ see java6 only supports 56-bit keys * @ see bouncycastle supports 64-bit keys, the official website is http://www.bouncycastle.org/*/public static string initkey () throws nosuchalgorithmexception {keygenerator kg = keygenerator. getinstance (key_algorithm); kg. init (168); secretkey = kg. generatekey (); Return base64.encodebase64string (secretkey. getencoded ();}/*** conversion key */Private Static key Tokey (byte [] Key) throws exception {desedekeyspec DKS = new desedekeyspec (key ); secretkeyfactory keyfactory = secretkeyfactory. getinstance (key_algorithm); secretkey = keyfactory. generatesecret (DKS); Return secretkey ;} /*** encrypt data ** @ Param data to be encrypted * @ Param key * @ return encrypted data */public static string encrypt (string data, string key) throws exception {key K = Tokey (base64.decodebase64 (key); cipher = cipher. getinstance (cipher_algorithm); cipher. init (cipher. encrypt_mode, k); Return base64.encodebase64string (cipher. dofinal (data. getbytes ()));} /*** decrypt data ** @ Param data to be decrypted data * @ Param key * @ return decrypted data */public static string decrypt (string data, string key) throws exception {key K = Tokey (base64.decodebase64 (key); cipher = cipher. getinstance (cipher_algorithm); cipher. init (cipher. decrypt_mode, k); return new string (cipher. dofinal (base64.decodebase64 (data);} public static void main (string [] ARGs) throws exception {string source = ""; system. out. println ("Original:" + source); string key = initkey (); system. out. println ("key:" + key); string encryptdata = encrypt (source, key); system. out. println ("encryption:" + encryptdata); string decryptdata = decrypt (encryptdata, key); system. out. println ("decryption:" + decryptdata );}}