Desede Symmetric cryptographic Algorithm tool class

Source: Internet
Author: User
Tags decrypt

Using Cipher's core function, it encapsulates a cryptographic decryption tool class that can be used directly. You need to download Commons-codec-1.9.jar and import the project before you use it.

The tool classes are as follows:

 Packagecom.pcict.util.test;Importorg.apache.commons.codec.binary.Base64;ImportJavax.crypto.*;ImportJavax.crypto.spec.DESedeKeySpec;ImportJava.nio.charset.Charset;Importjava.security.InvalidKeyException;ImportJava.security.Key;Importjava.security.NoSuchAlgorithmException;ImportJava.security.SecureRandom;Importjava.security.spec.InvalidKeySpecException;Importjava.util.Arrays;/*** Desede symmetric encryption Algorithm * * @Description *@authorLJZ * @created July 31, 2015 Morning 11:30:04 *@version* @history *@see */ Public classDesedeutils {//Encryption Mode     Public Static Final intEncrypt_mode =Cipher.encrypt_mode; //Decryption Mode     Public Static Final intDecrypt_mode =Cipher.decrypt_mode; Private Static FinalString algorithm = "Desede"; Private Static FinalCharset UTF8 = Charset.forname ("UTF-8"); PrivateCipher Cipher =NULL; Private intOpmode = 0; //Initialize encryption or decryption     Public synchronized BooleanInitintmode, String key) {        if(Opmode! = 0) {            return true; }        if(Mode! = Encrypt_mode && Mode! =Decrypt_mode) {            return false; }        if(Key = =NULL||Key.isempty ()) {            return false; }        Try{cipher=cipher.getinstance (algorithm); } Catch(nosuchalgorithmexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(nosuchpaddingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            if(Cipher = =NULL) {                return false; }} Key Seckey=Getseckey (key); if(Seckey = =NULL) {            return false; }        Try{cipher.init (mode, Seckey,Newsecurerandom ()); } Catch(InvalidKeyException e) {//TODO auto-generated Catch blockE.printstacktrace (); return false; } opmode=mode; return true; }    Private StaticKey Getseckey (String key) {Secretkey SecureKey=NULL; Try {            byte[] material =arrays.copyof (Base64.decodebase64 (Key.getbytes (UTF8)),24); Desedekeyspec KeySpec=Newdesedekeyspec (material); Secretkeyfactory keyfactory=secretkeyfactory. getinstance (algorithm); SecureKey=Keyfactory.generatesecret (KEYSPEC); } Catch(InvalidKeyException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(nosuchalgorithmexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(invalidkeyspecexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnSecureKey; }    //Encrypt     Public synchronizedstring Encrypt (string data) {if(Opmode! =Encrypt_mode) {            return NULL; }        if(Data = =NULL) {            return NULL; }        byte[] Encdata =NULL; Try{encdata=cipher.dofinal (Data.getbytes (UTF8)); } Catch(illegalblocksizeexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(badpaddingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        if(Encdata = =NULL) {            return NULL; }        return NewString (Base64.encodebase64 (encdata), UTF8); }    //decryption     Public synchronizedstring Decrypt (string data) {if(Opmode! =Decrypt_mode) {            return NULL; }        if(Data = =NULL) {            return NULL; }        byte[] Decdata =NULL; Try{decdata=cipher.dofinal (Base64.decodebase64 (Data.getbytes (UTF8))); } Catch(illegalblocksizeexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(badpaddingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        if(Decdata = =NULL) {            return NULL; }        return NewString (Decdata, UTF8); }}

The test is as follows:

 Packagecom.pcict.util.test; Public classTest { Public Static voidMain (string[] args) {desedeutils encoder=Newdesedeutils (); String Key= "123456"; //with 123456 as the encryption key, in the back of the decryption will also use the key as the decryption keyEncoder.init (Desedeutils.encrypt_mode, key); String Str= Encoder.encrypt ("1");        System.out.println (str); Desedeutils Decoder=Newdesedeutils (); //call the initial dissolve secretDecoder.init (Desedeutils.decrypt_mode, key); String str1=Decoder.decrypt (str);    System.out.println (STR1); }}

Desede Symmetric cryptographic Algorithm tool class

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.