導航:首頁 > 編程語言 > phpdes

phpdes

發布時間:2022-01-14 21:55:11

『壹』 php des3加密key長度不到24位怎麼辦

php使用3DES 加密時,如果加密用的key長度不足可以使用 「」來進行補位。

假設使用了 pkcs#5 填充,key的長度為8位,但是實際給的key只有7位,那麼可以使用一個 「」進行補位。如圖:

其他情況,可以以此類推。

『貳』 用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,對於短數據可解出。

『叄』 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加密用js解密解除亂碼,發現2個加密出的東西不一樣

JS加密和PHP加密是不同的,雖然都是md5加密,所以你不能直接判斷js加密和PHP加密是否相等,畢竟是兩種語言,要麼都用js的加密,要麼都是PHP的

『陸』 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實現和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);
}

}
?>

『捌』 求 js和PHP版的 DES加解密程序

php: http://jingyan..com/article/358570f67135b6ce4624fc4a.html
js:http://www.cnblogs.com/qiongmiaoer/p/3573474.html

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

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

『拾』 php怎樣用des加密演算法給介面加密

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

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

再或者是用https方式傳輸

閱讀全文

與phpdes相關的資料

熱點內容
linuxrpm卸載jdk 瀏覽:860
mysql許可權設置命令 瀏覽:618
hexophp 瀏覽:271
用什麼app買東西半價 瀏覽:62
蘋果下載的pdf文件怎麼打開 瀏覽:211
如何在伺服器上隱藏源站地址 瀏覽:645
單片機進制字母對應表 瀏覽:528
向某人下命令 瀏覽:627
編程中刪除數組中的數 瀏覽:86
aes對稱加密反編譯 瀏覽:550
java編譯成exe 瀏覽:190
gps處理演算法 瀏覽:596
什麼app可以和對象存錢 瀏覽:146
java字元串表達式計算 瀏覽:330
javacmd環境變數 瀏覽:51
電視上面找不到全民歌app怎麼辦 瀏覽:156
單片機中psw0 瀏覽:994
優酷視頻加密么 瀏覽:763
本地連接dos命令 瀏覽:206
雲伺服器怎麼上傳金幣房卡游戲 瀏覽:71