This article mainly introduces the implementation of PHP and C # consistent DES encryption and decryption instance, has a certain reference value, now share to everyone, the need for friends can refer to
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) {p Toencrypt = 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 in the original text 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= "p Todecrypt "> String to Decrypt </param>//<param name=" SKey "> Decryption key, required to be 8 bytes, same as encryption key </param>/// Lt;returns> decryption succeeded in returning the decrypted string, failed back to source string </returns> #region Desdecode des decryption public static stringDesdecode (String ptodecrypt, String sKey) {//HttpContext.Current.Response.Write (Ptodecrypt + "<br& gt; "+ 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 (ptodecrypt.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}
<?php class 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 (MC Rypt_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->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 (strspn ($text, Chr ($pad), strlen ($text)-$pad)! = $pad) return false; Return substr ($text, 0,-1 * $pad); }}?>