Click here to Skip to main content
16,007,885 members
Home / Discussions / C#
   

C#

 
GeneralThe using statement Pin
paululvinius5-Feb-05 1:06
paululvinius5-Feb-05 1:06 
GeneralRe: The using statement Pin
Robert Rohde5-Feb-05 1:17
Robert Rohde5-Feb-05 1:17 
GeneralRunning Without a form GUI Pin
Amir Jalaly5-Feb-05 0:03
Amir Jalaly5-Feb-05 0:03 
GeneralRe: Running Without a form GUI Pin
Gary Thom5-Feb-05 1:18
Gary Thom5-Feb-05 1:18 
Generalcommunication between a c# application and C++ com Pin
yaya1014-Feb-05 21:45
yaya1014-Feb-05 21:45 
GeneralRe: communication between a c# application and C++ com Pin
Heath Stewart5-Feb-05 4:52
protectorHeath Stewart5-Feb-05 4:52 
GeneralRe: communication between a c# application and C++ com Pin
yaya1017-Feb-05 7:24
yaya1017-Feb-05 7:24 
GeneralOdd SSL Public Key Pin
bigals4-Feb-05 18:25
bigals4-Feb-05 18:25 
Hello everyone

I have written a little app to manage certificates.

Now getting the PRIVATE key from the certificate is, quick, simple and straight forward... but I just get a constant result for the Public Key...

here is an example (I have vut out most of the PVK, but you get the drift Wink | ;) )
<result>
Public Key
<rsakeyvalue><modulus><exponent>AA==

Private Key
<rsakeyvalue><modulus>23aDb/XXXXXXXXXXXXXXXXXX+......=


Here is the code I have to get the Public key

<br />
using System;<br />
using CAPICOM;<br />
using System.Security.Cryptography;<br />
using System.Security.Cryptography.X509Certificates;<br />
using System.Runtime.InteropServices;<br />
using System.Windows.Forms;<br />
using System.Text;<br />
<br />
namespace CertificateManager<br />
{<br />
	[StructLayout(LayoutKind.Sequential)]<br />
	public struct PUBKEYBLOBHEADERS <br />
	{<br />
		public byte bType;	//BLOBHEADER<br />
		public byte bVersion;	//BLOBHEADER<br />
		public short reserved;	//BLOBHEADER<br />
		public uint aiKeyAlg;	//BLOBHEADER<br />
		public uint magic;	//RSAPUBKEY<br />
		public uint bitlen;	//RSAPUBKEY<br />
		public uint pubexp;	//RSAPUBKEY<br />
	}<br />
<br />
	public class PublicKey<br />
	{		<br />
		const uint CERT_SYSTEM_STORE_CURRENT_USER	= 0x00010000;<br />
		const uint CERT_STORE_READONLY_FLAG		= 0x00008000;<br />
		const uint CERT_STORE_OPEN_EXISTING_FLAG	= 0x00004000;<br />
		const uint CERT_FIND_SUBJECT_STR	= 0x00080007;<br />
		const uint X509_ASN_ENCODING 		= 0x00000001;<br />
		const uint PKCS_7_ASN_ENCODING 	= 0x00010000;<br />
		const uint RSA_CSP_PUBLICKEYBLOB	= 19;<br />
		const int  AT_KEYEXCHANGE		= 1;  //keyspec values<br />
		const int  AT_SIGNATURE		= 2;<br />
		static uint ENCODING_TYPE 		= PKCS_7_ASN_ENCODING | X509_ASN_ENCODING ;<br />
		<br />
		[DllImport("crypt32.dll")]<br />
		public static extern bool CryptDecodeObject(<br />
			uint CertEncodingType,<br />
			uint lpszStructType,<br />
			byte[] pbEncoded,<br />
			uint cbEncoded,<br />
			uint flags,<br />
			[In, Out] byte[] pvStructInfo,<br />
			ref uint cbStructInfo);<br />
<br />
<br />
		private byte[] _modulus;<br />
		private byte[] _exponent;<br />
<br />
		public byte[] Modulus<br />
		{<br />
			get{return _modulus;}<br />
			set{_modulus = value;}<br />
		}<br />
<br />
		public byte[] Exponent<br />
		{<br />
			get{return _exponent;}<br />
			set{_exponent = value;}<br />
		}<br />
<br />
		public PublicKey(X509Certificate cert)<br />
		{<br />
			byte[] publickeyblob = null;<br />
			byte[] encodedpubkey = cert.GetPublicKey(); //asn.1 encoded public key<br />
<br />
			uint blobbytes=0;<br />
		<br />
			if(CryptDecodeObject(ENCODING_TYPE, RSA_CSP_PUBLICKEYBLOB, encodedpubkey, (uint)encodedpubkey.Length, 0, null, ref blobbytes))<br />
			{<br />
				publickeyblob = new byte[blobbytes];				<br />
				MessageBox.Show(Encoding.ASCII.GetString(publickeyblob));<br />
			}<br />
<br />
	 <br />
			PUBKEYBLOBHEADERS pkheaders = new PUBKEYBLOBHEADERS() ;<br />
			int headerslength = Marshal.SizeOf(pkheaders);<br />
			IntPtr buffer = Marshal.AllocHGlobal( headerslength);<br />
			Marshal.Copy( publickeyblob, 0, buffer, headerslength );<br />
			pkheaders = (PUBKEYBLOBHEADERS) Marshal.PtrToStructure( buffer, typeof(PUBKEYBLOBHEADERS) );<br />
			Marshal.FreeHGlobal( buffer );<br />
<br />
<br />
			//-----  Get public key size in bits -------------<br />
			//this.certkeysize = pkheaders.bitlen;<br />
<br />
			//-----  Get public exponent -------------<br />
			byte[] exponent = BitConverter.GetBytes(pkheaders.pubexp); //little-endian ordered<br />
			Array.Reverse(exponent);    //convert to big-endian order<br />
			this.Exponent = exponent;<br />
			<br />
<br />
			//-----  Get modulus  -------------<br />
			int modulusbytes = (int)pkheaders.bitlen/8 ;<br />
			byte[] modulus = new byte[modulusbytes];<br />
			try<br />
			{<br />
				Array.Copy(publickeyblob, headerslength, modulus, 0, modulusbytes);<br />
				Array.Reverse(modulus);   //convert from little to big-endian ordering.<br />
				this.Modulus = modulus;<br />
				<br />
			}<br />
			catch(Exception)<br />
			{<br />
			}<br />
		}<br />
	}<br />
}<br />
<br />


Can anyone tell me why I am getting such an odd result for the public key?

Hope someone can shed some light on this, as it's been driving me nuts! Sigh | :sigh:

Thanks in advance!
GeneralStop and Start MSDOS C# App Pin
StephenMcAllister4-Feb-05 18:17
StephenMcAllister4-Feb-05 18:17 
GeneralRe: Stop and Start MSDOS C# App Pin
Heath Stewart5-Feb-05 4:44
protectorHeath Stewart5-Feb-05 4:44 
GeneralAccess Database Pin
Cappuccino_David4-Feb-05 16:51
Cappuccino_David4-Feb-05 16:51 
GeneralRe: Access Database Pin
Yulianto.4-Feb-05 17:16
Yulianto.4-Feb-05 17:16 
GeneralRe: Access Database Pin
Luis Alonso Ramos4-Feb-05 19:23
Luis Alonso Ramos4-Feb-05 19:23 
GeneralRe: Access Database Pin
Cappuccino_David5-Feb-05 8:23
Cappuccino_David5-Feb-05 8:23 
GeneralRe: Access Database Pin
Luis Alonso Ramos5-Feb-05 8:30
Luis Alonso Ramos5-Feb-05 8:30 
GeneralonMaximize() or onResize() questions... Pin
new_phoenix4-Feb-05 16:46
new_phoenix4-Feb-05 16:46 
GeneralRe: onMaximize() or onResize() questions... Pin
Heath Stewart5-Feb-05 3:39
protectorHeath Stewart5-Feb-05 3:39 
GeneralRe: onMaximize() or onResize() questions... Pin
new_phoenix5-Feb-05 8:20
new_phoenix5-Feb-05 8:20 
GeneralRe: onMaximize() or onResize() questions... Pin
Heath Stewart6-Feb-05 5:35
protectorHeath Stewart6-Feb-05 5:35 
GeneralRe: onMaximize() or onResize() questions... Pin
new_phoenix6-Feb-05 12:12
new_phoenix6-Feb-05 12:12 
GeneralRe: onMaximize() or onResize() questions... Pin
Heath Stewart7-Feb-05 6:36
protectorHeath Stewart7-Feb-05 6:36 
GeneralBest Practices - Serializing Dialog Data Pin
Newbie_T4-Feb-05 16:37
Newbie_T4-Feb-05 16:37 
GeneralRe: Best Practices - Serializing Dialog Data Pin
Heath Stewart5-Feb-05 3:30
protectorHeath Stewart5-Feb-05 3:30 
QuestionHow to modify the tnsnames.ora file by method? Pin
Colinyin4-Feb-05 13:06
Colinyin4-Feb-05 13:06 
GeneralCant get drag-n-drop from Excel to work. Please help. Pin
Flack4-Feb-05 12:14
Flack4-Feb-05 12:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.