The key blob appears to contain both public and private key parameters, which is good. The only problem is that we don't know which is which - we don't know anything about their order in the key blob. These are the parameters that are most likely present:
// modulus INTEGER, -- n
// publicExponent INTEGER, -- e
// privateExponent INTEGER, -- d
// prime1 INTEGER, -- p
// prime2 INTEGER, -- q
// exponent1 INTEGER, -- d mod (p-1)
// exponent2 INTEGER, -- d mod (q-1)
// coefficient INTEGER, -- (inverse of q) mod p
The following code tries reading the required RSA parameters from the key blob, constructs an SshPrivateKey
object based on them, and checks whether it's usable. Please give it a try and let me know whether it works:
using System.IO;
using System.Text;
using System.Security.Cryptography;
using Rebex.Security.Cryptography;
using Rebex.Net;
....
// read RSA parameters from the key blob
var rp = new RSAParameters();
using (BinaryReader reader = new BinaryReader(new MemoryStream(keyblob)))
{
byte[] type = reader.ReadBytes((int)(reader.ReadUInt32() >> 24));
rp.Modulus = reader.ReadBytes((int)(reader.ReadUInt32() >> 24));
rp.Exponent = reader.ReadBytes((int)(reader.ReadUInt32() >> 24));
rp.D = reader.ReadBytes((int)(reader.ReadUInt32() >> 24));
// if it doesn't work, try uncomenting this line:
//reader.ReadBytes((int)(reader.ReadUInt32() >> 24));
// if it still doesn't work, try uncomenting this line as well:
//reader.ReadBytes((int)(reader.ReadUInt32() >> 24));
// and one more:
//reader.ReadBytes((int)(reader.ReadUInt32() >> 24));
rp.P = reader.ReadBytes((int)(reader.ReadUInt32() >> 24));
rp.Q = reader.ReadBytes((int)(reader.ReadUInt32() >> 24));
}
// use Rebex RSAManaged class to calculate additional helper parameters
var rsa = new RSAManaged();
rsa.ImportParameters(rp);
rp = rsa.ExportParameters(true);
// create SshPrivateKey using the RSA parameters
var key = SshPrivateKey.CreateFrom(rp);
// create a signature and verify it
byte[] hash = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes("test data"));
byte[] signature = key.CreateSignature(hash, SignatureHashAlgorithm.SHA1);
bool valid = key.VerifySignature(hash, signature, SignatureHashAlgorithm.SHA1);
// if it is not valid, something is wrong and the key is not usable
if (!valid)
throw new Exception("Invalid key.");
If it doesn't work, try uncomening one of the three commented lines and give it a try again. Repeat until all three lines are uncommented. If it still doesn't work, let me know.