Use des to encrypt and decrypt files in Android or Java

Source: Internet
Author: User

Recently, in my project, I needed to encrypt some SQL statements on the android client. I used DES encryption, and the result encryption appeared.

javax.crypto.BadPaddingException: Given final block not properly padded

Such an error, or the garbled problem, is very tangled! When I checked some information, it may be a key issue or a code issue. I found that the key is correct, that is, when the key is created, the obtained byte [] array has some processing operations. The complete code is as follows:

 

Package COM. spring. sky. util; import Java. io. bufferedreader; import Java. io. bufferedwriter; 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. key; import javax. crypto. cipher; import javax. crypto. cipherinputstream; import J Avax. crypto. spec. secretkeyspec; import Org. apache. HTTP. entity. inputstreamentity;/***** des file encryption & Decryption <br> * supports file intercommunication between Android and Windows * @ author spring. sky * Email: vipa1888@163.com * QQ: 840950105 **/public class filedes {/** key of encryption/Decryption */private key mkey; /** decrypted password */private cipher mdecryptcipher;/** encrypted password */private cipher mencryptcipher; Public filedes (string key) throws exception {initkey (key ); initcipher ();}/** * Create a key * @ Param keyrule */Public void initkey (string keyrule) {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];} mkey = new secretkeyspec (bytetemp, "des ");} /***** initialize the loading password * @ throws exception */private void initcipher () throws exception {mencryptci Pher = cipher. getinstance ("des"); mencryptcipher. init (cipher. encrypt_mode, mkey); mdecryptcipher = cipher. getinstance ("des"); mdecryptcipher. init (cipher. decrypt_mode, mkey);}/*** encrypted file * @ Param in * @ Param savepath: Location of the encrypted file */Public void doencryptfile (inputstream in, string savepath) {If (in = NULL) {system. out. println ("inputstream is null"); return;} Try {cipherinputstream CIN = new cipherinputstream (in, m Encryptcipher); outputstream OS = new fileoutputstream (savepath); byte [] bytes = new byte [1024]; int Len =-1; while (LEN = cin. read (bytes)> 0) {OS. write (bytes, 0, Len); OS. flush ();} OS. close (); cin. close (); In. close (); system. out. println ("encrypted");} catch (exception e) {system. out. println ("encryption failed"); E. printstacktrace () ;}/ *** encrypted file ** @ Param filepath the file path to be encrypted * @ Param savepath the location after encryption * @ throws filenotfoundexcep Tion */Public void doencryptfile (string filepath, string savepath) throws filenotfoundexception {doencryptfile (New fileinputstream (filepath), savepath );} /*** decrypt the file * @ Param in */Public void dodecryptfile (inputstream in) {If (in = NULL) {system. out. println ("inputstream is null"); return;} Try {cipherinputstream CIN = new cipherinputstream (in, mdecryptcipher); bufferedreader reader = new bufferedreader (New I Nputstreamreader (CIN); string line = NULL; while (line = reader. Readline ())! = NULL) {system. out. println (line);} reader. close (); cin. close (); In. close (); system. out. println ("decrypted");} catch (exception e) {system. out. println ("decryption failed"); E. printstacktrace () ;}/ *** decrypt file * @ Param filepath file path * @ throws exception */Public void dodecryptfile (string filepath) throws exception {dodecryptfile (New fileinputstream (filepath);} public static void main (string [] ARGs) throws exception {filedes = new filedes ("Spring. sky "); filedes. doencryptfile ("D:/a.txt", "d:/B"); // encrypt filedes. dodecryptfile ("D:/B"); // decryption }}

 

 

The above code is successfully tested on the Android 1.6 and Java platforms respectively. There is no problem. I just want to encapsulate it based on different requirements and hope to help you, let everyone avoid detours!

Related Article

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.