Java implementation DES encryption and decryption algorithm

Source: Internet
Author: User
Tags decrypt

The DES algorithm has three entry parameters: Key, Data, Mode. Where key is 8 bytes A total of 64 bits, is the working key of the DES algorithm, the data is 8 bytes 64 bits, is to be encrypted or decrypted, the mode for DES works, there are two kinds: encryption or decryption.
The DES algorithm works like this: If mode is encrypted, the data is encrypted using key to generate the password form (64 bits) of the output of the DES, and the data is decrypted, and if mode is decryption, a key is used to decrypt the information in the form of the password. The plaintext (64-bit) reverts to data as the output of DES. At both ends of the communication network, both parties agree to the key, in the source point of communication with key to the core data des encryption, and then in the form of a password in the public communications network (such as telephone network) to the end of the communication network, the data arrives at the destination, with the same key to decrypt the password data, It reproduces the core data in the form of plaintext. This ensures the security and reliability of core data (such as pins, MACS, etc.) that are transmitted in the public communication network.
By periodically switching to a new key at the source and destination of the communication network, it is possible to further improve the confidentiality of the data, which is now a popular practice in the financial Transactions network.

Here is the specific code: (Remember to remember the string to byte or byte to string must be encoded, otherwise it may be garbled)

Importjava.io.IOException;ImportJava.security.SecureRandom;ImportJavax.crypto.Cipher;ImportJavax.crypto.SecretKey;Importjavax.crypto.SecretKeyFactory;ImportJavax.crypto.spec.DESKeySpec;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;/*** des encryption and decryption algorithm * *@authorLifq * @date 2015-3-17 a.m. 10:12:11*/ Public classDesutil {Private Final StaticString des = "des"; Private Final StaticString ENCODE = "GBK"; Private Final StaticString Defaultkey = "test1234";  Public Static voidMain (string[] args)throwsException {String data= "Test ss"; //System.err.println (Encrypt (data, key)); //System.err.println (Decrypt (Encrypt (data, key), key));System.out.println (Encrypt (data));    System.out.println (Decrypt (Encrypt (data))); }    /*** Use default key encryption * *@returnString *@authorLifq * @date 2015-3-17 pm 02:46:43*/     Public StaticString Encrypt (String data)throwsException {byte[] bt =Encrypt (Data.getbytes (ENCODE), Defaultkey.getbytes (ENCODE)); String STRs=NewBase64encoder (). Encode (BT); returnSTRs; }    /*** Decrypt with default key * *@returnString *@authorLifq * @date 2015-3-17 pm 02:49:52*/     Public StaticString decrypt (String data)throwsIOException, Exception {if(Data = =NULL)            return NULL; Base64decoder Decoder=NewBase64decoder (); byte[] buf =decoder.decodebuffer (data); byte[] bt =Decrypt (buf, Defaultkey.getbytes (ENCODE)); return NewString (BT, ENCODE); }    /*** Description is encrypted based on key value * *@paramData *@paramKey * Encryption key byte array *@return     * @throwsException*/     Public StaticString Encrypt (string data, string key)throwsException {byte[] bt =Encrypt (Data.getbytes (ENCODE), Defaultkey.getbytes (ENCODE)); String STRs=NewBase64encoder (). Encode (BT); returnSTRs; }    /*** Description is decrypted based on key value * *@paramData *@paramKey * Encryption key byte array *@return     * @throwsIOException *@throwsException*/     Public StaticString decrypt (string data, string key)throwsIOException, Exception {if(Data = =NULL)            return NULL; Base64decoder Decoder=NewBase64decoder (); byte[] buf =decoder.decodebuffer (data); byte[] bt =Decrypt (buf, Key.getbytes (ENCODE)); return NewString (BT, ENCODE); }    /*** Description is encrypted based on key value * *@paramData *@paramKey * Encryption key byte array *@return     * @throwsException*/    Private Static byte[] Encrypt (byte[] Data,byte[] key)throwsException {//generate a trustworthy random number sourceSecureRandom sr =NewSecureRandom (); //Create a Deskeyspec object from the original key dataDeskeyspec DKs =NewDeskeyspec (key); //Create a key factory and use it to convert Deskeyspec to Secretkey objectsSecretkeyfactory keyfactory =secretkeyfactory.getinstance (DES); Secretkey SecureKey=Keyfactory.generatesecret (DKS); //The cipher object actually completes the cryptographic operationCipher Cipher =cipher.getinstance (DES); //initializing a Cipher object with a keyCipher.init (Cipher.encrypt_mode, SecureKey, SR); returncipher.dofinal (data); }    /*** Description is decrypted based on key value * *@paramData *@paramKey * Encryption key byte array *@return     * @throwsException*/    Private Static byte[] Decrypt (byte[] Data,byte[] key)throwsException {//generate a trustworthy random number sourceSecureRandom sr =NewSecureRandom (); //Create a Deskeyspec object from the original key dataDeskeyspec DKs =NewDeskeyspec (key); //Create a key factory and use it to convert Deskeyspec to Secretkey objectsSecretkeyfactory keyfactory =secretkeyfactory.getinstance (DES); Secretkey SecureKey=Keyfactory.generatesecret (DKS); //The cipher object actually completes the decryption operationCipher Cipher =cipher.getinstance (DES); //initializing a Cipher object with a keyCipher.init (Cipher.decrypt_mode, SecureKey, SR); returncipher.dofinal (data); }}

Java implementation DES encryption and decryption algorithm

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.