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!