㈠ 求一个DES 算法 php python 通用 PHP进行加密 python解密

用hash呗。
import hashlib

a = "a test string"
print hashlib.md5(a).hexdigest()
print hashlib.sha1(a).hexdigest()
print hashlib.sha224(a).hexdigest()
print hashlib.sha256(a).hexdigest()
print hashlib.sha384(a).hexdigest()
print hashlib.sha512(a).hexdigest()

针对str类型的。
加密的话,可以对最后得出的hash值再处理即可。比如左移,右移,某2位替换,某位加几等等即可。
解密直接用逆序就可以了。

㈡ 一个.net的程序源给我.NET 加密 DES我必须要用php换算出相同的算法,才能解密让密码同步,求大神们赐教

DES是一种加密算法,与程序语言无关,只要理解他算法各种语言都能实现
PHP使用DES加密,DES加密后返回大写十六进制字符串,再进行BASE64转码
http://jingyan..com/article/358570f67135b6ce4624fc4a.html

㈢ 如何用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);
}

}
?>

㈣ php怎样用des加密算法给接口加密

所谓的接口加密 是对接口调用的参数加密, php des加密算法 网上有很多. 如:
http://www.cnblogs.com/cocowool/archive/2009/01/07/1371309.html

如果还嫌不安全,那就制定一个token生成规则,按某些服务器端和客户端都拥有的共同属性生成一个随机串,客户端生成这个串,服务器收到请求也校验这个串。.

再或者是用https方式传输

㈤ 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 加密 密钥长度问题

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 des3加密key长度不到24位怎么办

php使用3DES
加密时,如果加密用的key长度不足可以使用
“”来进行补位。
假设使用了
pkcs#5
填充,key的长版度为8位,但是权实际给的key只有7位,那么可以使用一个
“”进行补位。如图:
其他情况,可以以此类推。

㈧ PHP 用DES加密的信息为什么跟Java加密的结果不同 用CBC模式的

他们的加密算法都是通用的,是可以解开的,只要你des的模式,加密长度,初始向量什么的都一样就可以。

㈨ 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加密

<?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,对于短数据可解出。