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」。沒有引號。
爲什麼downvotes? – HerrimanCoder
「SnoqL/xcgfFBkhZld + QXRnsl7zS9viJpJsbm4MocDUQ =」解密爲字符串「443」。沒有引號。 – HerrimanCoder
這個問題可能因爲StackOverflow不是代碼編寫服務而被降低了。 – ntoskrnl