php asp.net des,转DES的dotNet到php实现
C# DESCryptoServiceProvider php 2011年07月27日 星期三 11:35
using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Security.Cryptography; namespace ConsoleApplication1 ??? class Program ??????? static string skey = "12345678";
static string str = "gg";
static string enstr = "D084F252A84AB151"; ??????? static void Main(string[] args) ??????????? Console.WriteLine(Decrypt(enstr, skey));//解密
Console.WriteLine(Encrypt(str,skey));//加密
} ??????? public static string Encrypt(string pToEncrypt, string sKey) ??????????? DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte數組中
//原來使用的UTF8編碼,我改成Unicode編碼了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt); ??????????? //建立加密對象的密鑰和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法 ??????????? des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray()) ??????????????? //Format as hex
ret.AppendFormat("{0:X2}", b); ??????????? ret.ToString();
return ret.ToString();
} ??????? public static string Decrypt(string pToDecrypt, string sKey) ??????????? DESCryptoServiceProvider des = new DESCryptoServiceProvider(); ??????????? //Put the input string into the byte array
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++) ??????????????? int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i; ??????????? des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock(); ??????????? //Get the decrypted data back from the memory stream
//建立StringBuild對象,CreateDecrypt使用的是流對象,必須把解密后的文本變成流對象
StringBuilder ret = new StringBuilder(); ??????????? return System.Text.Encoding.Default.GetString(ms.ToArray()); ??? } ///
class DES ??? var $key;
var $iv; //偏移量 ??? function DES( $key, $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 ); ? ? $key = '12345678';
$crypt = new DES($key); $str = 'gg'; $p = $crypt->encrypt($str); echo "
";
echo $p; $p = $crypt->decrypt($p); echo "
";
echo $p;
?>
更多交流到提問網(shenzhidao.com)一起交流網絡新技術。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的php asp.net des,转DES的dotNet到php实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WTL 学习博文地址
- 下一篇: 存储过程入门与提高