PHP implementation and C # consistent DES encryption decryption

Source: Internet
Author: User

Original: PHP implementation and C # consistent DES encryption and decryption

PHP implementation and C # consistent DES encryption decryption, can be searched from the internet to a large heap, but the test found no use. The following are the correct codes that I found after a hard struggle. I hope you can use it when the system is integrated.

Note: The length of the key is within 8 bits.

C # version des plus decryption algorithm using System;   Using System.Data;   Using System.Configuration;   Using System.Web;   Using System.Web.Security;   Using System.Web.UI;   Using System.Web.UI.WebControls;   Using System.Web.UI.WebControls.WebParts;   Using System.Web.UI.HtmlControls;   Using System.Data.SqlClient;   Using System.Security.Cryptography;   Using System.IO;   Using System.Text;    public class des{//Add decryption key private static string skey = "12345678";                   Initialization vector private static byte[] Desiv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};                   #region Desencode des encryption public static string Desencode (String ptoencrypt, String SKey)                       {Ptoencrypt = HttpContext.Current.Server.UrlEncode (Ptoencrypt);                       DESCryptoServiceProvider des = new DESCryptoServiceProvider (); byte[] Inputbytearray = encoding.getencoding ("UTF-8").                                      GetBytes (Ptoencrypt); //Establish the key and offset of the encrypted object///The GetBytes method of using the Asciiencoding.ascii method//Make the input password must be entered in English text Des.                       Key = ASCIIEncoding.ASCII.GetBytes (SKey);                       DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);                       MemoryStream ms = new MemoryStream (); CryptoStream cs = new CryptoStream (MS, Des.                                      CreateEncryptor (), cryptostreammode.write); Cs.                       Write (Inputbytearray, 0, inputbytearray.length); Cs.                                      FlushFinalBlock ();                       StringBuilder ret = new StringBuilder (); foreach (Byte b in Ms. ToArray ()) {ret.                       AppendFormat ("{0:x2}", b); } ret.                       ToString (); return ret.                   ToString (); } #endregion//<summary>////</summary>//<param name= "Ptodecrypt" > String to Decrypt </param>//<param Nam                E= "SKey" > Decryption key, required 8 bytes, same as encryption key </param>//<returns> decryption successfully returned decrypted string, failed back to source string </returns>                #region Desdecode des decryption public static string Desdecode (String ptodecrypt, String SKey)                       {//HttpContext.Current.Response.Write (Ptodecrypt + "<br>" + SKey);                       HttpContext.Current.Response.End ();                                 DESCryptoServiceProvider des = new DESCryptoServiceProvider ();                    byte[] Inputbytearray = new BYTE[PTODECRYPT.LENGTH/2]; for (int x = 0; x < PTODECRYPT.LENGTH/2; + +) {int i = (Convert.ToInt32 (pT                        Odecrypt.substring (x * 2, 2), 16));                    INPUTBYTEARRAY[X] = (byte) i; } des. Key = ASCIIEncoding.ASCII.GetBytes (SKey);                    DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);                    MemoryStream ms = new MemoryStream (); CryptoStream cs = new CryptoStream (MS, Des.                    CreateDecryptor (), cryptostreammode.write); Cs.                    Write (Inputbytearray, 0, inputbytearray.length); Cs.                                 FlushFinalBlock ();                                 StringBuilder ret = new StringBuilder (); Return HttpContext.Current.Server.UrlDecode (System.Text.Encoding.Default.GetString (Ms.                ToArray ()));      } #endregion}

<?phpclass Des{var $key; var $iv;//Offset function DES ($key, $iv =0) {//key length 8 for example: 1234abcd$this->key = $key; if ($iv = = 0 {$this->iv = $key;//default to $key as IV} else {$this->iv = $iv;//mcrypt_create_iv (Mcrypt_get_block_size (mcrypt_des , MCRYPT_MODE_CBC), mcrypt_dev_random);}} function Encrypt ($STR) {//encryption, return uppercase hexadecimal string $size = Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC); $str = $this Pkcs5pad ($str, $size); return Strtoupper (Bin2Hex (MCRYPT_CBC (mcrypt_des, $this->key, $str, Mcrypt_encrypt, $this-&G T;IV)));} function Decrypt ($STR) {//Decrypt $strbin = $this->hex2bin (strtolower ($STR)); $str = MCRYPT_CBC (mcrypt_des, $this->key , $strBin, Mcrypt_decrypt, $this->iv) $str = $this->pkcs5unpad ($str); return $str;} function Hex2bin ($hexData) {$binData = ""; for ($i = 0; $i < strlen ($hexData); $i + = 2) {$binData. = Chr (Hexdec (substr ($hexData, $i, 2)));} return $binData;} function Pkcs5pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize); return $text. Str_repeat (Chr ($pad), $pad);} function Pkcs5unpad ($text) {$pad = Ord ($text {strlen ($text)-1}), if ($pad > strlen ($text)) return False;if (St RSPN ($text, Chr ($pad), strlen ($text)-$pad)! = $pad) return False;return substr ($text, 0,-1 * $pad);}? >


PHP implementation and C # consistent DES encryption decryption

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.