Click here to Skip to main content
15,909,656 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: How to copy form files from one project to another without loosing the desinger view [modified] Pin
Pritish20-Feb-08 3:55
Pritish20-Feb-08 3:55 
GeneralCustom Collection Pin
CodingYoshi18-Feb-08 11:29
CodingYoshi18-Feb-08 11:29 
GeneralRe: Custom Collection Pin
Patrick Etc.18-Feb-08 12:05
Patrick Etc.18-Feb-08 12:05 
GeneralRe: Custom Collection Pin
CodingYoshi18-Feb-08 16:51
CodingYoshi18-Feb-08 16:51 
GeneralRe: Custom Collection Pin
darkelv18-Feb-08 17:01
darkelv18-Feb-08 17:01 
GeneralRe: Custom Collection Pin
Patrick Etc.18-Feb-08 18:54
Patrick Etc.18-Feb-08 18:54 
GeneralCustomizing web part title border in MOSS 2007. Pin
sdklfjlkdsfjdklsfjkdslfjdslkf18-Feb-08 2:58
sdklfjlkdsfjdklsfjkdslfjdslkf18-Feb-08 2:58 
QuestionECDsaCng elliptic curve class produces exception on import of private key Pin
Steve_K17-Feb-08 23:40
Steve_K17-Feb-08 23:40 
Hello everybody,

I was playing around with the new .net 3.5 ECDsaCng elliptic curve class of the System.Security.Cryptography namespace and have encountered a problem with importing a previously exported private key. The strange thing is that importing a public key works fine, but importing a private key always fails with a cryptographic exception, although I am calling the import function properly, as far as I can tell.

Before this drives me nuts, I thought I should post the code here - maybe someone can have a look at it and check whether I am missing something, or whether this is a bug within the ECDsaCng class itself.

Here's the VB2008 code - the section where the error occurs is marked (bottom of function)

<br />
<br />
Imports System.Security.Cryptography<br />
<br />
        Public Function CreateSignature(ByVal strString As String) As Boolean<br />
<br />
        'The exception occurs in the 'objCngKey = CngKey.Import' line on the bottom<br />
<br />
        Dim data() As Byte<br />
        Dim bSignature() As Byte<br />
        Dim sSignature As String<br />
        Dim objCngKey As CngKey<br />
        Dim keyName As String = "ECDSA Key"<br />
        Dim publicKeyBytes(), privateKeyBytes() As Byte<br />
<br />
        'Check if CngKey exists - if so, delete it, if not create a new pair of keys<br />
        If (CngKey.Exists(keyName)) Then<br />
            objCngKey = CngKey.Open(keyName)<br />
            objCngKey.Delete()<br />
        Else<br />
            Dim creationParameters = New CngKeyCreationParameters()<br />
            creationParameters.ExportPolicy = CngExportPolicies.AllowPlaintextExport<br />
            objCngKey = CngKey.Create(CngAlgorithm.ECDsaP256, keyName, creationParameters)<br />
        End If<br />
<br />
        'Export the public and the private key to byte arrays<br />
        publicKeyBytes = objCngKey.Export(CngKeyBlobFormat.EccPublicBlob)<br />
        privateKeyBytes = objCngKey.Export(CngKeyBlobFormat.EccPrivateBlob)<br />
<br />
        'Convert String to be signed to a byte array<br />
        data = System.Text.Encoding.Default.GetBytes(strString)<br />
<br />
        'Create a ECDsaCng Object<br />
        Dim ecdsa As ECDsaCng = New ECDsaCng(objCngKey)<br />
<br />
        'Sign the string<br />
        bSignature = ecdsa.SignData(data)<br />
<br />
        'Convert Signature to Base64 string for better reading<br />
        sSignature = Convert.ToBase64String(bSignature)<br />
<br />
        'Destroy Keypair<br />
        objCngKey.Delete()<br />
        objCngKey.Dispose()<br />
        ecdsa = Nothing<br />
<br />
        'Re-Import the keys created above<br />
        'Actually the code below was part of another function, to which I pass the private key which was created by the code above<br />
        'The purpose of this was to sign another string with the same private key<br />
<br />
        'Re-Import the previously exported public key - that works fine.<br />
        objCngKey = CngKey.Import(publicKeyBytes, CngKeyBlobFormat.EccPublicBlob) 'That works fine<br />
        'NOW, here's where the trouble starts:<br />
        '>>>>>>>>>><br />
        objCngKey = CngKey.Import(privateKeyBytes, CngKeyBlobFormat.EccPrivateBlob)<br />
<br />
'Here a cryptographic exception occurs<br />
        '>>>>>>>>>><br />
        'Although I am re-importing the key exactly as exported above, a cryptographic exception occurs.<br />
        'What am I doing wrong? Importing the public key works fine, importing the private key fails...<br />
        'I also tried to only import the private key, without importing the public key, but that does not help either ...<br />
        'Is this a bug in the ECDsaCng function, or am I doing anything wrong?<br />
<br />
<br />
    End Function<br />
<br />


Thanks for any help and for pointing me into the right direction.

TIA,

Steve
GeneralAdding new syntax highlighting Pin
barbq200017-Feb-08 2:55
barbq200017-Feb-08 2:55 
GeneralRe: Adding new syntax highlighting Pin
Pete O'Hanlon17-Feb-08 10:25
mvePete O'Hanlon17-Feb-08 10:25 
GeneralStreamWriter.WriteLine converting hex A0 to hex EF BF BD Pin
Mike Bluett16-Feb-08 13:46
Mike Bluett16-Feb-08 13:46 
GeneralRe: StreamWriter.WriteLine converting hex A0 to hex EF BF BD Pin
Guffa17-Feb-08 4:22
Guffa17-Feb-08 4:22 
GeneralRe: StreamWriter.WriteLine converting hex A0 to hex EF BF BD Pin
Mike Bluett17-Feb-08 8:13
Mike Bluett17-Feb-08 8:13 
GeneralRe: StreamWriter.WriteLine converting hex A0 to hex EF BF BD Pin
Guffa17-Feb-08 10:11
Guffa17-Feb-08 10:11 
GeneralDataGridTableStyle equivalent for DataGridView Pin
keniagm15-Feb-08 7:40
keniagm15-Feb-08 7:40 
GeneralRe: DataGridTableStyle equivalent for DataGridView Pin
led mike15-Feb-08 7:56
led mike15-Feb-08 7:56 
GeneralRe: DataGridTableStyle equivalent for DataGridView Pin
keniagm15-Feb-08 8:07
keniagm15-Feb-08 8:07 
GeneralRe: DataGridTableStyle equivalent for DataGridView Pin
led mike15-Feb-08 8:15
led mike15-Feb-08 8:15 
GeneralRe: DataGridTableStyle equivalent for DataGridView Pin
keniagm15-Feb-08 8:27
keniagm15-Feb-08 8:27 
GeneralRe: DataGridTableStyle equivalent for DataGridView Pin
led mike15-Feb-08 9:05
led mike15-Feb-08 9:05 
GeneralRe: DataGridTableStyle equivalent for DataGridView Pin
keniagm15-Feb-08 9:20
keniagm15-Feb-08 9:20 
GeneralRe: DataGridTableStyle equivalent for DataGridView Pin
Member 1190124510-Mar-21 18:29
Member 1190124510-Mar-21 18:29 
GeneralRe: DataGridTableStyle equivalent for DataGridView Pin
Richard Deeming10-Mar-21 21:30
mveRichard Deeming10-Mar-21 21:30 
Question[Message Deleted] Pin
Ippacciani15-Feb-08 7:40
Ippacciani15-Feb-08 7:40 
GeneralRe: hashTable Pin
led mike15-Feb-08 8:04
led mike15-Feb-08 8:04 

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.