2014-07-27 72 views
-3

3年前我構建了一個多層應用程序,它在.NET端加密並在PHP端進行解密。我從來沒有創建過相應的PHP加密函數,因爲我不認爲我會需要它。現在我知道了,我不記得我從哪裏得到PHP解密。我不知道PHP的加密程度,從頭開始構建它,並使用我現有的php Decrypt()函數工作。需要匹配的PHP加密函數

我需要有人指向我的PHP加密()函數,我的PHP Decrypt()將正確解密。下面的所有細節。

.NET加密功能:

private string Encrypt(string plainText) 
{ 
    if (string.IsNullOrEmpty(plainText)) 
     return string.Empty; 

    string passPhrase = "secret1"; 
    string saltValue = "secret2"; 
    int passwordIterations = 1000; 
    string initVector = "pOWaTbO92LfXbh69JkYzfT7P465TNc0h"; 
    int keySize = 256; 

    byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); 
    byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); 
    byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); 

    Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(passPhrase, saltValueBytes, passwordIterations); 

    byte[] keyBytes = password.GetBytes(keySize/8); 

    // Create uninitialized Rijndael encryption object. 
    RijndaelManaged symmetricKey = new RijndaelManaged(); 
    symmetricKey.BlockSize = 256; 
    symmetricKey.KeySize = 256; 
    symmetricKey.Padding = PaddingMode.Zeros; 
    symmetricKey.Mode = CipherMode.CBC; 

    ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); 
    MemoryStream memoryStream = new MemoryStream(); 
    CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); 
    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); 
    cryptoStream.FlushFinalBlock(); 
    byte[] cipherTextBytes = memoryStream.ToArray(); 
    memoryStream.Close(); 
    cryptoStream.Close(); 
    string cipherText = Convert.ToBase64String(cipherTextBytes); 
    return cipherText; 
} 

PHP解密功能:

function Decrypt($encryptedText) { 
    $p = "secret1"; 
    $s = "secret2"; 
    $iv = "pOWaTbO92LfXbh69JkYzfT7P465TNc0h"; 
    $c = 1000; 
    $kl = 32; 
    $a = 'sha1'; 
    $hl = strlen(hash($a, null, true)); # Hash length 
    $kb = ceil($kl/$hl);    # Key blocks to compute 
    $dk = '';       # Derived key 

    # Create key 
    for ($block = 1; $block <= $kb; $block ++) { 

     # Initial hash for this block 
     $ib = $b = hash_hmac($a, $s . pack('N', $block), $p, true); 

     # Perform block iterations 
     for ($i = 1; $i < $c; $i ++) 

      # XOR each iterate 
      $ib ^= ($b = hash_hmac($a, $b, $p, true)); 

     $dk .= $ib; # Append iterated block 
    } 

    $key = substr($dk, 0, $kl); 
    $plainText = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($encryptedText), MCRYPT_MODE_CBC, $iv)); 

    $textArray = str_split($plainText); 
    foreach($textArray as $char) { 
     $ascii = ord($char); 
     if($ascii < 32 || $ascii > 127){ 
      return ""; 
     } 
    } 

    return $plainText; 
} 

我的.NET加密()和PHP解密()工作的偉大在一起的,沒有問題。有一個PHP加密英雄在那裏,可以告訴我一個PHP的Encrypt()函數,與張貼的php Decrypt()函數一起工作?

一個加密示例:「SnoqL/xcgfFBkhZld + QXRnsl7zS9viJpJsbm4MocDUQ =」解密爲字符串「443」。沒有引號。

+0

爲什麼downvotes? – HerrimanCoder

+0

「SnoqL/xcgfFBkhZld + QXRnsl7zS9viJpJsbm4MocDUQ =」解密爲字符串「443」。沒有引號。 – HerrimanCoder

+0

這個問題可能因爲StackOverflow不是代碼編寫服務而被降低了。 – ntoskrnl

回答

1

以下是帶證明的加密代碼。我拿'這是一個測試!'使用我的函數加密,作爲解密函數的輸入,解密函數成功解密並打印出'This is a test!'。到屏幕上。

<? 

echo Decrypt(Encrypt('This is a test!')); 

function Encrypt($plainText) { 
    $key = createKey(); 
    $encryptedText = rtrim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plainText, MCRYPT_MODE_CBC, "pOWaTbO92LfXbh69JkYzfT7P465TNc0h"))); 

    return $encryptedText; 
} 

function Decrypt($encryptedText) { 
    $key = createKey(); 
    $plainText = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($encryptedText), MCRYPT_MODE_CBC, "pOWaTbO92LfXbh69JkYzfT7P465TNc0h")); 

    return $plainText; 
} 

function createKey() { 
    $p = "secret1"; 
    $s = "secret2"; 
    $dk = ''; 

    for ($block = 1; $block <= 2; $block++) { 
    $ib = $b = hash_hmac('sha1', $s . pack('N', $block), $p, true); 

    for ($i = 1; $i < 1000; $i++) 
     $ib ^= ($b = hash_hmac('sha1', $b, $p, true)); 
     $dk .= $ib; 
    } 

    return substr($dk, 0, 32); 
} 

創建了移動大量的重複的代碼我們本來簡單的解密()/加密()函數的createKey()函數。