Java DES Test

Source: Internet
Author: User
Tags base64 decrypt

 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

Related Article

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.