473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

EFS AddUsersToEncryptedFile Function (advapi32.dll)

Hi there,
I'm trying to encrypt Files with EFS, by using functions of the advapi32
dll.
But I have problems with the function 'AddUsersToEncryptFile'.

I have the name of the file which I want to encrypt and the accountname of
the users who should added to the file as an allowed user.
Now I try with the function 'LookupAccountName' to get the SID of the user,
that works fine. But now I don't know how to get the certificate of the user
and add this in a ENCRYPTION_CERTIFICATE_LIST which I can use for the
function 'AddUsersToEncryptedFile'.

My code I have still written:

Setup:

[StructLayout(LayoutKind.Sequential)]
internal struct ENCRYPTION_CERTIFICATE_LIST
{
internal int nUsers;
internal IntPtr pUsers;
}

[DllImport("advapi32.dll", EntryPoint = "AddUsersToEncryptedFile", CharSet =
CharSet.Unicode, SetLastError = true)]
private static extern bool addUsersToEncryptedFile(string lpFilename, IntPtr
pUsers);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool LookupAccountName(
string lpSystemName,
string lpAccountName,
[MarshalAs(UnmanagedType.LPArray)] byte[] Sid,
ref uint cbSid,
StringBuilder ReferencedDomainName,
ref uint cchReferencedDomainName,
out SID_NAME_USE peUse);

Logic:

int counter = 0;

foreach (FileInfo file in files)
{
//certificates is a list of the users which have to be added
IntPtr[] iptArray = new IntPtr[certificates.Count];
int zaehler = 0;

foreach (CertUser u in certificates)
{
//RegName is the user accountname
string accountName = u.RegName;
byte[] Sid = null;
uint cbSid = 0;
StringBuilder referencedDomainName = new StringBuilder();
uint cchReferencedDomainName = (uint)referencedDomainName.Capacity;
SID_NAME_USE sidUse;

int err = NO_ERROR;
if (!LookupAccountName(null, accountName, Sid, ref cbSid,
referencedDomainName, ref cchReferencedDomainName, out sidUse))
{
err = Marshal.GetLastWin32Error();
if (err == ERROR_INSUFFICIENT_BUFFER || err == ERROR_INVALID_FLAGS)
{
Sid = new byte[cbSid];
referencedDomainName.EnsureCapacity((int)cchRefere ncedDomainName);
err = NO_ERROR;
if (!LookupAccountName(null, accountName, Sid, ref cbSid,
referencedDomainName, ref cchReferencedDomainName, out sidUse))
err = Marshal.GetLastWin32Error();
}
}

int size = Marshal.SizeOf(Sid[0]) * Sid.Length;
IntPtr iptr = Marshal.AllocHGlobal(size);
Marshal.Copy(Sid, 0, iptr, Sid.Length);
iptArray[counter] = iptr;
counter++;
}
//Here I want to create the Encryption_Certificate_List and fill it
(needless to say it doesn't work ;-) )

IntPtr pointerAufPointerArray =
Marshal.AllocHGlobal(Marshal.SizeOf(iptArray[0]));
Marshal.Copy(iptArray, 0, pointerAufPointerArray, iptArray.Length);
ENCRYPTION_CERTIFICATE_LIST ecl =
(ENCRYPTION_CERTIFICATE_LIST)Marshal.PtrToStructur e(pointerAufPointerArray,
typeof(ENCRYPTION_CERTIFICATE_LIST));
IntPtr pEcl =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ENCRYPT ION_CERTIFICATE_LIST)));

Marshal.Copy(pointerAufPointerArray, bla.ToArray(), 0,
Marshal.SizeOf(typeof(ENCRYPTION_CERTIFICATE_LIST) ));
addUsersToEncryptedFile(file.FullName.ToString(), pEcl);
}
Thanks in advance for your help!

Kind regards
Silke
Oct 17 '08 #1
0 1565

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zeno Lee | last post by:
I'm trying to authenticate a user against a windows network. I want it to work across any kind of windows network from NT 4.0 up to Windows 2003 ADS. So far I've been using DirectoryEntry and...
3
by: mrzt | last post by:
the following codes won't work. ============================== using System; using System.Runtime; using System.Runtime.InteropServices; namespace Shutdown { /// <summary> /// Class1...
2
by: Dov P | last post by:
hello everyone, i need to use api function named RegSetKeySecurity in my C# program. This function require ADVAPI32.DLL, but i cannot add this reference. I tried to use Microsoft.Win32.Registry...
0
by: caldera | last post by:
hi, I want to forms authentication with windows password. So that I import advapi32.dll. Import code like this public static extern bool LogonUser(String lpszUsername,String lpszDomain,String...
2
by: kevin.kenny | last post by:
Hi All, Sorry to crosspost but it's a security and an ASP.NET problem I have. We run each website site under it's own I_<user> account and ASP.NET is configured to impersonate so requests run...
3
by: Parveen | last post by:
I'm trying to use the GetUserName API function in the advapi32.dll library. I declare my function as follows Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer...
2
by: Lance Geeck | last post by:
I have many items that I lifted off from Microsoft's website several years ago. These samples were in VB6. I now want to convert an application to VB.NET. I am getting an error that says "As Any...
1
by: raam_kimi | last post by:
Hi All I got some problem in importing advapi32.dll when i call the LogonUser method it throws an error like this 'Unable to find an entry point named LogonUser in DLL advapi32.dll.' ...
9
by: trini | last post by:
hi there, I found this code on your forum for which I am glad because I can use it pretty well IF there's a way to alter it and get a 32-character hashcode instead of 16! Anybody knows what's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.