PHP and asp.net C # reversible cryptographic algorithms for common use

Source: Internet
Author: User
Tags chr decrypt php and strlen

PHP encryption algorithm

The code is as follows Copy Code

<?php
Class DES
{
var $key;
var $iv; Offset amount

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 (Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC), mcrypt_dev_random);
}
}

function Encrypt ($STR) {
Encryption, returning uppercase hexadecimal strings
$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)
& nbsp;           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
{
<summary>
Des encrypted decryption string
</summary>
public class Desencryption
{
<summary>
Des encrypted string
</summary>
<param name= "encryptstring" > Strings to be encrypted </param>
<param name= "Encryptkey" > Encryption key required for 8-bit </param>
<returns> encryption successfully returned the encrypted string, failed to return null</returns>
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;
}
}

<summary>
Des decryption string
</summary>
<param name= "decryptstring" > Strings to be decrypted </param>
<param name= "Decryptkey" > Decryption key, required 8-bit, same as encryption key </param>
<returns> decryption successfully returned the decrypted string, failed to return null</returns>
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 + +)
{
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;
}
}
}
}

Related Article

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.