Java DES encryption and decryption File

Source: Internet
Author: User

In Android development or Java Development, some files are often encrypted or decrypted. Below is a method I recently tested to encrypt and decrypt the files using DES. Record it to prevent forgetting it later!

The following code is directly pasted and detailed comments are written as follows:

Package COM. spring. des; import Java. io. bufferedreader; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import Java. security. invalidkeyexception; import Java. security. key; import Java. security. nosuchalgorithmexception; import javax. crypto. c Ipher; import javax. crypto. cipherinputstream; import javax. crypto. nosuchpaddingexception; import javax. crypto. spec. secretkeyspec;/**** des file encryption and decryption ** @ author spring sky <br> * Email: vipa1888@163.com <br> * QQ: 840950105 **/public class desfileencrypt {/*** list of file paths to be encrypted */public static string [] filepath = {"D:/sasdfp. SQL "};/*** list of encrypted file paths */public static string [] outfilepath = new string [filepath. leng Th]; Private Static final string key = "spring sky"; Public desfileencrypt () {super (); getkey (key); initcipher (); // initialize the encrypted file crateencryptfile ();} Private Key key;/***** decrypt the password */private cipher cipherdecrypt;/***** encrypt the password */private cipher cipherencrypt; /*** the encrypted file is flat and recorded in the encrypted file path **/private void crateencryptfile () {string outpath = NULL; For (INT I = 0; I <filepath. length; I ++) {try {outpath = filepath [I]. substri Ng (0, filepath [I]. lastindexof (". ") + ". bin "; encrypt (filepath [I], outpath); outfilepath [I] = outpath; system. out. println (filepath [I] + "encrypted, the encrypted file is:" + outfilepath [I]);} catch (exception e) {e. printstacktrace () ;}} system. out. println ("=========================================================== ============== ");} /*** the core of the encrypted file ** @ Param file * the file to be encrypted * @ Param destfile * the encrypted file name */Public void encrypt (string file, string destfile) Throws exception {inputstream is = new fileinputstream (File); outputstream out = new fileoutputstream (destfile); cipherinputstream CIS = new cipherinputstream (is, cipherencrypt ); byte [] buffer = new byte [1024]; int R; while (r = CIs. read (buffer)> 0) {out. write (buffer, 0, R);} CIs. close (); is. close (); out. close ();}/***** decrypt the file * @ Param destfile */Public void decrypt (string destfile) {try {inputstream I S = new fileinputstream (destfile); cipherinputstream CIS = new cipherinputstream (is, cipherdecrypt); bufferedreader reader = new bufferedreader (New inputstreamreader (CIS); string line = NULL; while (line = reader. readline ())! = NULL) {system. out. println (line);} reader. close (); CIS. close (); is. close ();} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} private void initcipher () {try {// encrypted cipherencrypt = cipher. getinstance ("des"); cipherencrypt. init (cipher. encrypt_mode, this. key); // the decrypted ciphercipherdecrypt = cipher. getinstance ("des"); cipherdecrypt. init (cipher. decrypt_mode, this. key);} catch (invalidkeyexception e) {e. printstacktrace ();} catch (nosuchalgorithmexception e) {e. printstacktrace ();} catch (nosuchpaddingexception e) {e. printstacktrace () ;}}/*** customize a key ** @ Param string */Public Key getkey (string keyrule) {// key = NULL; byte [] keybyte = keyrule. getbytes (); // create an empty octal group. The default value is 0 byte [] bytetemp = new byte [8]. // convert the specified rule into an eight-digit group for (INT I = 0; I <bytetemp. length & I <keybyte. length; I ++) {bytetemp [I] = keybyte [I];} key = new secretkeyspec (bytetemp, "des"); Return key ;} /*** decrypt the password ** @ return */public cipher getcipheredcrypt () {return cipherdecrypt;}/*** encrypt the password ** @ return */public cipher getcipherencrypt () {return cipherencrypt;}/***** test encryption and decryption * @ Param ARGs */public static void main (string [] ARGs) {desfileencrypt = new desfileencrypt (); desfileencrypt. decrypt (outfilepath [0]); // decrypt the first file and test the decrypted result }}

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.