PDA

View Full Version : [Win32][C#] CryptoAPI - Usare CryptEncrypt


Energy++
24-02-2009, 10:38
salve ragazzi, ho questo codice che permette di decifrare un file o dei dati utilizzando le cryptoAPI:


[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern bool CryptAcquireContext(out IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern bool CryptCreateHash(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, out IntPtr phHash);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern bool CryptHashData(IntPtr hHash, byte[] pbData, uint dwDataLen, uint dwFlags);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern bool CryptDeriveKey(IntPtr hProv, uint Algid, IntPtr hBaseData, uint dwFlags, out IntPtr phKey);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern bool CryptDecrypt(IntPtr hKey, IntPtr hHash, bool Final, uint dwFlags, byte[] pbData, ref uint pdwDataLen);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern bool CryptEncrypt(IntPtr hKey, IntPtr hHash, bool Final, uint dwFlags, byte[] pbData, ref uint pdwDataLen);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern bool CryptDestroyKey(IntPtr hKey);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern bool CryptDestroyHash(IntPtr hHash);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);

private const uint PROV_RSA_AES = 24;
private const uint CALG_SHA1 = 0x8004;
private const uint CALG_AES_128 = 0x660E;
private const uint CRYPT_EXPORTABLE = 1;

private const long E_FAIL = 0x80004005;
private IntPtr m_hProvider;
private IntPtr m_hHash;
private IntPtr m_hKey;

private void ReleaseKey()
{
if (m_hKey != IntPtr.Zero)
CryptDestroyKey(m_hKey);

if (m_hHash != IntPtr.Zero)
CryptDestroyHash(m_hHash);

if (m_hProvider != IntPtr.Zero)
CryptReleaseContext(m_hProvider, 0);
}

private long UpdateKey(string szKey)
{
if (szKey.Length != 16)
return E_FAIL;

ReleaseKey();

szKey = szKey.ToLower();
uint length = (uint)Encoding.Unicode.GetBytes(szKey).Length;
if (!CryptAcquireContext(out m_hProvider, null, null, PROV_RSA_AES, 0))
{
if (!CryptAcquireContext(out m_hProvider, null, null, PROV_RSA_AES, 8))
return E_FAIL;
}

if (CryptCreateHash(m_hProvider, CALG_SHA1, IntPtr.Zero, 0, out m_hHash))
if (CryptHashData(m_hHash, Encoding.Unicode.GetBytes(szKey), length, 0))
if (CryptDeriveKey(m_hProvider, CALG_AES_128, m_hHash, CRYPT_EXPORTABLE, out m_hKey))
return 0;

return E_FAIL;
}

public long Decrypt(string szKey, byte[] pData, Stream result)
{
long lResult = UpdateKey(szKey);
if (lResult != 0)
return lResult;

uint dwSize = (uint)pData.Length;
if (CryptDecrypt(m_hKey, IntPtr.Zero, true, 0, pData, ref dwSize))
{
result.Write(pData, 0, (int)dwSize);
return 0;
}

return lResult;
}


ora io non sono molto esperto di win32 e questo codice che ho trovato è un pò criptico per me, ma a quanto ho capito inizializza il provider per la crittografia asimmetrica rsa e con la chiave pubblica passata al metodo Decrypt recupera quella privata e decifra i dati...

ora io avrei bisogno di crittografare nuovamente i dati però utilizzando una chiave pubblica diversa...

c'è qualcuno che può indirizzarmi verso il procedimento esatto? :)

fero86
24-02-2009, 12:21
scusa se non rispondo alla tua domanda, ma come mai usi le CryptoAPI anziché l'apposito namespace System.Security.Cryptography (http://msdn.microsoft.com/en-us/library/system.security.cryptography.aspx) di .NET?

Energy++
24-02-2009, 12:45
ho provato ad utilizzare la classe RSACryptoServiceProvider, ma non riesco a capire come procedere per recuperare la chiave privata...

potresti aiutarmi magari traducendo quel codice in modo che utilizzi RSACryptoServiceProvider?

come faccio a recuperare la chiave privata? come faccio a specificare il contenitore dove si trova la chiave?


quel codice di sopra non l'ho scritto io, quindi non so esattamente come arriva allo scopo...

Energy++
25-02-2009, 20:09
up