The Android client and server side Pass DES encryption authentication

Source: Internet
Author: User
Tags crypt

Reprint Address: http://blog.csdn.net/spring21st/article/details/6730283

Since Android apps do not have a session mechanism like web development, there is no way to get the client login state by using PHPSESSID.

In this case, how to use a "handshake" method when the server side obtains the user's login status and maintains it after the user logs on.

Each cell phone has its own IMEI number, then can you pass this logo to do certification?

After trial, the answer is YES!

When the client requests the server side, the request parameter is an encrypted string of IMEI (param 1) and Imei&ua (param 2), and the server decrypts the two parameters passed by the client, which is the same as the two IMEI value. If the same, the token is returned to the client, and each time the client requests the server side, the token is carried. This allows the server to get the user's login status.

Here, I adopt the DES encryption method, because PHP and Java des encryption is different, so separate processing:

ImportJava.security.Key; ImportJava.security.SecureRandom; ImportJava.security.spec.AlgorithmParameterSpec; ImportJavax.crypto.Cipher; Importjavax.crypto.SecretKeyFactory; ImportJavax.crypto.spec.DESKeySpec; ImportJavax.crypto.spec.IvParameterSpec; Importcom.sun.org.apache.xml.internal.security.utils.Base64;  Public classDes2 { Public Static FinalString algorithm_des = "Des/cbc/pkcs5padding"; /*** des algorithm, encryption * *@paramdata to encrypt String *@paramkey Encryption private key, length cannot be less than 8 bits *@returnencrypted byte array, commonly combined with BASE64 encoding using *@throwsCryptexception Exception*/       Public StaticString encode (string key,string data)throwsException {returnencode (Key, data.getbytes ()); }      /*** des algorithm, encryption * *@paramdata to encrypt String *@paramkey Encryption private key, length cannot be less than 8 bits *@returnencrypted byte array, commonly combined with BASE64 encoding using *@throwsCryptexception Exception*/       Public Staticstring encode (String key,byte[] data)throwsException {Try{deskeyspec DKs=NewDeskeyspec (Key.getbytes ()); Secretkeyfactory keyfactory= Secretkeyfactory.getinstance ("DES"); //the length of the key cannot be less than 8 bit bytesKey Secretkey =Keyfactory.generatesecret (DKS); Cipher Cipher=cipher.getinstance (algorithm_des); Ivparameterspec IV=NewIvparameterspec ("12345678". GetBytes ()); Algorithmparameterspec Paramspec=IV;                            Cipher.init (Cipher.encrypt_mode, Secretkey,paramspec); byte[] bytes =cipher.dofinal (data); returnBase64.encode (bytes); } Catch(Exception e) {Throw NewException (e); }      }        /*** des algorithm, decryption * *@paramdata to decrypt String *@paramkey decrypts the private key, the length cannot be less than 8 bits *@returndecrypted array of bytes *@throwsException Exception*/       Public Static byte[] Decode (String key,byte[] data)throwsException {Try{securerandom SR=NewSecureRandom (); Deskeyspec DKs=NewDeskeyspec (Key.getbytes ()); Secretkeyfactory keyfactory= Secretkeyfactory.getinstance ("DES"); //the length of the key cannot be less than 8 bit bytesKey Secretkey =Keyfactory.generatesecret (DKS); Cipher Cipher=cipher.getinstance (algorithm_des); Ivparameterspec IV=NewIvparameterspec ("12345678". GetBytes ()); Algorithmparameterspec Paramspec=IV;              Cipher.init (Cipher.decrypt_mode, Secretkey,paramspec); returncipher.dofinal (data); } Catch(Exception e) {Throw NewException (e); }      }            /*** Get the encoded value *@paramKey *@paramData *@return      * @throwsException*/       Public Staticstring Decodevalue (string key,string data) {byte[] datas; String value=NULL; Try {              if(System.getproperty ("Os.name")! =NULL&& (System.getproperty ("Os.name"). Equalsignorecase ("SunOS") | | System.getproperty ("Os.name"). Equalsignorecase ("Linux"))) {datas=decode (Key, Base64.decode (data)); }              Else{datas=decode (Key, Base64.decode (data)); } Value=NewString (datas); } Catch(Exception e) {value= ""; }          returnvalue; }        /*** Test *@paramkey:12345678*/       Public Static voidMain (string[] args)throwsException {System.out.println ("Ming: Cychai:" + des2.encode ("12345678", "Cychai")); }  }  

Php:

classDES {var $key; var $iv;//Offset Amount      functionDES ($key,$iv=0)      {          $this-Key=$key; if($iv= = 0)          {              $this->iv =$key; }          Else           {              $this->iv =$iv; }      }        //Encrypt    functionEncrypt$str)      {                 $size= Mcrypt_get_block_size (Mcrypt_des,MCRYPT_MODE_CBC); $str=$this->pkcs5pad ($str,$size ); $data=MCRYPT_CBC (Mcrypt_des,$this-Key,$str, Mcrypt_encrypt,$this-IV); //$data =strtoupper (Bin2Hex ($data));//return uppercase hexadecimal string        return Base64_encode($data); }            //decryption    functionDecrypt$str)      {          $str=Base64_decode($str); //$strBin = $this->hex2bin (strtolower ($STR));         $str= MCRYPT_CBC (Mcrypt_des,$this-Key,$str, Mcrypt_decrypt,$this-IV); $str=$this->pkcs5unpad ($str ); return $str; }        functionHex2bin ($hexData)      {          $binData= "";  for($i= 0;$i<strlen($hexData);$i+ = 2)          {              $binData.=CHR(Hexdec(substr($hexData,$i, 2))); }          return $binData; }        functionPkcs5pad ($text,$blocksize)      {          $pad=$blocksize- (strlen($text) %$blocksize); return $text.str_repeat(CHR($pad),$pad ); }        functionPkcs5unpad ($text)      {          $pad=Ord($text{strlen($text)-1} ); if($pad>strlen($text ))              return false; if(strspn($text,CHR($pad),strlen($text) -$pad) !=$pad)              return false; return substr($text, 0,-1 *$pad ); }  }  $str= ' abc '; $key= ' 12345678 '; $crypt=NewDES ($key); $mstr=$crypt->encrypt ($str); $str=$crypt->decrypt ($mstr); Echo  $str. ' <=> '.$mstr;

The Android client and server side Pass DES encryption authentication

Related Article

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.