using CryptUnprotectData api from c#

Z

ziggy

Hi,

i'm trying to use function from crypt32.dll in dot net, and get exception:

An unhandled exception of type 'System.TypeLoadException' occurred in
WindowsApplication1.exe

Additional information: Could not load type WinApis from assembly
WindowsApplication1, Version=1.0.2006.19524, Culture=neutral,
PublicKeyToken=null because the method CryptUnprotectData has no RVA.


can anyone shade some light on the subject?

TIA, z.
 
M

Mattias Sjögren

can anyone shade some light on the subject?

Can you post your code?



Mattias
 
Z

z f

this is a windows application with 2 text boxes and one button:



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Text;

using System.Security;

using System.Runtime.CompilerServices;

using System.Runtime.InteropServices;

using System.Security.Permissions;

namespace WindowsApplication1

{

/// <summary>

/// Summary description for Form1.

/// </summary>

///

//[StrongNameIdentityPermission(SecurityAction.LinkDemand,
PublicKey="00240000048000009400000006020000002400005253413100040000010001002
72736AD6E5F9586BAC2D531EABC3ACC666C2F8EC879FA94F8F7B0327D2FF2ED523448F83C3D5
C5DD2DFC7BC99C5286B2C125117BF5CBE242B9D41750732B2BDFFE649C6EFB8E5526D526FDD1
30095ECDB7BF210809C6CDAD8824FAA9AC0310AC3CBA2AA0523567B2DFA7FE250B30FACBD62D
4EC99B94AC47C7D3B28F1F6E4C8")]

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.TextBox txtIn;

private System.Windows.Forms.TextBox txtOut;

private System.Windows.Forms.Button btnDec;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.txtIn = new System.Windows.Forms.TextBox();

this.txtOut = new System.Windows.Forms.TextBox();

this.btnDec = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// txtIn

//

this.txtIn.Location = new System.Drawing.Point(8, 8);

this.txtIn.Multiline = true;

this.txtIn.Name = "txtIn";

this.txtIn.Size = new System.Drawing.Size(328, 104);

this.txtIn.TabIndex = 0;

this.txtIn.Text = "";

//

// txtOut

//

this.txtOut.Location = new System.Drawing.Point(8, 200);

this.txtOut.Multiline = true;

this.txtOut.Name = "txtOut";

this.txtOut.Size = new System.Drawing.Size(328, 104);

this.txtOut.TabIndex = 0;

this.txtOut.Text = "textBox1";

//

// btnDec

//

this.btnDec.Location = new System.Drawing.Point(128, 144);

this.btnDec.Name = "btnDec";

this.btnDec.TabIndex = 1;

this.btnDec.Text = "try";

this.btnDec.Click += new System.EventHandler(this.btnDec_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(528, 453);

this.Controls.Add(this.btnDec);

this.Controls.Add(this.txtIn);

this.Controls.Add(this.txtOut);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void btnDec_Click(object sender, System.EventArgs e)

{

//MessageBox.Show ("test");

txtOut.Text = LocalUnprotectData ( txtIn.Text);

}

public static string LocalUnprotectData(string data)

{

byte[] buffer1 = Convert.FromBase64String(data);

byte[] buffer2 = LocalUnprotectData(buffer1);

string text1 = null;

if (buffer2 == null)

{

return text1;

}

while (buffer2.Length != 0)

{

return Encoding.Unicode.GetString(buffer2);

}

return string.Empty;

}


public static byte[] LocalUnprotectData(byte[] data)

{

return UnprotectData(data, 1);

}


private static byte[] UnprotectData(byte[] data, int dwFlags)

{

NativeMethods.DATA_BLOB data_blob1;

NativeMethods.DATA_BLOB data_blob2;

byte[] buffer1 = null;

data_blob1 = new NativeMethods.DATA_BLOB();

data_blob1.cbData = data.Length;

data_blob1.pbData = Marshal.AllocHGlobal(data_blob1.cbData);

Marshal.Copy(data, 0, data_blob1.pbData, data_blob1.cbData);

data_blob2 = new NativeMethods.DATA_BLOB();

try

{

if (NativeMethods.CryptUnprotectData(ref data_blob1, null, IntPtr.Zero,
IntPtr.Zero, IntPtr.Zero, dwFlags, ref data_blob2))

{

buffer1 = new byte[data_blob2.cbData];

Marshal.Copy(data_blob2.pbData, buffer1, 0, data_blob2.cbData);

byte[] buffer2 = new byte[data_blob2.cbData];

Marshal.Copy(data_blob2.pbData, buffer2, 0, data_blob2.cbData);

NativeMethods.LocalFree(data_blob2.pbData);

data_blob2.pbData = IntPtr.Zero;

return buffer1;

}

int num1 = Marshal.GetLastWin32Error();

string text1 = string.Format("CryptUnprotectData: Win32 error:{0}", num1);

throw new Exception(text1);

}

finally

{

if (data_blob1.pbData != IntPtr.Zero)

{

Marshal.FreeHGlobal(data_blob1.pbData);

}

while (data_blob2.pbData != IntPtr.Zero)

{

NativeMethods.LocalFree(data_blob2.pbData);

data_blob2.pbData = IntPtr.Zero;

break;

}

}

return buffer1;

}


}


}

internal sealed class NativeMethods

{

// Methods

public NativeMethods()

{

}


// [DllImport("crypt32", CharSet=CharSet.Auto, SetLastError=true)]

// internal static extern bool CryptProtectData(ref DATA_BLOB dataIn, string
szDataDescr, IntPtr optionalEntropy, IntPtr pvReserved, IntPtr
pPromptStruct, int dwFlags, ref DATA_BLOB pDataOut);

[DllImport("crypt32", CharSet=CharSet.Auto, SetLastError=true)]

internal static extern bool CryptUnprotectData(ref DATA_BLOB dataIn,
StringBuilder ppszDataDescr, IntPtr optionalEntropy, IntPtr pvReserved,
IntPtr pPromptStruct, int dwFlags, ref DATA_BLOB pDataOut);

[DllImport("kernel32.dll")]

internal static extern IntPtr LocalFree(IntPtr hMem);



// Nested Types

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]

public struct DATA_BLOB

{

public int cbData;

public IntPtr pbData;

}

}
 
M

Mattias Sjögren

And when exactly are you getting the exception? The code compiles and
loads fine here.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top