A reversible cryptographic algorithm that can be shared between PHP and C #,
In some projects require the generation of encryption in PHP, and then in ASP. NET to accept the password to decrypt, below and you share a PHP and ASP. C # can be shared reversible encryption algorithm, interested can refer to the reference.
PHP Encryption algorithm:
<?phpclass des{var $key; var $iv; Offset function DES ($key = ' 11001100 ', $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 (Mcryp T_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); }}?>
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 Cryptographic decryption string //public class Desencryption {///// des encrypted string /////String to encrypt///Encryption key, required for 8-bit///
encryption succeeded in returning the encrypted string, and failed to return null
public static string Encryptdes (String encryptstring, String encryptkey = "11001100") {try {byte[] R Gbkey = 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 Decrypt///Decryption key, required to be 8-bit, same as encryption key///
decryption succeeded in returning the decrypted string, and failed to return null
public static string Decryptdes (String decryptstring, String decryptkey = "11001100") {try {byte[] R Gbkey = ASCIIEncoding.ASCII.GetBytes (Decryptkey); byte[] Rgbiv = RgbKey; byte[] Inputbytearray = new BYTE[DECRYPTSTRING.LENGTH/2]; for (int x = 0; x < DECRYPTSTRING.LENGTH/2; + +) {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 PHP and C # can be common reversible encryption algorithm, I hope that everyone's learning is helpful.
http://www.bkjia.com/PHPjc/1065156.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065156.html techarticle PHP and C # can be shared with the reversible encryption algorithm in detail, in some projects require to generate encryption in PHP, and then in ASP. NET to accept the password and then decrypt, below and we share a PHP and ...