Java encryption and decryption-DES 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. deskeyspec; import Org. apache. commons. codec. binary. base64; /*** DES symmetric encryption algorithm * @ see ============================== ======================================================== ========================================================== =====* @ See symmetric encryption algorithm is used to encrypt and decrypt data. Use a key to encrypt the data, and use the same key to decrypt the data * @ see des is an algorithm proposed by the US National Institute of Standards. Since the data security of encryption and decryption is proportional to the key length, the 56-bit key of Des has formed a security risk * @ see and later improved the DES algorithm, with the Triple DES algorithm (also called desede or triple-DES ). The full name is TDEA: Triple Data Encryption Algorithm * @ see desede improves the DES algorithm's key length and the number of iterations, improved security strength * @ see. However, the desede algorithm processing speed is slow and the key computing time is long, the low encryption efficiency makes the development of symmetric encryption algorithms not optimistic * @ see ========================== ========================================================== ========================================================== ===========* @ see Java and bouncycastle support different DES algorithm data encryption, mainly reflected in the key length, working mode and filling mode * @ see java6 only supports 56-bit keys, while bouncycastle supports 64-bit keys, its official website is http://www.bouncy Castle.org/* @ see, even on the desede algorithm, bouncycastle's key length is also longer than Java's key length * @ see ==================== ========================================================== ========================================================== =========* @ see In addition, java APIs only provide three symmetric encryption algorithm key material implementation classes: Des, desede, and PBE * @ see ================== ========================================================== ========================================================== =====================* @ see more java encryption and decryption algorithm implementations, you can refer to this blog http :// Blog.csdn.net/kongqz/article/category/800296 * @ see ============================== ========================================================== =================================================================== */Public class descodec {// algorithm name public static final string key_algorithm = "des "; // algorithm name/encryption mode/filling mode // des has four working modes --> ECB: Electronic cryptographic mode, CBC: Encrypted group link mode, CFB: encryption feedback mode, ofB: output feedback mode public static final string cipher_algorithm = "des/ECB/pkcs5padding ";/*** Generate key */public static string initkey () throws nosuchalgorithmexception {keygenerator kg = keygenerator. getinstance (key_algorithm); // instantiate the key generator kg. init (56); // initialize the secret generator secretkey = kg. generatekey (); // generate the key return base64.encodebase64string (secretkey. getencoded (); // obtain the binary key encoding form}/*** convert key */Private Static key Tokey (byte [] key) throws exception {javaseyspec DKS = new javaseyspec (key); // instantiate the des key Sec Retkeyfactory keyfactory = secretkeyfactory. getinstance (key_algorithm); // instantiate the key factory secretkey = keyfactory. generatesecret (DKS); // generate the key 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); // restore the Key Cipher cipher = cipher. getinstance (cipher_algorithm );// Instantiate the cipher object, which is used to complete the actual encryption operation cipher. init (cipher. encrypt_mode, k); // initialize the cipher object and set it to the encryption mode return base64.encodebase64string (cipher. dofinal (data. getbytes (); // performs the encryption operation. Encrypted results are usually transmitted in base64 encoding.}/***** 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); // initialize the cipher object and set it to the decryption mode return new string (cipher. dofinal (base64.decodebase64 (data); // execute the decryption operation} 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.