Android--des encryption

Source: Internet
Author: User

Base64.java

ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.OutputStream; Public classBase64 {Private Static Final Char[] Legalchars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/". ToCharArray (); /*** data[] to encode * *@paramData *@return     */     Public StaticString Encode (byte[] data) {                intStart = 0; intLen =data.length; StringBuffer buf=NewStringBuffer (Data.length * 3/2); intEnd = Len-3; inti =start; intn = 0;  while(I <=end) {                    intD = ((((int) Data[i]) & 0x0ff) << 16)                            | ((((int) Data[i + 1]) & 0X0FF) << 8)                            | (((int) Data[i + 2]) & 0X0FF); Buf.append (legalchars[(d>>) & 63]); Buf.append (legalchars[(d>>) & 63]); Buf.append (legalchars[(d>> 6) & 63]); Buf.append (Legalchars[d& 63]); I+ = 3; if(n++ >= 14) {n= 0; Buf.append (" "); }                }                 if(i = = start + len-2) {                    intD = ((((int) Data[i]) & 0x0ff) << 16)                            | ((((int) Data[i + 1]) & 255) << 8); Buf.append (legalchars[(d>>) & 63]); Buf.append (legalchars[(d>>) & 63]); Buf.append (legalchars[(d>> 6) & 63]); Buf.append ("="); } Else if(i = = start + len-1) {                    intD = (((int) Data[i]) & 0x0ff) << 16; Buf.append (legalchars[(d>>) & 63]); Buf.append (legalchars[(d>>) & 63]); Buf.append ("=="); }                 returnbuf.tostring (); }    Private Static intDecodeCharc) {if(c >= ' A ' && C <= ' Z ')            return((int) c)-65; Else if(c >= ' a ' && c <= ' z ')            return((int) c)-97 + 26; Else if(c >= ' 0 ' && C <= ' 9 ')            return((int) c)-48 + 26 + 26; Else            Switch(c) { Case+:                return62;  Case‘/‘:                return63;  Case=:                return0; default:                Throw NewRuntimeException ("Unexpected code:" +c); }    }    /*** Decodes the given BASE64 encoded String to a new byte array.     The byte * Array holding the decoded data is returned. */     Public Static byte[] Decode (String s) {bytearrayoutputstream bos=NewBytearrayoutputstream (); Try{decode (S, BOS); } Catch(IOException e) {Throw Newruntimeexception (); }        byte[] Decodedbytes =Bos.tobytearray (); Try{bos.close (); Bos=NULL; } Catch(IOException ex) {System.err.println ("Error while decoding BASE64:" +ex.tostring ()); }        returndecodedbytes; }    Private Static voidDecode (String s, OutputStream OS)throwsIOException {inti = 0; intLen =s.length ();  while(true) {             while(I < len && S.charat (i) <= ") I++; if(i = =len) Break; intTri = (decode (S.charat (i)) << 18)                    + (Decode (S.charat (i + 1)) << 12)                    + (Decode (S.charat (i + 2)) << 6)                    + (Decode (S.charat (i + 3))); Os.write (Tri>>) & 255); if(S.charat (i + 2) = = ' = ')                 Break; Os.write (Tri>> 8) & 255); if(S.charat (i + 3) = = ' = ')                 Break; Os.write (Tri& 255); I+ = 4; }    }}

Des.java

 Packagecn.crane.game.puzzle.utils.security;ImportJavax.crypto.Cipher;ImportJavax.crypto.spec.IvParameterSpec;ImportJavax.crypto.spec.SecretKeySpec; Public classDES {Private Static byte[] IV = {1, 2, 3, 4, 5, 6, 7, 8 }; /*** Encryption * *@paramencryptstring *@paramEncryptkey *@return     * @throwsException*/     Public Staticstring Encryptdes (String encryptstring, String encryptkey) {//Ivparameterspec zeroiv = new Ivparameterspec (new byte[8]);        Try{ivparameterspec Zeroiv=NewIvparameterspec (iv); Secretkeyspec Key=NewSecretkeyspec (Encryptkey.getbytes (), "DES"); Cipher Cipher= Cipher.getinstance ("des/cbc/pkcs5padding");            Cipher.init (Cipher.encrypt_mode, Key, ZEROIV); byte[] EncryptedData =cipher.dofinal (Encryptstring.getbytes ()); returnBase64.encode (EncryptedData); } Catch(Exception e) {e.printstacktrace (); }        return""; }    /*** Decryption * *@paramdecryptstring *@paramDecryptkey *@return     * @throwsException*/     Public Staticstring Decryptdes (String decryptstring, String decryptkey) {Try {            byte[] Bytemi =Base64.decode (decryptstring); Ivparameterspec Zeroiv=NewIvparameterspec (iv); //Ivparameterspec zeroiv = new Ivparameterspec (new byte[8]);Secretkeyspec key =NewSecretkeyspec (Decryptkey.getbytes (), "DES"); Cipher Cipher= Cipher.getinstance ("des/cbc/pkcs5padding");            Cipher.init (Cipher.decrypt_mode, Key, ZEROIV); byteDecrypteddata[] =cipher.dofinal (Bytemi); return NewString (Decrypteddata); } Catch(Exception e) {e.printstacktrace (); }        return""; }}

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.