当前位置:首页 » 密码管理 » 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);
}

}
?>
热点内容
安卓怎么关闭美易订阅 发布:2024-05-18 19:29:16 浏览:642
苹果手机配置代理服务器怎么开 发布:2024-05-18 19:29:07 浏览:229
服务器屏蔽了别人的ip 发布:2024-05-18 19:10:09 浏览:619
怎么获取ins服务器地址 发布:2024-05-18 19:10:03 浏览:30
仙方一般是什么配置 发布:2024-05-18 18:53:43 浏览:159
黑莓安卓手机主题下载到哪里 发布:2024-05-18 18:47:18 浏览:57
汤靖轩编程 发布:2024-05-18 18:46:04 浏览:533
脚本故事梗 发布:2024-05-18 18:29:02 浏览:823
安卓和csharp哪个发展好 发布:2024-05-18 18:09:30 浏览:527
换编程题库 发布:2024-05-18 18:00:58 浏览:562