Describes the reversible encryption algorithms that PHP and C # can share ,. PHP and C # can share a detailed description of reversible encryption algorithms. in some projects, it is required to generate encryption in php, and then decrypt the password received in asp.net, the following describes a reversible encryption algorithm that can be shared by PHP, PHP, and C,
In some projects, it is required to generate encryption in php, and then decrypt the password accepted in asp.net. we will share with you a reversible encryption algorithm that PHP and asp.net C # can share with you, for more information, see.
Php encryption algorithm:
<? Phpclass DES {var $ key; var $ iv; // offset function DES ($ key = '000000', $ iv = 0) {// key length 8 for example: 1234 abcd $ this-> key = $ key; if ($ iv = 0) {$ this-> iv = $ key; // $ key is used as the default value.} else {$ this-> iv = $ iv; // mcrypt_create_iv (mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM );}} function encrypt ($ str) {// encrypted, returns an 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 = ""; ($ 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) ;}}?>
Asp.net program code:
Using System; using System. Collections. Generic; using System. IO; using System. Linq; using System. Security. Cryptography; using System. Text; namespace WindowsFormsApplication1 {////// DES encryption and decryption string ///Public class DesEncryption {////// DES encrypted string //////String to be encrypted///Encryption key, which must be 8 bits///
The encrypted string is returned successfully. if the encrypted string fails, null is returned.
Public static string EncryptDES (string encryptString, string encryptKey = "11001100") {try {byte [] rgbKey = ASCIIEncoding. ASCII. getBytes (encryptKey. substring (0, 8); byte [] rgbIV = rgbKey; byte [] inputByteArray = Encoding. UTF8.GetBytes (encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider (); MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, dCSP. createEncryptor (rgbKey, rgbIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); StringBuilder ret = new StringBuilder (); foreach (byte B in mStream. toArray () {ret. appendFormat ("{0: X2}", B);} ret. toString (); return ret. toString () ;}catch {return null ;}}////// DES decryption string //////String to be decrypted///Decryption key, which must be 8 bits, the same as the encryption key///
Returns the decrypted string. if the decryption fails, the return value is null.
Public static string DecryptDES (string decryptString, string decryptKey = "11001100") {try {byte [] rgbKey = ASCIIEncoding. ASCII. getBytes (decryptKey); byte [] rgbIV = rgbKey; byte [] inputByteArray = new byte [decryptString. length/2]; for (int x = 0; x <decryptString. length/2; x ++) {int I = (Convert. toInt32 (decryptString. substring (x * 2, 2), 16); inputByteArray [x] = (byte) I;} DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider (); MemoryStream mStream = new MemoryStream (); cryptoStream cStream = new CryptoStream (mStream, DCSP. createDecryptor (rgbKey, rgbIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); return Encoding. UTF8.GetString (mStream. toArray () ;}catch {return null ;}}}}
The above is the reversible encryption algorithm that PHP and C # can share. I hope it will help you learn it.
Secret: in some projects, it is required to generate encryption in php, and then decrypt the password accepted in asp.net. here is a PHP and...