Password for a drawing can be set through program by using the “Database.SaveAs” method. Here is a sample code to demonstrate the use of the “SecurityParameters” to password protect your drawing.
The links provided in the code refer to MSDN documentation. These can help you choose a provider and the key length. Since “RC4” is the only algorithm supported by AutoCAD, it important to select the key length from the list keeping this in mind.
[CommandMethod("PwdProtect")]
static public void PwdProtectMethod()
{
String dwgFilePath = "C:\Test.dwg";
if (!System.IO.File.Exists(dwgFilePath))
return;
string password = "Autodesk";
// Choose a provider name from the list of providers
//http://msdn.microsoft.com/en-us/library/windows/desktop/
//aa380243(v=vs.85).aspx
string providerName
= "Microsoft Enhanced Cryptographic Provider v1.0";
string subject = null;
string issuer = null;
string serialNumber = null;
string comment = null;
string timeServer = null;
SecurityActions actions = SecurityActions.EncryptData;
SecurityAlgorithm algorithm = SecurityAlgorithm.RC4;
// Find the default/min/max key length
// for the provider name used
// http://msdn.microsoft.com/en-us/library/windows/desktop/
// bb931357(v=vs.85).aspx
uint keyLength = 128;
uint providerType = 1;
try
{
using (Database db = new Database(false, true))
{
db.ReadDwgFile
(
dwgFilePath,
System.IO.FileShare.ReadWrite,
true,
""
);
SecurityParameters sp
= new SecurityParameters
(
password,
providerName,
subject,
issuer,
serialNumber,
comment,
timeServer,
actions,
algorithm,
keyLength,
providerType
);
db.SaveAs
(
db.Filename,
false,
DwgVersion.Current,
sp
);
}
}
catch (System.Exception ex)
{
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(ex.Message);
}
}
-Balaji

Leave a Reply