如何用php实现和c#一致的php post des加密解密密

在PHP中基于mcrypt扩展实现DES加解密
在PHP中实现DES加解密算法有强大的mcrypt扩展的支持,因此实现起来非常的简单。
function encrypt($encrypt,$key){
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES,MCRYPT_MODE_ECB),MCRYPT_RAND);
$passcrypt = mcrypt_encrypt(MCRYPT_DES ,$key, $encrypt, MCRYPT_MODE_ECB, $iv);
$encode = base64_encode($passcrypt);
return $encode;}function decrypt($decrypt,$key){
$decoded = base64_decode($decrypt);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES,MCRYPT_MODE_ECB),MCRYPT_RAND);
$decrypted = mcrypt_decrypt(MCRYPT_DES ,$key, $decoded, MCRYPT_MODE_ECB, $iv);
return $decrypted;}echo $s = encrypt('','');echo decrypt($s,'');
标签(Tag):
------分隔线----------------------------
------分隔线----------------------------PHP与asp.net C#可共用的可逆加密算法
来源:&&&时间: 19:27:02&&&阅读数:
[导读] 因为工作需我们需要在php中生成加密然后在asp net中接受过来的密码再解密,下面我找到一个PHP与asp net C 可共用的可逆加密算法,有需要了解的同学可参考。php加密算法 代码如下复制代码 <?phpclass
因为工作需我们需要在php中生成加密然后在asp.net中接受过来的密码再解密,下面我找到一个PHP与asp.net C#可共用的可逆加密算法,有需要了解的同学可参考。
php加密算法
&?phpclass DES{&&& var $&&& var $ //偏移量&&& &&& function DES($key = '', $iv=0 ) {&&& //key长度8例如:1234abcd&&&&&&& $this-&key = $&&&&&&& if( $iv == 0 ) {&&&&&&&&&&& $this-&iv = $ //默认以$key 作为 iv&&&&&&& } else {&&&&&&&&&&& $this-&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 $&&& }&&& &&& function hex2bin($hexData) {&&&&&&& $binData = &&;&&&&&&& for($i = 0; $i & strlen ( $hexData ); $i += 2) {&&&&&&&&&&& $binData .= chr ( hexdec (
( $hexData, $i, 2 ) ) );&&&&&&& }&&&&&&& return $binD&&& }&&& 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 ))&&&&&&&&&&&&&&&&&& if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)&&&&&&&&&&&&&&&&&& return substr ( $text, 0, - 1 * $pad );&&& }&&& }?&
asp.net程序代码&
using Susing System.Collections.Gusing System.IO;using System.Lusing System.Security.Cusing System.Tnamespace 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 = &&)&&&&&&& {&&&&&&&&&&& try&&&&&&&&&&& {&&&&&&&&&&&&&&& byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(encryptKey.Substring(0, 8));&&&&&&&&&&&&&&& byte[] rgbIV = rgbK&&&&&&&&&&&&&&& 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();&&&&&&&&&&&&&&&
(byte b in mStream.ToArray())&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& ret.AppendFormat(&{0:X2}&, b);&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& ret.ToString();&&&&&&&&&&&&&&& return ret.ToString(); &&&&&&&&&&& }&&&&&&&&&&& &&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&& }&&&&&&& /// &summary&&&&&&&& /// DES解密字符串&&&&&&& /// &/summary&&&&&&&& /// &param name=&decryptString&&待解密的字符串&/param&&&&&&&& /// &param name=&decryptKey&&解密密钥,要求为8位,和加密密钥相同&/param&&&&&&&& /// &returns&解密成功返回解密后的字符串,失败返回null&/returns&&&&&&&& public static string DecryptDES(string decryptString, string decryptKey = &&)&&&&&&& {&&&&&&&&&&& try&&&&&&&&&&& {&&&&&&&&&&&&&&& byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(decryptKey);&&&&&&&&&&&&&&& byte[] rgbIV = rgbK&&&&&&&&&&&&&&& 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&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&& }&&& }}
除非特别声明,PHP100新闻均为原创或投稿报道,转载请注明作者及原文链接原文地址:
延伸阅读 More
视频教程 Video
网站服务:
专题合作 : tijing#(#换成@)
会员问题 :
友情链接 :
网站投稿 :
@php100官方
php100官方微信
Copyright (C) 2007-, All Rights Reserved 版权所有 京ICP备号-14
请关注php100官方微信1629人阅读
php实现和c#一致的DES加密解密,可以从网上搜到一大堆,但是测试后发现都没法用。以下正确代码是我经过苦苦才找到的。希望大家在系统整合时能用的上。
注意:key的长度为8位以内。
//C# 版DES 加解密算法
using System.D
using System.C
using System.W
using System.Web.S
using System.Web.UI;
using System.Web.UI.WebC
using System.Web.UI.WebControls.WebP
using System.Web.UI.HtmlC
using System.Data.SqlC
using System.Security.C
using System.IO;
using System.T
public class Des{
//加解密密钥
private static
string skey = &&;
//初始化向量
private static byte[] DESIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
#region DESEnCode DES加密
public static string DESEnCode(string pToEncrypt, string sKey)
pToEncrypt = HttpContext.Current.Server.UrlEncode(pToEncrypt);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.GetEncoding(&UTF-8&).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);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
ret.AppendFormat(&{0:X2}&, b);
ret.ToString();
return ret.ToString();
#endregion
/// &summary&
/// &/summary&
/// &param name=&pToDecrypt&& 待解密的字符串&/param&
/// &param name=&sKey&& 解密密钥,要求为8字节,和加密密钥相同&/param&
/// &returns&解密成功返回解密后的字符串,失败返源串&/returns&
#region DESDeCode DES解密
public static string DESDeCode(string pToDecrypt, string sKey)
HttpContext.Current.Response.Write(pToDecrypt + &&br&& + sKey);
HttpContext.Current.Response.End();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
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);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
#endregion
var $ //偏移量
function DES( $key, $iv=0 ) {
//key长度8例如:1234abcd
$this-&key = $
if( $iv == 0 ) {
$this-&iv = $ //默认以$key 作为 iv
$this-&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 );
function hex2bin($hexData) {
$binData = &&;
for($i = 0; $i & strlen ( $hexData ); $i += 2) {
$binData .= chr ( hexdec ( substr ( $hexData, $i, 2 ) ) );
return $binD
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 ))
if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)
return substr ( $text, 0, - 1 * $pad );
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:571745次
积分:10444
积分:10444
排名:第667名
原创:451篇
转载:154篇
评论:82条
阅读:3556
文章:10篇
阅读:6716
(4)(10)(2)(8)(6)(5)(5)(2)(2)(10)(4)(13)(36)(20)(4)(7)(9)(14)(3)(29)(36)(38)(5)(10)(5)(33)(69)(5)(22)(71)(29)(20)(38)(11)(20)DES加密和解密PHP等语言的方法
的加解密函数
class DesComponent {&var $key = ;
&function encrypt($string) {
&&$ivArray=array(0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF);&&$iv=&&foreach ($ivArray as $element)&&&$iv.=CHR($element);
&&&$size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );& &&&&&& $string = $this-&pkcs5Pad ( $string, $size );&
&&$data =& mcrypt_encrypt(MCRYPT_DES, $this-&key, $string, MCRYPT_MODE_CBC, $iv);
&&$data = base64_encode($data);&&return $&}
&function decrypt($string) {
&&$ivArray=array(0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF);&&$iv=&&foreach ($ivArray as $element)&&&$iv.=CHR($element);
&&$string = base64_decode($string);&&//echo("****");&&//echo($string);&&//echo("****");&&$result =& mcrypt_decrypt(MCRYPT_DES, $this-&key, $string, MCRYPT_MODE_CBC, $iv);&& $result = $this-&pkcs5Unpad( $result );&
&&return $&}&&& 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 ))& &&&&&&&&&&&& &&&&&&& if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)& &&&&&&&&&&&& &&&&&&& return substr ( $text, 0, - 1 * $pad );& &&& }& &}
$des = new DesComponent();echo ($des-&encrypt(""));echo "";
//die($des-&decrypt("zLVdpYUM0qw="));//die($des-&decrypt("zLVdpYUM0qzEsNshEEI6Cg=="));
$t2 =$des-&decrypt("zLVdpYUM0qw="); echo $t2;echo "--";echo strlen($t2);echo is_utf8($t2);
echo "";$t3 = mb_convert_encoding($t2,"GB2312", "utf-8");echo $t3;echo "--";echo strlen($t3);echo is_utf8($t3);
$t1 =$des-&decrypt("zLVdpYUM0qzEsNshEEI6Cg=="); echo $t1;echo "--";echo strlen($t1);echo is_utf8($t1);
echo "";$t3 = mb_convert_encoding($t1, "utf-8","GB2312");echo $t3;echo "--";echo strlen($t3);echo is_utf8($t3);
function is_utf8($string) { return preg_match(%^(?: [x09x0Ax0Dx20-x7E] # ASCII | [xC2-xDF][x80-xBF] # non-overlong 2-byte | xE0[xA0-xBF][x80-xBF] # excluding overlongs | [xE1-xECxEExEF][x80-xBF]{2} # straight 3-byte | xED[x80-x9F][x80-xBF] # excluding surrogates | xF0[x90-xBF][x80-xBF]{2} # planes 1-3 | [xF1-xF3][x80-xBF]{3} # planes 4-15 | xF4[x80-x8F][x80-xBF]{2} # plane 16 )*$%xs, $string); }?&
的加解密函数
package ghj1976.D
/*&* Copyright (C) 2010 The
Open Source Project&*&* Licensed under the Apache License, Version 2.0 (the "License");&* you may not use this file except in compliance with the License.&* You may obtain a copy of the License at&*&*&&&&& &*&* Unless required by applicable law or agreed to in writing, software&* distributed under the License is distributed on an "AS IS" BASIS,&* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&* See the License for the specific language governing permissions and&* limitations under the License.&*/&import java.io.UnsupportedEncodingE&/**&* Utilities for encoding and decoding the Base64 representation of&* binary data.& See RFCs 2045 and 3548.&*/public class Base64 {&&& /**&&&& * Default values for encoder/decoder flags.&&&& */&&& public static final int DEFAULT = 0;&&&& /**&&&& * Encoder flag bit to omit the padding = characters at the end&&&& * of the output (if any).&&&& */&&& public static final int NO_PADDING = 1;&&&& /**&&&& * Encoder flag bit to omit all line terminators (i.e., the output&&&& * will be on one long line).&&&& */&&& public static final int NO_WRAP = 2;&&&& /**&&&& * Encoder flag bit to indicate lines should be terminated with a&&&& * CRLF pair instead of just an LF.& Has no effect if &&&& * NO_WRAP} is specified as well.&&&& */&&& public static final int CRLF = 4;&&&& /**&&&& * Encoder/decoder flag bit to indicate using the "URL and&&&& * filename safe" variant of Base64 (see RFC 3548 section 4) where&&&& *
_} are used in place of
+} and&&&& *
/}.&&&& */&&& public static final int URL_SAFE = 8;&&&& /**&&&& * Flag to pass to
Base64OutputStream} to indicate that it&&&& * should not close the output stream it is wrapping when it&&&& * itself is closed.&&&& */&&& public static final int NO_CLOSE = 16;&&&& //& --------------------------------------------------------&&& //& shared code&&& //& --------------------------------------------------------&&&& /* package */ static abstract class Coder {&&&&&&& public byte[]&&&&&&&&&&&&&&& /**&&&&&&&& * Encode/decode another block of input data.& this.output is&&&&&&&& * provided by the caller, and must be big enough to hold all&&&&&&&& * the coded data.& On exit, this.opwill be set to the length&&&&&&&& * of the coded data.&&&&&&&& *&&&&&&&& * @param finish true if this is the final call to proce如何用php实现和c#一致的DES加密解密
[问题点数:100分,结帖人s777n]
如何用php实现和c#一致的DES加密解密
[问题点数:100分,结帖人s777n]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
相关推荐:
2010年4月 PHP大版内专家分月排行榜第三2010年1月 PHP大版内专家分月排行榜第三
2010年4月 PHP大版内专家分月排行榜第三2010年1月 PHP大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 java des加密 php解密 的文章

 

随机推荐