當前位置:首頁 » 密碼管理 » des加密演算法php

des加密演算法php

發布時間: 2022-05-24 10:21:26

php DES加密

詳見這里的回答,http://..com/link?url=-JSxo3JUy_cGCbCDx00HIUkOMnSSlEEID-_ ,應該可以滿足你的需求。

⑵ 總結一下PHP中有哪些加密演算法技術

1. MD5加密

string md5 ( string $str [, bool $raw_output = false ] )
2. Crype加密
string crypt ( string $str [, string $salt ] )
crypt() 返回一個基於標准 UNIX DES 演算法或系統上其他可用的替代演算法的散列字元串。
3. Sha1加密
string sha1 ( string $str [, bool $raw_output = false ] )
4. url加密
string urlencode ( string $str )
5. base64

string base64_encode ( string $data )

⑶ Des加密結果 在 php5.4 和 php5.5 中不一樣嗎

一樣的。

des加密 數據加密演算法 是一種對稱加密演算法,相同的數據得出的一定是一樣的加密hash散列。

⑷ Java用Des方式加密之後,PHP怎麼解密

DES是一種標準的數據加密演算法,關於這個演算法的詳細介紹可以參考wiki和網路:
php中有一個擴展可以支持DES的加密演算法,是:extension=php_mcrypt.dll
在配置文件中將這個擴展打開還不能夠在windows環境下使用
需要將PHP文件夾下的 libmcrypt.dll 拷貝到系統的 system32 目錄下,這是通過phpinfo可以查看到mcrypt表示這個模塊可以正常試用了。
下面是PHP中使用DES加密解密的一個例子:
//$input - stuff to decrypt
//$key - the secret key to use
function do_mencrypt($input, $key)
{
$input = str_replace(""n", "", $input);
$input = str_replace(""t", "", $input);
$input = str_replace(""r", "", $input);
$key = substr(md5($key), 0, 24);
$td = mcrypt_mole_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$encrypted_data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_mole_close($td);
return trim(chop(base64_encode($encrypted_data)));
}
//$input - stuff to decrypt
//$key - the secret key to use
function do_mdecrypt($input, $key)
{
$input = str_replace(""n", "", $input);
$input = str_replace(""t", "", $input);
$input = str_replace(""r", "", $input);
$input = trim(chop(base64_decode($input)));
$td = mcrypt_mole_open('tripledes', '', 'ecb', '');
$key = substr(md5($key), 0, 24);
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$decrypted_data = mdecrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_mole_close($td);
return trim(chop($decrypted_data));
}

⑸ 關於php des 加密 密鑰長度問題

php5.6的key長度要求是32位元組的,你這個明顯不滿足要求的。
參考以下寫法:
<?php
# --- ENCRYPTION ---

# the key should be random binary, use scrypt, bcrypt or PBKDF2 to
# convert a string into a key
# key is specified using hexadecimal
$key = pack('H*', "");

# show key size use either 16, 24 or 32 byte keys for AES-128, 192
# and 256 respectively
$key_size = strlen($key);
echo "Key size: " . $key_size . "\n";

$plaintext = "This string was AES-256 / CBC / ZeroBytePadding encrypted.";

# create a random IV to use with CBC encoding
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

# creates a cipher text compatible with AES (Rijndael block size = 128)
# to keep the text confidential
# only suitable for encoded input that never ends with value 00h
# (because of default zero padding)
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
$plaintext, MCRYPT_MODE_CBC, $iv);

# prepend the IV for it to be available for decryption
$ciphertext = $iv . $ciphertext;

# encode the resulting cipher text so it can be represented by a string
$ciphertext_base64 = base64_encode($ciphertext);

echo $ciphertext_base64 . "\n";

# === WARNING ===

# Resulting cipher text has no integrity or authenticity added
# and is not protected against padding oracle attacks.

# --- DECRYPTION ---

$ciphertext_dec = base64_decode($ciphertext_base64);

# retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
$iv_dec = substr($ciphertext_dec, 0, $iv_size);

# retrieves the cipher text (everything except the $iv_size in the front)
$ciphertext_dec = substr($ciphertext_dec, $iv_size);

# may remove 00h valued characters from end of plain text
$plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,
$ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);

echo $plaintext_dec . "\n";
?>

⑹ php怎樣用des加密演算法給介面加密

所謂的介面加密 是對介面調用的參數加密, php des加密演算法 網上有很多. 如:
http://www.cnblogs.com/cocowool/archive/2009/01/07/1371309.html

如果還嫌不安全,那就制定一個token生成規則,按某些伺服器端和客戶端都擁有的共同屬性生成一個隨機串,客戶端生成這個串,伺服器收到請求也校驗這個串。.

再或者是用https方式傳輸

⑺ 用PHP的方法解DES加密

<?php
$key='LY870513';
$ctext='j45Rrzxm0jD62U1w798yBg==';
$ptext=mcrypt_decrypt(MCRYPT_DES,$key,base64_decode($ctext),MCRYPT_MODE_CBC,"x12x34x56120x90xabxcdxef");
//echoiconv('UTF-8','GBK',$ptext);//GBK環境使用,UTF8環境多餘不用
echo$ptext;//UTF8環境用
20219241337

由於不清楚原代碼的塊鏈接模式,暫時用的CBC,對於短數據可解出。

⑻ PHP DES加密函數

兩個函數如下:
加密函數:encrypt
function encrypt($encrypt,$key="") {
$iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND );
$passcrypt = mcrypt_encrypt ( MCRYPT_RIJNDAEL_256, $key, $encrypt, MCRYPT_MODE_ECB, $iv );
$encode = base64_encode ( $passcrypt );
return $encode;
}

解密函數:decrypt
function decrypt($decrypt,$key="") {
$decoded = base64_decode ( $decrypt );
$iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND );
$decrypted = mcrypt_decrypt ( MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_ECB, $iv );
return $decrypted;
}

⑼ PHP 用DES加密的信息為什麼跟Java加密的結果不同 用CBC模式的

他們的加密演算法都是通用的,是可以解開的,只要你des的模式,加密長度,初始向量什麼的都一樣就可以。

⑽ 如何用php實現和c#一致的DES加密解密

PHP實現和c#一致的DES加密解密,可以從網上搜到一大堆,但是測試後發現都沒法用。以下正確代碼是我經過苦苦才找到的。希望大家在系統整合時能用的上。

注意:key的長度為8位以內。

[csharp]viewplainprint?
//C#版DES加解密演算法
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
usingSystem.Security.Cryptography;
usingSystem.IO;
usingSystem.Text;
publicclassDes{
//加解密密鑰
privatestaticstringskey="12345678";
//初始化向量
privatestaticbyte[]DESIV={0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF};

#regionDESEnCodeDES加密
publicstaticstringDESEnCode(stringpToEncrypt,stringsKey)
{
pToEncrypt=HttpContext.Current.Server.UrlEncode(pToEncrypt);
DESCryptoServiceProviderdes=newDESCryptoServiceProvider();
byte[]inputByteArray=Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);

//建立加密對象的密鑰和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得輸入密碼必須輸入英文文本
des.Key=ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV=ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStreamms=newMemoryStream();
CryptoStreamcs=newCryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);

cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();

StringBuilderret=newStringBuilder();
foreach(bytebinms.ToArray())
{
ret.AppendFormat("{0:X2}",b);
}
ret.ToString();
returnret.ToString();
}
#endregion
///<summary>
///
///</summary>
///<paramname="pToDecrypt">待解密的字元串</param>
///<paramname="sKey">解密密鑰,要求為8位元組,和加密密鑰相同</param>
///<returns>解密成功返回解密後的字元串,失敗返源串</returns>
#regionDESDeCodeDES解密
publicstaticstringDESDeCode(stringpToDecrypt,stringsKey)
{
//HttpContext.Current.Response.Write(pToDecrypt+"<br>"+sKey);
//HttpContext.Current.Response.End();
DESCryptoServiceProviderdes=newDESCryptoServiceProvider();

byte[]inputByteArray=newbyte[pToDecrypt.Length/2];
for(intx=0;x<pToDecrypt.Length/2;x++)
{
inti=(Convert.ToInt32(pToDecrypt.Substring(x*2,2),16));
inputByteArray[x]=(byte)i;
}

des.Key=ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV=ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStreamms=newMemoryStream();
CryptoStreamcs=newCryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();

StringBuilderret=newStringBuilder();

returnHttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
}
#endregion
}
[php]viewplainprint?
<?php
classDES
{
var$key;
var$iv;//偏移量

functionDES($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);
}
}

functionencrypt($str){
//加密,返回大寫十六進制字元串
$size=mcrypt_get_block_size(MCRYPT_DES,MCRYPT_MODE_CBC);
$str=$this->pkcs5Pad($str,$size);
returnstrtoupper(bin2hex(mcrypt_cbc(MCRYPT_DES,$this->key,$str,MCRYPT_ENCRYPT,$this->iv)));
}

functiondecrypt($str){
//解密
$strBin=$this->hex2bin(strtolower($str));
$str=mcrypt_cbc(MCRYPT_DES,$this->key,$strBin,MCRYPT_DECRYPT,$this->iv);
$str=$this->pkcs5Unpad($str);
return$str;
}

functionhex2bin($hexData){
$binData="";
for($i=0;$i<strlen($hexData);$i+=2){
$binData.=chr(hexdec(substr($hexData,$i,2)));
}
return$binData;
}

functionpkcs5Pad($text,$blocksize){
$pad=$blocksize-(strlen($text)%$blocksize);
return$text.str_repeat(chr($pad),$pad);
}

functionpkcs5Unpad($text){
$pad=ord($text{strlen($text)-1});
if($pad>strlen($text))
returnfalse;
if(strspn($text,chr($pad),strlen($text)-$pad)!=$pad)
returnfalse;
returnsubstr($text,0,-1*$pad);
}

}
?>
熱點內容
sql創建表查詢表 發布:2024-05-05 01:00:12 瀏覽:798
食色抖音上傳 發布:2024-05-05 00:55:56 瀏覽:656
java圖片下載 發布:2024-05-05 00:50:45 瀏覽:597
唱吧如何上傳伴奏 發布:2024-05-05 00:49:04 瀏覽:443
什麼配置單反拍視頻最好 發布:2024-05-05 00:30:56 瀏覽:478
sql敏感 發布:2024-05-05 00:28:20 瀏覽:62
android工程師筆試 發布:2024-05-05 00:10:52 瀏覽:948
python調試pycharm 發布:2024-05-05 00:10:51 瀏覽:707
索尼電腦vaio忘了密碼如何恢復出廠設置 發布:2024-05-05 00:09:56 瀏覽:895
安卓系統的用戶管理在哪裡 發布:2024-05-04 23:12:27 瀏覽:430