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