`
kisseveryone
  • 浏览: 196668 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

PHP JAVA C#互通的DES加密解密算法

des 
阅读更多
注意:
des中的key必须是8位

先看java代码

public static String encrypt(String message, String key) throws Exception {
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));

		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
		IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
		cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

		return toHexString(cipher.doFinal(message.getBytes("UTF-8")));
	}

public static String decrypt(String message, String key) throws Exception {

		byte[] bytesrc = convertHexString(message);
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
		IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));

		cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

		byte[] retByte = cipher.doFinal(bytesrc);
		return new String(retByte);
	}

public static byte[] convertHexString(String ss) {
		byte digest[] = new byte[ss.length() / 2];
		for (int i = 0; i < digest.length; i++) {
			String byteString = ss.substring(2 * i, 2 * i + 2);
			int byteValue = Integer.parseInt(byteString, 16);
			digest[i] = (byte) byteValue;
		}

		return digest;
	}
	public static String toHexString(byte b[]) {
		StringBuffer hexString = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			String plainText = Integer.toHexString(0xff & b[i]);
			if (plainText.length() < 2)
				plainText = "0" + plainText;
			hexString.append(plainText);
		}

		return hexString.toString();
	}


java写的已经很明显使用的是CBC/PKCS补码方式

在看PHP
function encrypt($str) {
        //加密,返回大写十六进制字符串
        $size = mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_[color=red]CBC[/color] );
        $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 );
        $str = explode('_',$str);
        array_pop($str);
        $str = implode("_",$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 );
    }



最后看c#实现
/// <summary>
	/// DES加密方法
	/// </summary>
	/// <param name="strPlain">明文</param>
	/// <param name="strDESKey">密钥</param>
	/// <param name="strDESIV">向量</param>
	/// <returns>密文</returns>
	public string Encrypt(string source,string _DESKey)
	{
		StringBuilder sb = new StringBuilder();
		using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
		{
			byte[] key = ASCIIEncoding.ASCII.GetBytes(_DESKey);
			byte[] iv = ASCIIEncoding.ASCII.GetBytes(_DESKey);
			byte[] dataByteArray = Encoding.UTF8.GetBytes(source);
			des.Mode = System.Security.Cryptography.CipherMode.CBC;
			des.Key = key;
			des.IV = iv;
			string encrypt = "";
			using (MemoryStream ms = new MemoryStream())
			using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
			{
				cs.Write(dataByteArray, 0, dataByteArray.Length);
				cs.FlushFinalBlock();
				//輸出資料
				foreach (byte b in ms.ToArray())
				{
					sb.AppendFormat("{0:X2}", b);
				}
				encrypt = sb.ToString();
			}
			return encrypt;
		}
		
  	}

/// <summary>
        /// 进行DES解密。
        /// </summary>
        /// <param name="pToDecrypt">要解密的串</param>
        /// <param name="sKey">密钥,且必须为8位。</param>
        /// <returns>已解密的字符串。</returns>
        public string Decrypt(string source, string sKey)
        {
            byte[] inputByteArray = Encoding.UTF8.GetBytes(source);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }

分享到:
评论
5 楼 lianyoujun168 2015-10-08  
c#的解密方法走不通,能把调试通过的代码贴出来吗
4 楼 龙心之火 2014-05-24  
C#代码里那个解密算法有问题,拜托亲调试好了再上传代码好不?
3 楼 weishiym 2014-03-28  
<?php

class CryptDesDemo{
public $key ="";
public $iv = "";


public function init_crypt($key) {
$this->key = $key;
$this->iv= $key;
}

    public function encrypt($str) {
//加密,返回大写十六进制字符串
$size = mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC );
$str = $this->pkcs5Pad ( $str, $size );
$hexString = bin2hex( mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv ) );
return strtoupper( $hexString );
}

public 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 );
}
}
?>
2 楼 weishiym 2014-03-28  
还有iv向量,没说明初始值
1 楼 weishiym 2014-03-28  
php这段写得有问题啊
  function decrypt($str) { 
        //解密   
        $strBin = $this->hex2bin( strtolower( $str ) );   
        $str = mcrypt_cbc( MCRYPT_DES, $this->key, $strBin, MCRYPT_DECRYPT, $this->iv ); 
        $str = $this->pkcs5Unpad( $str ); 
        $str = explode('_',$str); 
        array_pop($str); 
        $str = implode("_",$str); 
        return $str; 
    } 

应该改为

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

相关推荐

Global site tag (gtag.js) - Google Analytics