Rajesh Pant

Rajesh Pant

  • NA
  • 1
  • 0

Encrypt a file using public key in c#

Jan 11 2006 9:50 AM

Hi

1. Actually i am taking a file c:\myfile.txt in which plain text is written. I want to encrypt the content of this file using a public key of the logged in user(e.g. administrator) and the encrypted file content is to be written to c:\encrypt.txt file.

2. Finally i will read content of c:\encrypt.txt file and decrypt it using private key of the logged in user whose certificate is installed in the system along with private key.

 

3. I have created two functions encryptFile() and decryptFile() for the above said operations. In the encryptFile function

the cryptoAPI CryptAcquireContext function is used to acquire a handle to a particular key container within a particular cryptographic service provider (CSP). The code is given below:

[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]

public static extern bool CryptAcquireContext (

ref IntPtr phProv,

string pszContainer,

string pszProvider,

uint dwProvType,

uint dwFlags);

// dwFlags definitions for CryptAcquireContext

public const uint CRYPT_VERIFYCONTEXT = 0xF0000000;

public const uint CRYPT_NEWKEYSET = 0x00000008;

public const uint CRYPT_DELETEKEYSET = 0x00000010;

public const uint CRYPT_MACHINE_KEYSET = 0x00000020;

public const uint CRYPT_SILENT = 0x00000040;

 

// CryptSetProvParam

public const uint PROV_RSA_FULL = 1;

public const uint PROV_RSA_SIG = 2;

public const uint PROV_DSS = 3;

IntPtr hCryptProv = IntPtr.Zero;

ulong hXchgKey = 0;

IntPtr hKey = IntPtr.Zero;

byte[] pbKeyBlob;

uint dwKeyBlobLen = 0;

When i am using the code written below then i acquire a handle to a particular key conatiner within a particular (CSP).

if (EncryptDecrypt.Form1.CryptAcquireContext(

ref hCryptProv,

null,

null, EncryptDecrypt.Form1.PROV_RSA_FULL,

EncryptDecrypt.Form1.CRYPT_VERIFYCONTEXT))

{

Console.Out.WriteLine("\n CSP has been acquired");

}

 

But when i use CRYPT_MACHINE_KEYSET flag then i am not able to get the handle. MSDN documentation states " This option is intended for applications that do not use public/private key pairs. If a key container is to be a machine container, the CRYPT_MACHINE_KEYSET flag must be used with all calls to CryptAcquireContext that reference the machine container". So we have to use CRYPT_MACHINE_KEYSET flags when we are using private/public key container.

if (EncryptDecrypt.Form1.CryptAcquireContext(

ref hCryptProv,

null,

null, EncryptDecrypt.Form1.PROV_RSA_FULL,

EncryptDecrypt.Form1.CRYPT_MACHINE_KEYSET))

{

Console.Out.WriteLine("\n CSP has been acquired");

}

THIS CODE DID NOT GET THE HANDLE TO THE CONTAINER. Can anybody tell me what is wrong with this code.

After the above code this code follows:

if (EncryptDecrypt.Form1.CryptGetUserKey(hCryptProv,

EncryptDecrypt.Form1.AT_KEYEXCHANGE,

ref hXchgKey))

{

Console.Out.WriteLine("\n User public key has be retrived");

}

And we did not get the key hXchgKey also.