Both PHP and. NET general cryptographic decryption function classes use 3DES encryption and decryption.

Source: Internet
Author: User

The following is the PHP code

<preclass=php name= "Code" >&nbsp;</pre><preclass=php name= "code" >php plus decryption function: </pre><preclass=php name= "Code" >&nbsp;</pre><preclass=php name= "Code" >functionEncrypt$string) {        //Key file for encryption        $key= "XXXXXXXX"; //Encryption Method        $cipher _alg=Mcrypt_tripledes; //initializing vectors to increase security        $iv= Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher _alg, MCRYPT_MODE_ECB),Mcrypt_rand); //Start Encryption        $encrypted _string= Mcrypt_encrypt ($cipher _alg,$key,$string, MCRYPT_MODE_ECB,$iv); return Base64_encode($encrypted _string);//Convert to 16//Return $encrypted _string;    }        functionDecrypt$string) {            $string=Base64_decode($string); //Key file for encryption            $key= "XXXXXXXX"; //Encryption Method            $cipher _alg=Mcrypt_tripledes; //initializing vectors to increase security            $iv= Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher _alg, MCRYPT_MODE_ECB),Mcrypt_rand); //Start Decryption            $decrypted _string= Mcrypt_decrypt ($cipher _alg,$key,$string, MCRYPT_MODE_ECB,$iv); return Trim($decrypted _string); }</PRE>

The following is the. NET code

usingSystem;usingSystem.Security.Cryptography;usingSystem.Text;/**//// <SUMMARY>///a summary description of the crypto3des. /// </SUMMARY> Public classcrypto3des{ Publiccrypto3des () {//        //TODO: Add constructor logic here//    }    PrivateSystem.Text.Encoding Encoding; /**//// <SUMMARY>    ///get the secret key/// </SUMMARY>     Public stringKey {Get        {            return "xxxxxxxx"; }    }    /**//// <SUMMARY>    ///Gets or sets the encoding of the encryption decryption/// </SUMMARY>     PublicSystem.Text.Encoding Encoding {Get        {            if(Encoding = =NULL) {Encoding=System.Text.Encoding.UTF8; }            returnencoding; }        Set{encoding=value; }    }    /**//// <SUMMARY>    ///Encrypt/// </SUMMARY>    /// <param name= "strstring" ></PARAM>    /// <param name= "strkey" ></PARAM>    /// <param name= "encoding" ></PARAM>    /// <RETURNS></RETURNS>     Public stringEncrypt3des (stringstrstring) {DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key= Encoding.GetBytes ( This.        Key); Des. Mode=CIPHERMODE.ECB; Des. Padding=Paddingmode.zeros; ICryptoTransform desencrypt=DES.        CreateEncryptor (); byte[] Buffer =encoding.        GetBytes (strstring); returnConvert.tobase64string (Desencrypt.transformfinalblock (Buffer,0, buffer.length)); }    /**//// <SUMMARY>    ///decryption/// </SUMMARY>    /// <param name= "strstring" ></PARAM>    /// <param name= "strkey" ></PARAM>    /// <RETURNS></RETURNS>     Public stringDecrypt3des (stringstrstring) {DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key= Encoding.UTF8.GetBytes ( This.        Key); Des. Mode=CIPHERMODE.ECB; Des. Padding=Paddingmode.zeros; ICryptoTransform Desdecrypt=DES.        CreateDecryptor (); byte[] Buffer =convert.frombase64string (strstring); returnUTF8Encoding.UTF8.GetString (Desdecrypt.transformfinalblock (Buffer,0, buffer.length)); }}

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.