Java Encryption Algorithm series-AESEBC

Source: Internet
Author: User
Tags base64 decrypt

 Package ***;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.io.UnsupportedEncodingException;Importjava.security.MessageDigest;ImportJavax.crypto.Cipher;ImportJavax.crypto.spec.IvParameterSpec;ImportJavax.crypto.spec.SecretKeySpec;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder; Public classAESEBC {/*confirmed * Encryption key can be composed of 26 letters and numbers * Here using AES-128-CBC encryption mode, key needs to be 16 bits. */        Private StaticString skey= "1234567890123456"; Private StaticString ivparameter= "1234567890123456"; Private StaticAESEBC instance=NULL; //private static        PrivateAesebc () {} Public StaticAesebc getinstance () {if(instance==NULL) Instance=NewAesebc (); returninstance; }        //Encrypt         PublicString Encrypt (String sSrc, String Encodingformat, String SKey, String ivparameter)throwsException {Cipher Cipher= Cipher.getinstance ("aes/ecb/pkcs5padding"); byte[] Raw =skey.getbytes (); Secretkeyspec Skeyspec=NewSecretkeyspec (Raw, "AES");            Cipher.init (Cipher.encrypt_mode, Skeyspec); byte[] encrypted =cipher.dofinal (Ssrc.getbytes (Encodingformat)); return NewBase64encoder (). Encode (encrypted);//transcoding is done here using BASE64.     }        //decryption         PublicString Decrypt (String sSrc, String Encodingformat, String SKey, String ivparameter)throwsException {Try {                byte[] raw = Skey.getbytes ("ASCII"); Secretkeyspec Skeyspec=NewSecretkeyspec (Raw, "AES"); Cipher Cipher= Cipher.getinstance ("aes/ecb/pkcs5padding");                Cipher.init (Cipher.decrypt_mode, Skeyspec); byte[] encrypted1 =NewBase64decoder (). Decodebuffer (SSRC);//first decrypt with Base64 .                byte[] Original =cipher.dofinal (encrypted1); String originalstring=NewString (Original,encodingformat); returnoriginalstring; } Catch(Exception ex) {return NULL; }    }         Public Static voidMain (string[] args)throwsException {//strings that need to be encryptedString CSRC = "123456"; System.out.println ("The string before encryption is:" +CSRC); //EncryptString enstring = Aesebc.getinstance (). Encrypt (CSRC, "Utf-8", Skey,ivparameter); System.out.println ("The encrypted string is:" +enstring); System.out.println ("yxvukr45pfz0ufpbdb8/ew==". Equals (enstring)); //decryptionString destring = Aesebc.getinstance (). Decrypt (enstring, "Utf-8", Skey,ivparameter); System.out.println ("The decrypted string is:" +destring); }}

Java Encryption Algorithm series-AESEBC

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.