PHP and C # consistent DES encryption and decryption algorithm classes

Source: Internet
Author: User
Des plus decryption algorithms, common classes in C # and PHP.

Attention:

1. The key in the class is the same value as the Vector IV.

2, the code is used Utf-8

The following is the specific code, can be copied directly to carry out the actual test

C # version Code:

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; C # des plus decryption algorithm class public class des{//Add decryption key private static string skey = "12345678"; #region desencode Des Encryption Publ IC static string Desencode (String ptoencrypt, String sKey) {Ptoencrypt = HttpContext.Current.Server.UrlEncode (ptoencr   YPT);   DESCryptoServiceProvider des = new DESCryptoServiceProvider (); byte[] Inputbytearray = encoding.getencoding ("UTF-8").       GetBytes (Ptoencrypt); 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 #region Desdecode des decrypt public static string Desdecode (String ptodecrypt, String sKey) {Descryptoserviceprovi Der des = new DESCryptoServiceProvider (); byte[] Inputbytearray = new Byte[ptodecrypt.length/2];for (int x = 0; x < PT ODECRYPT.LENGTH/2; X + +) {int i = (Convert.ToInt32 (ptodecrypt.substring (x * 2, 2)); Inputbytearray[x] = (byte) i;} des. Key = ASCIIEncoding.ASCII.GetBytes (sKey);d Es.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 Version Code:

 key = $key; $this->iv = $key; The default is $key as iv}//cryptographic function encrypt ($STR) {$size = Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC); $str = $this-&gt ;p Kcs5pad ($str, $size); return Strtoupper (Bin2Hex (MCRYPT_CBC (mcrypt_des, $this->key, $str, Mcrypt_encrypt, $this-& GT;IV)));} Decryption function Decrypt ($str) {$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);}} Header ("content-type:text/html; Charset=utf-8 "); $key = ' 12345678 '; $str = ' abc.123 '; $des = new des ($key); $str _en_des = $des->encrypt ($STR); Encrypted string $str_de_des = $des->decrypt ($str _en_des); Decrypt string echo ' encrypted string: ', $str, '
'; Echo ' encryption key: ', $key, '

'; Echo ' encrypted after string: ', $str _en_des, '
'; Echo ' decrypted string: ', $str _de_des, '
';? >

The code is searched online for sorting, especially thanks to blog: http://blog.csdn.net/e421083458/article/details/39373815

The above describes the PHP and C # consistent DES encryption and decryption algorithm classes, including the aspects of the content, I hope that the PHP tutorial interested in a friend to help.

  • 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.