Posted this over on Anandtech, but I think it may not be in the general forte of the patronage there. Let's give Ars a shot...
I'm trying to implement decryption of data in a .Net app that was encrypted via Win32 CryptoAPI (wincrypt).
A lot of it has been hit or miss for me, since I'm a native C/C++ dev that gets to play with Win32 all day every day. Unfortunately someone wanted a .Net compatible library, so I had to make them happy and do so. In the process, I have some encrypted data being passed between the native library and the .Net application. Problem is decrypting it.
I think the problem lies somewhere in how I'm generating the key on the native side of things. I ultimately use CryptDeriveKey() with a preloaded hashed data pretty much the way you'd see it in the MSDN examples, with the exception that there are multiple calls to CryptHashData() in order to generate the final hash value.
On the .Net side of things, the PasswordDeriveBytes object is said to essentially be a wrapper for the CryptDeriveKey API call, and in fact exposes a method called just that.
The problem that I have is that the hash object created on the native side of things used iterated data to generate the hash object used as the basis for the key. I've gone so far as being able to replicate that exact same hash data on the .Net side of things, but PasswordDeriveBytes seems to be expecting a byte string of data that it will hash itself, thereby not allowing me to use my iterated aggregate hash. Either that, or I'm simply not understanding enough of the .Net objects to work with them properly.
I was hoping someone might have a clue what I'm trying to say, and more importantly trying to do here.
Here's a sample of what I'm doing:
(Native Win32 library pseudocode)
(VB.Net side that needs to decrypt stuff pseudocode)
At this point the Decryptor should be ready to go, but it fails with NTE_BAD_DATA indicating to me that likely the key is incorrect.
My guess is that PasswordDeriveBytes is more expecting an actual password rather than the binary hash. But I'm not certain, as I went so far as to have the native take the base64 string of the resulting hash and feed IT into CryptDeriveKey(), thereby allowing me to do the same on the .Net side of things, but it still didn't work.
I can only guess that I'm not using PasswordDeriveBytes properly in this case.
Any help would be appreciated.
I'm trying to implement decryption of data in a .Net app that was encrypted via Win32 CryptoAPI (wincrypt).
A lot of it has been hit or miss for me, since I'm a native C/C++ dev that gets to play with Win32 all day every day. Unfortunately someone wanted a .Net compatible library, so I had to make them happy and do so. In the process, I have some encrypted data being passed between the native library and the .Net application. Problem is decrypting it.
I think the problem lies somewhere in how I'm generating the key on the native side of things. I ultimately use CryptDeriveKey() with a preloaded hashed data pretty much the way you'd see it in the MSDN examples, with the exception that there are multiple calls to CryptHashData() in order to generate the final hash value.
On the .Net side of things, the PasswordDeriveBytes object is said to essentially be a wrapper for the CryptDeriveKey API call, and in fact exposes a method called just that.
The problem that I have is that the hash object created on the native side of things used iterated data to generate the hash object used as the basis for the key. I've gone so far as being able to replicate that exact same hash data on the .Net side of things, but PasswordDeriveBytes seems to be expecting a byte string of data that it will hash itself, thereby not allowing me to use my iterated aggregate hash. Either that, or I'm simply not understanding enough of the .Net objects to work with them properly.
I was hoping someone might have a clue what I'm trying to say, and more importantly trying to do here.
Here's a sample of what I'm doing:
(Native Win32 library pseudocode)
Code:
CryptCreateHash(hash) // SHA1 - to be used as the basis for the key
CryptHashData(hash, inputdata1)
CryptHashData(hash, inputdata2)
...
CryptHashData(hash, inputdata_n)
CryptDeriveKey(encryptionkey, hash)
CryptEncrypt(encryptionkey, "AlgoID", data_to_encrypt)
(VB.Net side that needs to decrypt stuff pseudocode)
Code:
KeyHash as SHA1
...
KeyHash.TransformBlock(inputdata1)
KeyHash.TransformBlock(inputdata2)
...
KeyHash.TransformBlock(inputdata_n)
KeyHash.TransformFinalBlock()
' At this point the hash held by KeyHash is verified to be the same hash that's in "hash" on the native side of things - just trust me on this
pdb As PasswordDeriveBytes
???
??? ' Not sure what to put in the constructor here for PasswordDeriveBytes
???
Decryptor As AppropriateEncryptionServiceProviderType
Decryptor.Key = pdb.CryptDeriveKey("AlgoID", "SHA1", 0, Decryptor.IV)
At this point the Decryptor should be ready to go, but it fails with NTE_BAD_DATA indicating to me that likely the key is incorrect.
My guess is that PasswordDeriveBytes is more expecting an actual password rather than the binary hash. But I'm not certain, as I went so far as to have the native take the base64 string of the resulting hash and feed IT into CryptDeriveKey(), thereby allowing me to do the same on the .Net side of things, but it still didn't work.
I can only guess that I'm not using PasswordDeriveBytes properly in this case.
Any help would be appreciated.