PHP and asp. netC # reversible encryption algorithms that can be shared _ PHP Tutorial

Source: Internet
Author: User
PHP and asp. netC # reversible encryption algorithms that can be shared. Because of work requirements, we need to generate encryption in php and then decrypt the password accepted in asp.net. below I find a PHP and asp. netC # A common reversible encryption algorithm. you need to know that because of work requirements, we need to generate an encryption algorithm in php and then decrypt the password accepted in asp.net, next I will find a reversible encryption algorithm that can be shared by PHP and asp.net C #. For more information, see.

Php encryption algorithm

The code is as follows:

Class DES
{
Var $ key;
Var $ iv; // Offset

Function DES ($ key = '000000', $ iv = 0 ){
// The key length is 8, for example, 1234 abcd.
$ This-> key = $ key;
If ($ iv = 0 ){
$ This-> iv = $ key; // $ key is used as the iv by default.
} 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 = "";
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:

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;
}
}
}
}


Objective C # Shared reversible encryption algorithms. learn more...

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.