Receive parameters from a web service writen in .NET but i cant decrypt these
The parameters have been encrypted with SHA1, Rijndael 256 bits.
php code:
$passphrase='16charskey';
$salt = '16charssat';
$iterations = 2;
$keysize = 32;
$key = pbkdf2($passphrase,$salt, $iterations, $keysize);
$text = 'my crypted text';
$iv = '16charsinitvector';
$result = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_CBC, $iv)
the function pbkdf2 is the next
function pbkdf2( $p, $s, $c, $kl, $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
}
# Return derived key of correct length
return substr($dk, 0, $kl);
}
When i execute give the Warning: mcrypt_encrypt(): The IV parameter must be as long as the blocksize in
i tested change the length of $iv to 32 chars and the warning disappear. When i change the string $iv to other string with any string less than 32 chars, the result is the same that if $iv is empty.
any ideas?
UPDATED
This is the WebService code in the encrypt
Public Function strEncrypt( _
ByVal strText As String _
, ByVal strPass As String _
, ByVal strSalt As String _
, ByVal strHASH As String _
, ByVal nIterations As Integer _
, ByVal strIV As String _
, ByVal nSize As Integer _
) As String
Try
Dim btText As Byte() = Encoding.UTF8.GetBytes(strText)
Dim btSalt As Byte() = Encoding.UTF8.GetBytes(strSalt)
Dim btIV As Byte() = Encoding.UTF8.GetBytes(strIV)
Dim objPasswordDeriveBytes As PasswordDeriveBytes = New PasswordDeriveBytes(strPass, btSalt, strHASH, nIterations)
Dim btKey As Byte() = objPasswordDeriveBytes.GetBytes(nSize / 8)
Dim objRijndaelManaged As RijndaelManaged = New RijndaelManaged()
objRijndaelManaged.Mode = CipherMode.CBC
Dim objICryptoTransform As ICryptoTransform = objRijndaelManaged.CreateEncryptor(btKey, btIV)
Dim objMemoryStream As MemoryStream = New MemoryStream()
Dim objCryptoStream As CryptoStream = New CryptoStream(objMemoryStream, objICryptoTransform, CryptoStreamMode.Write)
objCryptoStream.Write(btText, 0, btText.Length)
objCryptoStream.FlushFinalBlock()
Dim btEncrypt As Byte() = objMemoryStream.ToArray()
objMemoryStream.Close()
objCryptoStream.Close()
strEncrypt = Convert.ToBase64String(btEncrypt)
Catch ex As Exception
strEncrypt = ""
End Try
EDIT
I change the pbkdf2 call to pbkdf1
function PBKDF1($pass, $salt, $count, $dklen)
{
$t = $pass.$salt;
$t = sha1($t, true);
for($i=2; $i <= $count; $i++)
{
$t = sha1($t, true);
}
$t = substr($t,0,$dklen-1);
return $t;
}
And the algorithm to MCRYPT_RIJNDAEL_128, that is the correct, but the output of mcrypt_encrypt and the web service for the same string, with the same salt, iterations, keysize, passphrase, is distinct...
i dont see the difference to get distinct outputs