System.Text.Encoding.Default.GetBytes
(1)
CngKeyBlobFormat.EccPrivateBlob
(1)
CngKeyBlobFormat.EccPublicBlob
(1)
CngExportPolicies.AllowPlaintextExport
(1)
CngAlgorithm.ECDsaP256
(1)
System.Security.Cryptography
(1)
ToBase64String
(1)
CngKey.Create
(1)

ECDsaCng elliptic curve class exception on import of private key

Asked By Steve_
16-Feb-08 05:35 AM
Hello everybody,

I was playing around with the new 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)

----------------- Code starts

Public Function CreateSignature(ByVal strString As String) As Boolean

'The exception occurs in the 'objCngKey = CngKey.Import' line on the
bottom

Dim data() As Byte
Dim bSignature() As Byte
Dim sSignature As String
Dim objCngKey As CngKey
Dim keyName As String = "ECDSA Key"
Dim publicKeyBytes(), privateKeyBytes() As Byte

'Check if CngKey exists - if so, delete it, if not create a new pair
of keys
If (CngKey.Exists(keyName)) Then
objCngKey = CngKey.Open(keyName)
objCngKey.Delete()
Else
Dim creationParameters = New CngKeyCreationParameters()
creationParameters.ExportPolicy =
CngExportPolicies.AllowPlaintextExport
objCngKey = CngKey.Create(CngAlgorithm.ECDsaP256, keyName,
creationParameters)
End If

'Export the public and the private key to byte arrays
publicKeyBytes = objCngKey.Export(CngKeyBlobFormat.EccPublicBlob)
privateKeyBytes = objCngKey.Export(CngKeyBlobFormat.EccPrivateBlob)

'Convert String to be signed to a byte array
data = System.Text.Encoding.Default.GetBytes(strString)

'Create a ECDsaCng Object
Dim ecdsa As ECDsaCng = New ECDsaCng(objCngKey)

'Sign the string
bSignature = ecdsa.SignData(data)

'Convert Signature to Base64 string for better reading
sSignature = Convert.ToBase64String(bSignature)

'Destroy Keypair
objCngKey.Delete()
objCngKey.Dispose()
ecdsa = Nothing

'Re-Import the keys created above
'Actually the code below was part of another function, to which I
pass the private key which was created by the code above
'The purpose of this was to sign another string with the same
private key

'Re-Import the previously exported public key - that works fine.
objCngKey = CngKey.Import(publicKeyBytes,
CngKeyBlobFormat.EccPublicBlob) 'That works fine
'NOW, here's the problem:
'>>>>>>>>>>
objCngKey = CngKey.Import(privateKeyBytes,
CngKeyBlobFormat.EccPrivateBlob)

'Here a cryptographic exception occurs
'>>>>>>>>>>
'Although I am re-importing the key exactly as exported above, a
cryptographic exception occurs.
'What am I doing wrong? Importing the public key works fine,
importing the private key fails...
'I also tried to only import the private key, without importing the
public key, but that does not help either ...
'Is this a bug in the ECDsaCng function, or am I doing anything wrong?

End Function

----------------- Code ends
Post Question To EggHeadCafe
around with the new ECDsaCng elliptic curve class of the System.Security.Cryptography namespace and have encountered a problem with importing and the private key to byte arrays publicKeyBytes = objCngKey.Export(CngKeyBlobFormat.EccPublicBlob) privateKeyBytes = objCngKey.Export(CngKeyBlobFormat.EccPrivateBlob) 'Convert String to be signed to a byte array data = System.Text.Encoding.Default GetBytes(strString) 'Create a ECDsaCng Object Dim ecdsa As ECDsaCng = New
to create hanging indents like these: style = "vertical-align: top"> Text text text text text text text text text text text text text text text text text text text text
NET Framework Encoding of Text Files Hi Currently text file that I create are UTF-8 encoded and I need ANSI encoding how can I do that Thank you, Samuel ng IIRC System.Text.Encoding.Default maps to ANSI / / / / / / / / / / / / / / / / / / / Option Strict On Imports System.IO Imports System.Text Module Module1 Sub Main() Dim text As String = 3D
NET Framework Text in cell not-conforming to cell size. . . Ok, I've but for some reason I had a piece of continuous text as such (including quotes): 'text''text''text''text''text''text' and this, for some reason, was being displayed as a single line within the browser. This text was inside a td of a properly formed table and the width of the cell was set explicitly, however the text forced the cell to be width of the text itself
encodeing and decodeing image strings to base64. My.Types.ps1xml [System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($this)) [System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($this)) Update-TypeData c: \ scripts \ My.Types.ps1xml is empty. Doing a GM on both says they are System.String and have all the same methods and properties includeing
NET Framework Encoding Text für writing a textfile Hi everybody, i have a problem I want to force the Stream to write an ANSI-text. For reading from an filestream i can define the type of ASCI by setting System.Text.Encoding.Default. What about Wrting Textdate ? The result seems to be UTF7 ASCII"? There's only one ASCII. How do you set System.Text.Encoding.Default, given that it's a read-only property? As
ich erstelle ganz normal mit myWriter = new StreamWriter(path, false, System.Text.Encoding.Default); einen Streamwriter und schreibe recht lange Zeilen mittels WriteLine. Die lasse ich mal einen eigene Kommentar dazu) : [StreamWriter.Close-Methode (System.IO)] http: / / msdn.microsoft.com / de-de / library / system.io.streamwriter.close.aspx [StreamWriter.AutoFlush-Eigenschaft (System.IO)] http: / / msdn.microsoft.com / de-de / library system.io.streamwriter.autoflush.aspx [StreamWriter.Flush-Methode (System.IO)] http: / / msdn.microsoft.com / de-de / library / system.io.streamwriter.flush.aspx Es kann in einigen F?llen
NET Framework TextWriter encoding Hello, I am trying to change the encoding for a textwriter: TextWriter w = new StringWriter(); XmlTextWriter xmlWriter = new XmlTextWriter(w); w.Encoding = System.Text.Encoding.Unicode; But I am getting an error Property or indexer System.IO.TextWriter.Encoding' cannot be assigned to - - it is read only How do w = 3D new StringWriter(); XmlTextWriter xmlWriter = 3D new XmlTextWriter(w); Encoding unicode = 3D Encoding.Unicode; w.Encoding = 3D unicode; I think you set up