Package Com.*;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;public class Desutil {private Key key;public DESUtil () {}public Desutil (String str) {setkey (str);//Generate Key}public key GetKey () {return key;} public void Setkey (key key) {This.key = key;} /** * generates KEY */public void Setkey (String strkey) According to parameters {try {keygenerator _generator = keygenerator.getinstance ("DES"); _gene Rator.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;} /** * Decryption with string cipher input, string plaintext output * * @param strmi * @return */public string decryptstr (String strmi) {Base64decoder ba SE64DE = new Base64decoder (); byte[] byteming = null;byte[] Bytemi = null; String strming = ""; try {Bytemi = Base64de.decodebuffer (strmi); byteming = This.decryptbyte (Bytemi); strming = new String (b Yteming, "UTF8");} catch (Exception e) {throw new RuntimeException ("Error Initializing Sqlmap class. Cause: "+ e);} finally {base64de = null;byteming = Null;bytemi = null;} return strming;} /** * Encryption with byte[] Clear text input, byte[] ciphertext output * * @param byteS * @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 new RuntimeException ("Error Initializing Sqlmap class. Cause: "+ e);} finally {cipher = null;} return Bytefina;} /** * Decrypt with byte[] ciphertext input, byte[] Clear Text Output * * @param byted * @return */private byte[] Decryptbyte (byte[] byted) {Cipher Ciphe r;byte[] Bytefina = null;try {cipher = Cipher.getinstance ("DES"); 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;} /** * file files are encrypted and saved in the destination file destfile * * @param file * Files to be encrypted such as C:/test/srcfile.txt * @param destfile * File names such as c:/are stored after encryption After encrypting file. txt */public void EncryptFile (string file, String destfile) throws Exception {Cipher Cipher = cipher.getinstance ("D ES ");//Cipher.init (Cipher.encrypt_mode, GetKey ()); Cipher.init (cipher.encryPt_mode, This.key); InputStream is = new FileInputStream (file); OutputStream out = new FileOutputStream (destfile); CipherInputStream cis = new CipherInputStream (IS, cipher); byte[] buffer = new Byte[1024];int R;while ((r = cis.read (buffer )) > 0) {out.write (buffer, 0, R);} Cis.close (); Is.close (); Out.close ();} /** * file uses DES algorithm to decrypt files * * @param file * Encrypted files such as c:/ After encrypting file. txt * * @param destfile * Decrypted file name such as c:/test/decrypted after file. txt */public void DecryptFile (string file, string Dest) throws Exception {Cipher Cipher = cipher.getinstance ("DES"); Cipher.init (Cipher.decrypt_mode, this.key); I Nputstream is = new FileInputStream (file), outputstream out = new FileOutputStream (dest); CipherOutputStream cos = new CipherOutputStream (out, cipher); byte[] buffer = new Byte[1024];int R;while ((r = is.read (buff ER)) >= 0) {cos.write (buffer, 0, R);} Cos.close (); Out.close (); Is.close ();} public static void Main (string[] args) throws Exception {Desutil des = new Desutil ("FDSFSFSF");//des encrypted File//Des.encryptfile ("G:/test.doc", "g:/ Encrypt Test.doc ");//DES decryption File//Des.decryptfile (" g:/ Encrypt Test.doc "," g:/ Decrypt Test.doc "); String str1 = "string to encrypt test";//des encryption string str2 = Des.encryptstr (STR1);//des decryption strings string destr = Des.decryptstr (str 2); System.out.println ("Before encryption:" + str1); SYSTEM.OUT.PRINTLN ("After encryption:" + str2); System.out.println ("After decryption:" + Destr);}}
Java DES encryption and decryption