Discussion:
PublicKeyFactor.CreateKey argument exception
Bogdan Suvar
2013-12-05 16:20:39 UTC
Permalink
Hi,
I'm currently trying to create an AsymmetricKeyParameter public key
parameter from an xml string like this:

public static AsymmetricKeyParameter xmlStringToPubKey(string
xmlString)
{
//AsymmetricCipherKeyPair keyPair;
if (String.IsNullOrEmpty(xmlString))
{
Console.WriteLine("Empty XML string received");
return null;
}
else
{
XDocument xdoc = XDocument.Parse(xmlString);
return
PublicKeyFactory.CreateKey(Streamify(xdoc.Descendants("Modulus").First().Value));
}
}

However, I'm getting an exception when calling CreateKey on
PublicKeyFactory obj. xdoc.Descendants returns a string which needs to
be converted into an array of bytes. I've tried different ways to
convert the string:
- Encoding.Unicode.getBytes(xmlString)
- getBytes(xmlString) method I've created to return a byte array from a
string
- Streamify - another method I've created to return a Stream

While I get different exceptions on each of the above methods none seem
to work. Any hints appreciated.
Bogdan Suvar
2013-12-05 16:35:23 UTC
Permalink
Another exception is EndOfStreamException:
{"DEF length 88 object truncated by 2"}
with the following stack trace:
at Org.BouncyCastle.Asn1.DefiniteLengthInputStream.ToArray()
at Org.BouncyCastle.Asn1.Asn1InputStream.BuildObject(Int32 tag,
Int32 tagNo, Int32 length)
at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
at Org.BouncyCastle.Asn1.Asn1Object.FromByteArray(Byte[] data)
at Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(Byte[]
keyInfoData)
at TestConsole.PGP.xmlStringToPubKey(String xmlString) in
PGP.cs:line 141
at TestConsole.Test.Main(String[] args) in Test.cs:line 22
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Post by Bogdan Suvar
Hi,
I'm currently trying to create an AsymmetricKeyParameter public key
public static AsymmetricKeyParameter xmlStringToPubKey(string
xmlString)
{
//AsymmetricCipherKeyPair keyPair;
if (String.IsNullOrEmpty(xmlString))
{
Console.WriteLine("Empty XML string received");
return null;
}
else
{
XDocument xdoc = XDocument.Parse(xmlString);
return
PublicKeyFactory.CreateKey(Streamify(xdoc.Descendants("Modulus").First().Value));
}
}
However, I'm getting an exception when calling CreateKey on
PublicKeyFactory obj. xdoc.Descendants returns a string which needs to
be converted into an array of bytes. I've tried different ways to
- Encoding.Unicode.getBytes(xmlString)
- getBytes(xmlString) method I've created to return a byte array from
a string
- Streamify - another method I've created to return a Stream
While I get different exceptions on each of the above methods none
seem to work. Any hints appreciated.
David Hook
2013-12-07 00:44:25 UTC
Permalink
The PublicKeyFactory is meant to interpret BER encoded key data. It
doesn't sound like that is what you are dealing with though.

You haven't provided a sample of the input below, but looking at the
code it appears the XML is presenting you with the public key as a
series of encoded numbers. If that is the case you need to convert the
elements to BigInteger objects first and then use those to create a
parameters object.

Regards,

David
Post by Bogdan Suvar
{"DEF length 88 object truncated by 2"}
at Org.BouncyCastle.Asn1.DefiniteLengthInputStream.ToArray()
at Org.BouncyCastle.Asn1.Asn1InputStream.BuildObject(Int32 tag,
Int32 tagNo, Int32 length)
at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
at Org.BouncyCastle.Asn1.Asn1Object.FromByteArray(Byte[] data)
at Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(Byte[]
keyInfoData)
at TestConsole.PGP.xmlStringToPubKey(String xmlString) in
PGP.cs:line 141
at TestConsole.Test.Main(String[] args) in Test.cs:line 22
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Post by Bogdan Suvar
Hi,
I'm currently trying to create an AsymmetricKeyParameter public key
public static AsymmetricKeyParameter xmlStringToPubKey(string
xmlString)
{
//AsymmetricCipherKeyPair keyPair;
if (String.IsNullOrEmpty(xmlString))
{
Console.WriteLine("Empty XML string received");
return null;
}
else
{
XDocument xdoc = XDocument.Parse(xmlString);
return
PublicKeyFactory.CreateKey(Streamify(xdoc.Descendants("Modulus").First().Value));
}
}
However, I'm getting an exception when calling CreateKey on
PublicKeyFactory obj. xdoc.Descendants returns a string which needs to
be converted into an array of bytes. I've tried different ways to
- Encoding.Unicode.getBytes(xmlString)
- getBytes(xmlString) method I've created to return a byte array from
a string
- Streamify - another method I've created to return a Stream
While I get different exceptions on each of the above methods none
seem to work. Any hints appreciated.
Bogdan Suvar
2013-12-07 07:23:58 UTC
Permalink
Thanks David
I've modified the method to take an RSAParameters object and use their
RSAParameters.Modulus and Exponent for creating the new BigInteger
parameters.
Post by David Hook
The PublicKeyFactory is meant to interpret BER encoded key data. It
doesn't sound like that is what you are dealing with though.
You haven't provided a sample of the input below, but looking at the
code it appears the XML is presenting you with the public key as a
series of encoded numbers. If that is the case you need to convert the
elements to BigInteger objects first and then use those to create a
parameters object.
Regards,
David
Post by Bogdan Suvar
{"DEF length 88 object truncated by 2"}
at Org.BouncyCastle.Asn1.DefiniteLengthInputStream.ToArray()
at Org.BouncyCastle.Asn1.Asn1InputStream.BuildObject(Int32 tag,
Int32 tagNo, Int32 length)
at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
at Org.BouncyCastle.Asn1.Asn1Object.FromByteArray(Byte[] data)
at Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(Byte[]
keyInfoData)
at TestConsole.PGP.xmlStringToPubKey(String xmlString) in
PGP.cs:line 141
at TestConsole.Test.Main(String[] args) in Test.cs:line 22
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Post by Bogdan Suvar
Hi,
I'm currently trying to create an AsymmetricKeyParameter public key
public static AsymmetricKeyParameter
xmlStringToPubKey(string
xmlString)
{
//AsymmetricCipherKeyPair keyPair;
if (String.IsNullOrEmpty(xmlString))
{
Console.WriteLine("Empty XML string received");
return null;
}
else
{
XDocument xdoc = XDocument.Parse(xmlString);
return
PublicKeyFactory.CreateKey(Streamify(xdoc.Descendants("Modulus").First().Value));
}
}
However, I'm getting an exception when calling CreateKey on
PublicKeyFactory obj. xdoc.Descendants returns a string which needs to
be converted into an array of bytes. I've tried different ways to
- Encoding.Unicode.getBytes(xmlString)
- getBytes(xmlString) method I've created to return a byte array from
a string
- Streamify - another method I've created to return a Stream
While I get different exceptions on each of the above methods none
seem to work. Any hints appreciated.
Loading...