Java encryption and decryption-desede symmetric encryption algorithm

Source: Internet
Author: User
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 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.