Because the job requires us to generate encryption in PHP and then in ASP. NET to accept the password to decrypt, below I found a PHP and ASP. C # can be shared reversible encryption algorithm, there is need to understand the students can refer to.
PHP encryption algorithm
| The code is as follows |
Copy Code |
Class DES { var $key; var $iv; Offset amount function DES ($key = ' 11001100 ', $iv = 0) { Key Length 8 Example: 1234ABCD $this->key = $key; if ($iv = = 0) { $this->iv = $key; Default to $key as IV } else { $this->iv = $iv; Mcrypt_create_iv (Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC), mcrypt_dev_random); } } function Encrypt ($STR) { Encrypt, 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
| The code is as follows |
Copy 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[] 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 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[] 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; } } } } |
http://www.bkjia.com/PHPjc/444596.html www.bkjia.com true http://www.bkjia.com/PHPjc/444596.html techarticle because the job requires us to generate encryption in PHP and then in ASP. NET to accept the password to decrypt, below I found a PHP and ASP C # can be common reversible encryption algorithm, there is need to understand ...