一、無需任何PHP擴展的加密
此類加密的代表有 威盾PHP加密專家、PHP在線加密平台、PHP神盾 等。
此類加密都是以eval函數為核心,輔以各式各樣的字元串混淆和各種小技巧,來達到加密目的(更准確的說,應該算是混淆)。下面以一個簡單的hello world為例來說明此類加密的大體過程。
<?php
echo "hello world";
首先 ,我們把這段代碼變為通過eval執行的
<?php
eval('echo "hello world";');
然後 ,我們再進行一些轉換,比如說base64編碼
<?php
eval(base64_decode('ZWNobyAiaGVsbG8gd29ybGQiOw=='));
就這樣子,我們的第一個加密過的php代碼新鮮出爐了。。。
上面這個例子非常非常簡單,基本上任何有一點php語言基礎甚至別的語言基礎的人都能輕松的看懂並解密。因此,我們需要一些方法讓這個加密至少看上去不是那麼簡單。
二、同時採用多種編碼函數
除了剛才提到的base64,php還有許多內置的編碼函數,例如urlencode、gzcompress等。把這些函數混合使用可以提高解密的復雜度(不是難度),此外還可以使用strtr來制定自己的編碼規則。 使用變數來代替函數名 使用特定字元來命名變數
這兒所說的特定字元是一些極其相似的字元,如I和1,0和O。試想一下滿屏都是O和0組成的變數,並且每一個的名字長度都在10個字元以上。。。 判斷文件自身是否被修改
這個功能看似容易,對文件做一下摘要再進行下對比即可知道是否被修改了,但是如何才能在文件內把摘要嵌入進去呢?我沒有找到完美的方案,但一個變通的方案還是很容易的。。。
<?php
$code = substr(file_get_contents(__FILE__), 0, -32);
$hash = substr(file_get_contents(__FILE__), -32);
if (md5($code) !== $hash) {
exit('file edited');
}
當然,你可以把這個校驗字元串放在別的位置來提高破解的難度。有了這個,別人想破解你的程序可就得多費一點功夫了。。。
既然知道了原理,那解密自然也就非常簡單了,總體來說就三步:
把eval替換為輸出,比如echo 根據編碼規則把字元串還原 如果文件未解密完全,從第一步開始繼續
當然,實際上的解密過程並沒有這么簡單,比如說如果加密的時候使用了gzcompress,那得到的數據將會包含一些二進制數據,而採用一般的文本編輯器打開時這些數據都會顯示為亂碼,並且在保存時丟失部分數據。解決方法很簡單也很麻煩,那就是使用二進制(16進制)方式打開、修改和保存。
『貳』 PHP常用加密解密方法
作者/上善若水
1.md5(string $str,bool $flag = false);
$flag = false 默認返回32位的16進至數據散列值
$flag = true 返回原始流數據
2.sha1($string,$flag = false)
$flag = false 默認返回40位的16進至數據散列值
true 返回原始流數據
3.hash(string $algo,srting $str,bool $flag);
$algo : 演算法名稱,可通過hash_algos()函數獲取所有hash加密的演算法
如:md5,sha1等,採用md5,sha1加密所得結果和1,2兩種方式結 果相同。
$flag = false 默認返回16進至的數據散列值,具體長度根據演算法不同
而不同。
true 返回原始流數據。
4.crypt(string $str,$string $salt);
函數返回使用 DES、Blowfish 或 MD5 演算法加密的字元串。
具體演算法依賴於PHP檢查之後支持的演算法和$salt的格式和長度,當 然具體結果也和操作系統有關。比較結果採用 hash_equals($crypted,crypt($input,$salt));//且salt值相同
Password_verify($str,$crypted);
5.password_hash ( string $str, integer $algo [, array $options ] )
函數返回哈希加密後的密碼字元串, password_hash() 是crypt()的 一個簡單封裝
$algo : 演算法 PASSWORD_DEFAULT ,PASSWORD_BCRYPT
$options = [
「cost」=>10,//指明演算法遞歸的層數,
「salt」=>「xxadasdsad」//加密鹽值,即將被遺 棄,採用系統自動隨機生成安全性更高
];
使用的演算法、cost 和鹽值作為哈希的一部分返回
Password_verify($str,$hashed);
6.base64_encode(string $str)
設計此種編碼是為了使二進制數據可以通過非純 8-bit 的傳輸層 傳輸,例如電子郵件的主體。base64_decode(string $encoded)
可以進行解碼;
7.mcrypt_encrypt ( string $cipher , string $key , string $data ,
string $mode [, string $iv ] )
mcrypt_decrypt ( string $cipher , string $key , string $crypted ,
string $mode [, string $iv ] )
$ciper:加密演算法,mcrypt_list_algorithms()可以獲取該函數所有支持的演算法
如MCRYPT_DES(「des」),MCRYPT_RIJNDAEL_128(「rijndael-128」);
$mode : 加密模式 ,mcrypt_list_modes()獲取所有支持的加密模式,ecb,cbc
$key: 加密的秘鑰,mcrypt_get_key_size ( string $cipher , string $mode )
獲取指定的演算法和模式所需的密鑰長度。$key要滿足這個長度,如果長 度無效會報出警告。
$iv : 加密的初始向量,可通過mcrypt_create_iv ( int $size [, int $source = MCRYPT_DEV_URANDOM ] ),
Iv的參數size:
通過mcrypt_get_iv_size ( string $cipher , string $mode )獲取
Iv 的參數source:
初始向量數據來源。可選值有: MCRYPT_RAND (系統隨機數生成 器), MCRYPT_DEV_RANDOM (從 /dev/random 文件讀取數據) 和 MCRYPT_DEV_URANDOM (從 /dev/urandom 文件讀取數據)。 在 Windows 平台,PHP 5.3.0 之前的版本中,僅支持 MCRYPT_RAND。
請注意,在 PHP 5.6.0 之前的版本中, 此參數的默認值 為 MCRYPT_DEV_RANDOM。
Note: 需要注意的是,如果沒有更多可用的用來產生隨機數據的信息, 那麼 MCRYPT_DEV_RANDOM 可能進入阻塞狀態。
$data : 要加密的字元串數據
『叄』 如何用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和.net 進行 encrypt加密,結果不同求解惑
php 的加密結果最後用的是base64編碼, .net加密結果是16進制。
『伍』 關於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方式傳輸
『柒』 c# des 16位密鑰和16數據的加密解密,急求...................
/// <summary>
/// 加密原函數
/// </summary>
/// <param name="str_in_data">加密原串</param>
/// <param name="str_DES_KEY">密鑰</param>
/// <returns>加密串</returns>
public static string Encrypt_DES16(string str_in_data, string str_DES_KEY)
{
try
{
byte[] shuju = new byte[8];
byte[] keys = new byte[8];
for (int i = 0; i < 8; i++)
{
shuju[i] = Convert.ToByte(str_in_data.Substring(i * 2, 2), 16);
keys[i] = Convert.ToByte(str_DES_KEY.Substring(i * 2, 2), 16);
}
DES desEncrypt = new DESCryptoServiceProvider();
desEncrypt.Mode = CipherMode.ECB;
//desEncrypt.Key = ASCIIEncoding.ASCII.GetBytes(str_DES_KEY);
desEncrypt.Key = keys;
byte[] Buffer;
Buffer = shuju;//ASCIIEncoding.ASCII.GetBytes(str_in_data);
ICryptoTransform transForm = desEncrypt.CreateEncryptor();
byte[] R;
R = transForm.TransformFinalBlock(Buffer, 0, Buffer.Length);
string return_str = "";
foreach (byte b in R)
{
return_str += b.ToString("X2");
}
return_str = return_str.Substring(0, 16);
return return_str;
}
catch (Exception e)
{
throw e;
}
}
『捌』 一個.net的程序源給我.NET 加密 DES我必須要用php換算出相同的演算法,才能解密讓密碼同步,求大神們賜教
DES是一種加密演算法,與程序語言無關,只要理解他演算法各種語言都能實現
PHP使用DES加密,DES加密後返回大寫十六進制字元串,再進行BASE64轉碼
http://jingyan..com/article/358570f67135b6ce4624fc4a.html
『玖』 用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,對於短數據可解出。
『拾』 DES加密後如何轉換為16進制字元串
可以通過下面的方法進行加密,key換成16位的密鑰即可。
import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DesUtil {
private final static String DES = "DES";
public static void main(String[] args) throws Exception {
String data = "123 456";
String key = "wang!@#$%";
System.err.println(encrypt(data, key));
System.err.println(decrypt(encrypt(data, key), key));
}
/**
* Description 根據鍵值進行加密
* @param data
* @param key 加密鍵byte數組
* @return
* @throws Exception
*/
public static String encrypt(String data, String key) throws Exception {
byte[] bt = encrypt(data.getBytes(), key.getBytes());
String strs = new BASE64Encoder().encode(bt);
return strs;
}
/**
* Description 根據鍵值進行解密
* @param data
* @param key 加密鍵byte數組
* @return
* @throws IOException
* @throws Exception
*/
public static String decrypt(String data, String key) throws IOException,
Exception {
if (data == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf,key.getBytes());
return new String(bt);
}
/**
* Description 根據鍵值進行加密
* @param data
* @param key 加密鍵byte數組
* @return
* @throws Exception
*/