ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.security.Key;ImportJava.security.SecureRandom;ImportJavax.crypto.Cipher;ImportJavax.crypto.CipherInputStream;ImportJavax.crypto.CipherOutputStream;ImportJavax.crypto.KeyGenerator;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder; Public classdesutil {key key; PublicDesutil () {} Publicdesutil (String str) {setkey (str);//Generate key } PublicKey GetKey () {returnkey; } Public voidSetkey (key key) { This. Key =key; } // /**//* Generate key based on parameters// *///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);// }// } /*** Generate key based on parameters*/ Public voidSetkey (String strkey) {Try{keygenerator _generator= Keygenerator.getinstance ("DES" ); _generator.init (NewSecureRandom (Strkey.getbytes ("Utf-8"))); SecureRandom SecureRandom=Securerandom.getinstance ("Sha1prng"); Securerandom.setseed (Strkey.getbytes ()); _generator.init (56, SecureRandom); This. Key =_generator.generatekey (); _generator=NULL ; } Catch(Exception e) {Throw NewRuntimeException ("Error Initializing Sqlmap class. Cause: "+e); } } /*** Encrypt string plaintext input, string ciphertext output*/ Publicstring Encryptstr (String strming) {byte[] Bytemi =NULL ; byte[] byteming =NULL ; String Strmi= "" ; Base64encoder Base64en=NewBase64encoder (); Try{byteming= Strming.getbytes ("UTF-8" ); Bytemi= This. Encryptbyte (byteming); Strmi=Base64en.encode (Bytemi); } Catch(Exception e) {Throw NewRuntimeException ("Error Initializing Sqlmap class. Cause: "+e); } finally{Base64en=NULL ; Byteming=NULL ; Bytemi=NULL ; } returnStrmi; } /*** Decrypt with string cipher input, string plaintext output * *@paramStrmi *@return */ Publicstring Decryptstr (String strmi) {Base64decoder base64de=NewBase64decoder (); byte[] byteming =NULL ; byte[] Bytemi =NULL ; String strming= "" ; Try{Bytemi=Base64de.decodebuffer (Strmi); Byteming= This. Decryptbyte (Bytemi); Strming=NewString (byteming, "UTF-8" ); } Catch(Exception e) {Throw NewRuntimeException ("Error Initializing Sqlmap class. Cause: "+e); } finally{BASE64DE=NULL ; Byteming=NULL ; Bytemi=NULL ; } returnstrming; } /*** Encrypted with byte[] plaintext input, byte[] ciphertext output * *@paramByteS *@return */ Private byte[] Encryptbyte (byte[] ByteS) { byte[] Bytefina =NULL ; Cipher Cipher; Try{cipher= Cipher.getinstance ("DES" ); Cipher.init (cipher. Encrypt_mode, key); Bytefina=cipher.dofinal (ByteS); } Catch(Exception e) {Throw NewRuntimeException ("Error Initializing Sqlmap class. Cause: "+e); } finally{cipher=NULL ; } returnBytefina; } /*** Decryption in byte[] ciphertext input, in byte[] Clear text Output * *@parambyted *@return */ Private byte[] Decryptbyte (byte[] byted) {Cipher Cipher; byte[] Bytefina =NULL ; Try{cipher= Cipher.getinstance ("DES" ); Cipher.init (cipher. Decrypt_mode, key); Bytefina=cipher.dofinal (byted); } Catch(Exception e) {Throw NewRuntimeException ("Error Initializing Sqlmap class. Cause: "+e); } finally{cipher=NULL ; } returnBytefina; } /*** file files are encrypted and saved in the target file destfile * *@paramfile * Files to be encrypted, such as C:/test/srcfile.txt *@paramDestFile * file names such as c:/are stored after encryption Post-encrypted file. txt*/ Public voidEncryptFile (string file, String destfile)throwsException {Cipher Cipher= Cipher.getinstance ("DES" ); //Cipher.init (Cipher.encrypt_mode, GetKey ());Cipher.init (cipher. Encrypt_mode, This. Key); InputStream is=Newfileinputstream (file); OutputStream out=NewFileOutputStream (destfile); CipherInputStream CIS=NewCipherInputStream (is, cipher); byte[] buffer =New byte[1024]; intR; while((r = cis.read (buffer)) > 0) {out.write (buffer,0, R); } cis.close (); Is.close (); Out.close (); } /*** files are decrypted using DES algorithm * *@paramfile * Encrypted files such as c:/ Post-Encrypted file. txt * *@paramdestfile * Decrypted file name such as c:/test/after decryption file. txt*/ Public voidDecryptFile (string file, String dest)throwsException {Cipher Cipher= Cipher.getinstance ("DES" ); Cipher.init (cipher. Decrypt_mode, This. Key); InputStream is=Newfileinputstream (file); OutputStream out=NewFileOutputStream (dest); CipherOutputStream Cos=NewCipherOutputStream (out, cipher); byte[] buffer =New byte[1024]; intR; while((r = is.read (buffer)) >= 0) {cos.write (buffer,0, R); } cos.close (); Out.close (); Is.close (); } Public Static voidMain (string[] args)throwsException {desutil des=NewDesutil ("jrkj123"); //DES Encrypted Files//des.encryptfile ("G:/test.doc", "g:/ Encryption Test.doc "); //DES decryption File//Des.decryptfile ("g:/ Encrypt Test.doc "," g:/ Decrypt Test.doc ");String str1 = "Taskid=68&correctdate=2015-04-13&flag=1" ; //DES Encrypted StringString str2 =des.encryptstr (STR1); //DES Decryption StringString destr =des.decryptstr (STR2); System. Out. println ("Before encryption:" +str1); System. Out. println ("After encryption:" +str2); System. Out. println ("After decryption:" +destr); } }
Java-des algorithm encryption and Decryption tool class