*
* @return a PKCS8 representation of the key.
*/
public byte[] getEncoded()
{
PrivateKeyInfo info;
byte[] keyEnc = this.getX().toByteArray();
byte[] keyBytes;
if (keyEnc[0] == 0)
{
keyBytes = new byte[keyEnc.length - 1];
}
else
{
keyBytes = new byte[keyEnc.length];
}
for (int i = 0; i != keyBytes.length; i++)
{
keyBytes[i] = keyEnc[keyEnc.length - 1 - i]; // must be little endian
}
try
{
if (gost3410Spec instanceof GOST3410ParameterSpec)
{
info = new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()))), new DEROctetString(keyBytes));
}
else
{
info = new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94), new DEROctetString(keyBytes));
}
return info.getEncoded(ASN1Encoding.DER);
}
catch (IOException e)
{
return null;
}