欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP和C#可共用的可逆加密算法详解

程序员文章站 2023-11-18 15:04:22
在一些项目中要求在php中生成加密,然后在asp.net中接受过来的密码再解密,下面和大家分享一个php与asp.net c#可共用的可逆加密算法,感兴趣的可以参考参考。...

在一些项目中要求在php中生成加密,然后在asp.net中接受过来的密码再解密,下面和大家分享一个php与asp.net c#可共用的可逆加密算法,感兴趣的可以参考参考。

php加密算法:

<?php
class des
{
  var $key;
  var $iv; //偏移量
  
  function des($key = '11001100', $iv=0 ) {
  //key长度8例如:1234abcd
    $this->key = $key;
    if( $iv == 0 ) {
      $this->iv = $key; //默认以$key 作为 iv
    } else {
      $this->iv = $iv; //mcrypt_create_iv ( mcrypt_get_block_size (mcrypt_des, mcrypt_mode_cbc), mcrypt_dev_random );
    }
  }
  
  function encrypt($str) {
  //加密,返回大写十六进制字符串
    $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) {
  //解密
    $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程序代码:

using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.security.cryptography;
using system.text;

namespace windowsformsapplication1
{
  /// <summary>
  /// des加密解密字符串
  /// </summary>
  public class desencryption
  {
    /// <summary>
    /// des加密字符串
    /// </summary>
    /// <param name="encryptstring">待加密的字符串</param>
    /// <param name="encryptkey">加密密钥,要求为8位</param>
    /// <returns>加密成功返回加密后的字符串,失败返回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解密字符串
    /// </summary>
    /// <param name="decryptstring">待解密的字符串</param>
    /// <param name="decryptkey">解密密钥,要求为8位,和加密密钥相同</param>
    /// <returns>解密成功返回解密后的字符串,失败返回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; 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;
      }
    }
  }
}

以上就是php和c#可共用的可逆加密算法,希望对大家的学习有所帮助。