Android----Java des encryption Algorithm Tool class

Source: Internet
Author: User

1  Packageentity;2 3 ImportJavax.crypto.Cipher;4 ImportJavax.crypto.SecretKey;5 Importjavax.crypto.SecretKeyFactory;6 ImportJavax.crypto.spec.DESKeySpec;7 ImportJavax.crypto.spec.IvParameterSpec;8 9  Public classDes {Ten     Private byte[] deskey;  One        A      -     //Decrypt Data -      Public StaticString decrypt (string message,string key)throwsException { the              -             byte[] Bytesrc =converthexstring (message);  -Cipher Cipher = cipher.getinstance ("des/cbc/pkcs5padding");  -Deskeyspec Deskeyspec =NewDeskeyspec (Key.getbytes ("UTF-8"));  +Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");  -Secretkey Secretkey =Keyfactory.generatesecret (DESKEYSPEC);  +Ivparameterspec IV =NewIvparameterspec (Key.getbytes ("UTF-8"));  A                     at Cipher.init (Cipher.decrypt_mode, Secretkey, iv);  -               -             byte[] Retbyte =cipher.dofinal (BYTESRC);  -             return NewString (Retbyte);  -     }    -    in      Public Static byte[] Encrypt (String message, string key) -             throwsException { toCipher Cipher = cipher.getinstance ("des/cbc/pkcs5padding");  +    -Deskeyspec Deskeyspec =NewDeskeyspec (Key.getbytes ("UTF-8"));  the    *Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");  $Secretkey Secretkey =Keyfactory.generatesecret (DESKEYSPEC); Panax NotoginsengIvparameterspec IV =NewIvparameterspec (Key.getbytes ("UTF-8"));  - Cipher.init (Cipher.encrypt_mode, Secretkey, iv);  the    +         returnCipher.dofinal (Message.getbytes ("UTF-8"));  A     }    the         +      Public Static byte[] converthexstring (String ss) -     {     $     byteDigest[] =New byte[Ss.length ()/2];  $      for(inti = 0; i < digest.length; i++)     -     {     -String byteString = ss.substring (2 * I, 2 * i + 2);  the     intBytevalue = Integer.parseint (byteString, 16);  -Digest[i] = (byte) Bytevalue; Wuyi     }     the    -     returnDigest;  Wu     }     -      About      Public StaticString tohexstring (byteb[]) {    $StringBuffer hexstring =NewStringBuffer ();  -          for(inti = 0; i < b.length; i++) {    -String plaintext = integer.tohexstring (0xFF &B[i]);  -             if(Plaintext.length () < 2)    Aplaintext = "0" +plaintext;  + hexstring.append (plaintext);  the         }    -             $         returnhexstring.tostring ();  the     }    the    the    the      Public Static voidMain (string[] args)throwsException { -String key = "12345678";  inString value= "test1234";  theString Jiami=java.net.urlencoder.encode (value, "Utf-8"). toLowerCase ();  the             AboutSYSTEM.OUT.PRINTLN ("Encrypt data:" +Jiami);  theString a=tohexstring (Encrypt (Jiami, key)). toUpperCase ();  theSYSTEM.OUT.PRINTLN ("Encrypted data is:" +a);  the          +          -String B=java.net.urldecoder.decode (Decrypt (A,key), "Utf-8") ;  theSYSTEM.OUT.PRINTLN ("Decrypted data:" +b); Bayi    the     }    the    -         -      the}

Android----Java des encryption 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.