How to integrate support for securing a property in my .NET application?

ImportantThe following article uses options that are available starting with the Professional edition and project type.

Step 1 - Loading and initializing the SecureProp library

This code needs to be added to your C# application.

 
using System.Runtime.InteropServices;

// Match the calling convention of the "GetRevealedTextLength" exported function
[DllImport("SecureProp.dll", CharSet = CharSet.Unicode)]
private static extern uint GetRevealedTextLength(string aSecuredText);

// Match the calling convention of the "RevealText" exported function
[DllImport("SecureProp.dll", CharSet = CharSet.Unicode)]
private static extern void RevealText(string aSecuredText, string aPlainTextBuffer, uint aBufferLength);
      

This code needs to be added to your VB.NET application.

 
' Match the calling convention of the "GetRevealedTextLength" exported function
  <DllImport("SecureProp.dll", CharSet:=CharSet.Unicode)>
  Private Shared Function GetRevealedTextLength(ByVal aSecuredText As String) As UInteger
  End Function
  ' Match the calling convention of the "RevealText" exported function
  <DllImport("SecureProp.dll", CharSet:=CharSet.Unicode)>
  Private Shared Sub RevealText(ByVal aSecuredText As String, ByVal aPlainTextBuffer As String, ByVal aBufferLength As UInteger)
  End Sub
      

SecureProp library exported functions

  • GetRevealedTextLength - the DLL exported function that reveals the secured text and returns its length
  • RevealText - the DLL exported function that reveals the secured text and stores the result in the provided buffer

Step 2 - Using the SecureProp library

This code needs to be added to your C# application.

 
private static void SecureProps()
{
  // Load the properties from the ini file
  List<SecuredProperty> properties = new List<SecuredProperty>();
  LoadPropertiesFromIni(ref properties);
  if (properties.Count == 0)
    return;
  //------------------------------------------------------------------------------------------
  // Use the SecureProp library
  //------------------------------------------------------------------------------------------
  foreach (SecuredProperty property in properties)
  {
    string propValue = property.mValue;
    //------------------------------------------------------------------------------------------
    // Exported function call
    // Reveal the secured text and return its length
    //------------------------------------------------------------------------------------------
    uint textLength = GetRevealedTextLength(propValue);
    // Allocate a buffer of wide characters with the length: textLength + 1
    // We need to add 1 in order to provide space for the null terminator
    char[] textBuffer = new char[++textLength];
    string textBufferStr = new string(textBuffer);
    //------------------------------------------------------------------------------------------
    // Exported function call
    // Reveal the secured text and store the result in the provided buffer
    //------------------------------------------------------------------------------------------
    RevealText(propValue, textBufferStr, textLength);
    // Output the revealed text
    System.Console.WriteLine(property.mName + " = " + textBufferStr);
  }
}

This code needs to be added to your VB.NET application.

 
Private Shared Sub SecureProps()
  ' Load the properties from the ini file
  Dim properties As List(Of SecuredProperty) = New List(Of SecuredProperty)
  LoadPropertiesFromIni(properties)
  If (properties.Count.Equals(0)) Then
    Return
  End If
  '------------------------------------------------------------------------------------------
  ' Use the SecureProp library
  '------------------------------------------------------------------------------------------
  For Each securedProp As SecuredProperty In properties
    Dim propValue As String = securedProp.mValue
    '------------------------------------------------------------------------------------------
    ' Exported function call
    ' Reveal the secured text And return its length
    '------------------------------------------------------------------------------------------
    Dim textLength As UInteger = GetRevealedTextLength(propValue)
    ' Allocate a buffer of wide characters with the length textLength + 1
    ' We need to add 1 in order to provide space for the null terminator
    textLength += 1
    Dim textBuffer As Char() = New Char(textLength) {}
    Dim textBufferStr As String = New String(textBuffer)
    '------------------------------------------------------------------------------------------
    ' Exported function call
    ' Reveal the secured text And store the result in the provided buffer
    '------------------------------------------------------------------------------------------
    RevealText(propValue, textBufferStr, textLength)
    ' Output the revealed text
    System.Console.WriteLine(securedProp.mName + " = " + textBufferStr)
  Next
End Sub

Sample Project

Download the C# sample or the VB.NET sample showcasing how the secure property functionality works.