Come convertire file di chiave PKCS8 in formato DER in #C?
Domanda
Come convertire file di chiave PKCS8 in formato DER, per ottenere chiave privata e pubblica in formato XML?
con openssl posso farlo utilizzando: OpenSSL PKCS8 -inform DER -in aaa010101aaa_FIEL.key -out aaa.txt
aaa010101aaa_FIEL.key è il file di Basi binaria
sto cercando di farlo con la libreria Bouncy Castle, ma ho problemi per la creazione di nuove EncryptedPrivateKeyInfo (...
c'è un post di farlo in java, ma ne ho bisogno in C # Come leggere una chiave password criptata con Java?
Soluzione 2
Ho trovato la risposta!
byte[] dataKey = File.ReadAllBytes(fileName);
Org.BouncyCastle.Crypto.AsymmetricKeyParameter asp =
Org.BouncyCastle.Security.PrivateKeyFactory.DecryptKey(pass.ToCharArray(), dataKey);
MemoryStream ms = new MemoryStream();
TextWriter writer = new StreamWriter(ms);
System.IO.StringWriter stWrite = new System.IO.StringWriter();
Org.BouncyCastle.OpenSsl.PemWriter pmw = new PemWriter(stWrite);
pmw.WriteObject(asp);
stWrite.Close();
return stWrite.ToString();
Altri suggerimenti
documentazione BouncyCastle è piuttosto scarsa e non ho fatto questa cosa esatta, ma si desidera utilizzare un Org.BouncyCastle.OpenSsl.PemReader di leggere il file in una (probabilmente) Org.BouncyCastle.Asn1.Pkcs.EncryptedPrivateKeyInfo , in questo modo:
using (FileStream FS = File.Open("whatever.key"))
{
using (TextReader TR = new StreamReader(FS))
{
PR = new Org.BouncyCastle.OpenSsl.PemReader(TR);
EPKI = (Org.BouncyCastle.Asn1.Pkcs.EncryptedPrivateKeyInfo)PR.ReadObject();
}
}
e poi EPKI.GetDerEncoded () si darebbe la cosa DER formattato. Non ci sono garanzie che questo funzionerà, ma PemReader dovrebbero almeno si mette sulla strada giusta.