Java Encryption Algorithm-des

Source: Internet
Author: User

 Public classDesutil {Private StaticString Strdefaultkey ="13456789ABCD";//the default key    PrivateCipher Encryptcipher =NULL;//Encrypt    PrivateCipher Decryptcipher =NULL;//decryption     Public StaticString Bytearr2hexstr (byte[] ARRB) {        intLen =arrb.length; StringBuffer SB=NewStringBuffer (len *2);  for(inti =0; i < Len; i++) {            intInttmp =Arrb[i];  while(Inttmp <0) {inttmp= Inttmp + the; }            if(Inttmp < -) {sb.append ("0"); } sb.append (Integer.tostring (inttmp, -)); }        returnsb.tostring (); }     Public Static byte[] HEXSTR2BYTEATRR (String strin) {byte[] ARRB =strin.getbytes (); intLen =arrb.length; byte[] Arro =New byte[Len/2];  for(inti =0; i < Len; i = i +2) {String strt=NewString (ARRB, I,2); Arro[i/2] = (byte) Integer.parseint (STRT, -); }        returnArro; }    //Public Desutils () throws Exception {this (Strdefaultkey); }     PublicDesutil () throws Exception { This(Strdefaultkey); }     PublicDesutil (String strdefaultkey2) throws Exception {Security.addprovider (NewSunjce ()); Key Key=GetKey (Strdefaultkey2.getbytes ()); Encryptcipher= Cipher.getinstance ("DES");        Encryptcipher.init (Cipher.encrypt_mode, key); Decryptcipher= Cipher.getinstance ("DES");    Decryptcipher.init (Cipher.decrypt_mode, key); }     Public byte[] Encrypt (byte[] arrb) throws Exception {returnencryptcipher.dofinal (ARRB); }     Publicstring Encrypt (String str) throws Exception {returnBytearr2hexstr (Encrypt (Str.getbytes ())); }     Public byte[] Decrypt (byte[] arrb) throws Exception {returndecryptcipher.dofinal (ARRB); }     Publicstring Decrypt (String Strin) throws Exception {return NewString (Decrypt (HEXSTR2BYTEATRR (Strin))); }    PrivateKey GetKey (byte[] arrbtmp) {        byte[]arrb=New byte[8];  for(intI=0; i<arrbtmp.length&&i<arrb.length;i++) {Arrb[i]=Arrbtmp[i]; }        //Generate keyKey key=NewJavax.crypto.spec.SecretKeySpec (ARRB,"DES"); returnkey;}  Public Static voidMain (string[] args) throws Exception {String test="ksjdfksjdflk;342315.,,52256266322555124123225555"; Desutil des=NewDesutil ("kj;skldjfklsdj,.-=sdfjj;f ' wons2862485");//Custom Key valueString B = des.encrypt (test);//EncryptSystem. out. println ("after encryption:"+b); String a=des.encrypt (test); //DateFormat f = new SimpleDateFormat ("Yyyy-mm-dd HH:ss:mm SSS");System. out. println ("after decryption"+des.decrypt (a));//decryption                    }    }

Java Encryption Algorithm-des

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.