Des encryption and decryption (applicable to Windows and Linux systems) to prevent decryption failures in Linux

Source: Internet
Author: User
Package COM. lasun. util; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. inputstream; import Java. io. outputstream; import Java. security. key; import Java. security. securerandom; import javax. crypto. cipher; import javax. crypto. cipherinputstream; import javax. crypto. cipheroutputstream; import javax. crypto. keygenerator; import sun. misc. base64decoder; import sun. misc. base64encoder; import COM. lasun. core. services. parametermanager; public class desutil {key; Public desutil () {} public desutil (string Str) {setkey (STR); // generate key} Public Key getkey () {return key;} public void setkey (Key key) {This. key = key;}/*** generate key */Public void setkey (string strkey) {try {keygenerator _ generator = keygenerator. getinstance ("des"); // prevents random key securerandom = securerandom in Linux. getinstance ("sha1prng"); securerandom. setseed (strkey. getbytes (); _ generator. init (56, securerandom); this. key = _ generator. generatekey (); _ generator = NULL;} catch (exception e) {Throw new runtimeexception ("error initializing sqlmap class. cause: "+ E) ;}}/*** generate key * // * Public void setkey (string strkey) {try {keygenerator _ generator = keygenerator. getinstance ("des"); _ generator. init (New securerandom (strkey. getbytes (); this. key = _ generator. generatekey (); _ generator = NULL;} catch (exception e) {Throw new runtimeexception ("error initializing sqlmap class. cause: "+ E) ;}} * // *** encrypt string plaintext input, string ciphertext output */Public String encryptstr (string strming) {byte [] bytemi = NULL; byte [] byteming = NULL; string strmi = ""; base64encoder base64en = new base64encoder (); try {byteming = strming. getbytes ("utf8"); bytemi = This. encryptbyte (byteming); strmi = base64en. encode (bytemi);} catch (exception e) {Throw new runtimeexception ("error initializing sqlmap class. cause: "+ E) ;}finally {base64en = NULL; byteming = NULL; bytemi = NULL;} return strmi ;}/ *** decrypt the string ciphertext input, string plaintext output ** @ Param strmi * @ return */Public String decryptstr (string strmi) {base64decoder base64de = new base64decoder (); byte [] byteming = NULL; byte [] bytemi = NULL; string strming = ""; try {bytemi = base64de. decodebuffer (strmi); byteming = This. decryptbyte (bytemi); strming = new string (byteming, "utf8");} catch (exception e) {Throw new runtimeexception ("error initializing sqlmap class. cause: "+ E);} finally {base64de = NULL; byteming = NULL; bytemi = NULL;} return strming;}/*** encrypt the input in byte [] plaintext, byte [] ciphertext output ** @ Param bytes * @ return */private byte [] encryptbyte (byte [] bytes) {byte [] bytefina = NULL; cipher; try {cipher = cipher. getinstance ("des/ECB/pkcs5padding"); cipher. init (cipher. encrypt_mode, key); bytefina = cipher. dofinal (bytes);} catch (exception e) {Throw new runtimeexception ("error initializing sqlmap class. cause: "+ E) ;}finally {cipher = NULL;} return bytefina;}/*** decrypt the ciphertext input in byte, output ** @ Param byted * @ return */private byte [] decryptbyte (byte [] byted) {cipher; byte [] bytefina = NULL; try {cipher = cipher. getinstance ("des/ECB/pkcs5padding"); cipher. init (cipher. decrypt_mode, key); bytefina = cipher. dofinal (byted);} catch (exception e) {Throw new runtimeexception ("error initializing sqlmap class. cause: "+ E);} finally {cipher = NULL;} return bytefina;} public static void main (string [] ARGs) throws exception {desutil des = new desutil ("123456"); // DES encrypted file // des. encryptfile ("G:/test.doc", "G:/encrypt test.doc"); // des decrypts the file // des. decryptfile ("G:/encrypted test.doc", "G:/decrypted test.doc"); string str1 = "jui8qayupolicyhqwsh"; // DES encryption string str2 = des. encryptstr (str1); // des decryption string destr = des. decryptstr (str2); system. out. println ("before encryption:" + str1); system. out. println ("encrypted:" + str2); system. out. println ("encrypted length:" + str2.length (); system. out. println ("decrypted:" + destr );}}


Note: the blue part (commented out) code is easy to use in windows, but in Linux it will lead to different keys generated each time, that is to say, encryption and decryption will save, on the Internet, because the two operating systems use different methods to complete supplementary bytes by default, this is the case;

The red part of the code prevents random generation of keys in Linux, which is common in two operating systems.

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.