Php and c # consistent DES encryption and decryption algorithm class

Source: Internet
Author: User
: This article mainly introduces the same DES encryption and decryption algorithm class in php and c #. if you are interested in the PHP Tutorial, refer to it. DES encryption and decryption algorithm, common class in C # and PHP.

Note:

1. the key in the class uses the same value as the vector iv.

2. UTF-8 encoding

The specific code is as follows. you can directly copy the code for actual testing.

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 encryption and decryption algorithm class public class Des {// encryption and decryption key private static string skey = "12345678 "; # region DESEnCode DES encrypt 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); 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) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); byte [] inputByteArray = new byte [pToDecrypt. length/2]; for (int x = 0; x <pToDecrypt. length/2; x ++) {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 code:

 Key = $ key; $ this-> iv = $ key; // $ key is used as the iv by default} // encryption function encrypt ($ str) {$ 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);} // 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 pkcs5Unpa D ($ 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) ;}} header ("Content-type: text/html; charset = utf-8 "); $ key = '000000'; $ str = 'ABC. 123 '; $ des = new DES ($ key); $ str_en_des = $ des-> encrypt ($ str ); // encrypted string $ str_de_des = $ des-> decrypt ($ str_en_des); // decrypts the string echo 'encrypted string: ', $ str ,'
'; Echo 'encryption key:', $ key ,'

'; Echo 'encrypted string:', $ str_en_des ,'
'; Echo' decrypted string: ', $ str_de_des ,'
';?>

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

The above introduces the same DES encryption and decryption algorithm class in php and c #, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.