|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Oct 2005
Messaggi: 1059
|
[Win32][C#] CryptoAPI - Usare CryptEncrypt
salve ragazzi, ho questo codice che permette di decifrare un file o dei dati utilizzando le cryptoAPI:
Codice:
[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 avrei bisogno di crittografare nuovamente i dati però utilizzando una chiave pubblica diversa... c'è qualcuno che può indirizzarmi verso il procedimento esatto? Ultima modifica di Energy++ : 24-02-2009 alle 10:40. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2006
Città: Roma
Messaggi: 1383
|
scusa se non rispondo alla tua domanda, ma come mai usi le CryptoAPI anziché l'apposito namespace System.Security.Cryptography di .NET?
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Oct 2005
Messaggi: 1059
|
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... Ultima modifica di Energy++ : 24-02-2009 alle 12:59. |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Oct 2005
Messaggi: 1059
|
up
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 21:41.



















