Packagecom.des.test;Importjava.io.UnsupportedEncodingException;Importjava.security.InvalidKeyException;Importjava.security.NoSuchAlgorithmException;Importjava.security.spec.InvalidKeySpecException;ImportJava.security.spec.KeySpec;ImportJava.util.logging.Level;ImportJava.util.logging.Logger;Importjavax.crypto.BadPaddingException;ImportJavax.crypto.Cipher;Importjavax.crypto.IllegalBlockSizeException;Importjavax.crypto.NoSuchPaddingException;ImportJavax.crypto.SecretKey;Importjavax.crypto.SecretKeyFactory;ImportJavax.crypto.spec.DESedeKeySpec;Importorg.apache.commons.codec.binary.Base64;/** * * @authory*/ Public classDesutil {Private Static FinalString Charset_type = "UTF-8"; Private Static FinalString Encrypt_key = "z7ckbccc5kwlzqjbunr1yhzg5nm"; Private Static FinalString encrypt_scheme = "Desede"; byte[] keyasbytes; Private FinalKeySpec Mykeyspec; Private Finalsecretkeyfactory mysecretkeyfactory; Private FinalCipher Cipher; Private FinalSecretkey Key; PublicDesutil ()throwsinvalidkeyexception, Unsupportedencodingexception, NoSuchAlgorithmException, Nosuchpaddingexception, INV alidkeyspecexception{keyasbytes=encrypt_key.getbytes (Charset_type); Mykeyspec=NewDesedekeyspec (keyasbytes); Mysecretkeyfactory=secretkeyfactory.getinstance (Encrypt_scheme); Cipher=cipher.getinstance (Encrypt_scheme); Key=Mysecretkeyfactory.generatesecret (MYKEYSPEC); } Publicstring Encrypt (String unencryptedstring) {string encryptedstring=NULL; Try{cipher.init (Cipher.encrypt_mode, key); byte[] plaintext =unencryptedstring.getbytes (Charset_type); byte[] Encryptedtext =cipher.dofinal (plaintext); BASE64 Base64=NewBase64 (); Encryptedstring=base64.encodeasstring (Encryptedtext); } Catch(InvalidKeyException ex) {Logger.getlogger (desutil.class. GetName ()). log (Level.severe,NULL, ex); } Catch(Unsupportedencodingexception ex) {Logger.getlogger (desutil.class. GetName ()). log (Level.severe,NULL, ex); } Catch(Illegalblocksizeexception ex) {Logger.getlogger (desutil.class. GetName ()). log (Level.severe,NULL, ex); } Catch(Badpaddingexception ex) {Logger.getlogger (desutil.class. GetName ()). log (Level.severe,NULL, ex); } returnencryptedstring; } Publicstring Decrypt (String encryptedstring) {string Decryptedtext=NULL; Try{cipher.init (Cipher.decrypt_mode, key); BASE64 Base64=NewBase64 (); byte[] Encryptedtext =Base64.decode (encryptedstring); byte[] plaintext =cipher.dofinal (Encryptedtext); Decryptedtext=NewString (Plaintext,charset_type); } Catch(InvalidKeyException ex) {Logger.getlogger (desutil.class. GetName ()). log (Level.severe,NULL, ex); } Catch(Illegalblocksizeexception ex) {Logger.getlogger (desutil.class. GetName ()). log (Level.severe,NULL, ex); } Catch(Badpaddingexception ex) {Logger.getlogger (desutil.class. GetName ()). log (Level.severe,NULL, ex); } Catch(Unsupportedencodingexception ex) {Logger.getlogger (desutil.class. GetName ()). log (Level.severe,NULL, ex); } returnDecryptedtext; } Public Static voidMain (string[] args)throwsException {String str= "Hello, this is Java DES encryption test Data"; Desutil Desutil=NewDesutil (); System.out.println ("==desc Before:" +str); String Encryptstr=Desutil.encrypt (str); System.out.println ("==desc after:" +encryptstr); System.out.println ("==descrypt:" +Desutil.decrypt (ENCRYPTSTR)); }}
Java DES Test