public static byte[] DecodeOpenSSLPrivateKey(string instr) {
const string pemprefix = "-----BEGIN RSA PRIVATE KEY-----" ;
const string pemsuffix = "-----END RSA PRIVATE KEY-----" ;
string pemstr = instr.Trim() ;
if(!pemstr.StartsWith(pemprefix) || !pemstr.EndsWith(pemsuffix)) {
throw new ArgumentException("missing BEGIN or END sequence of RSA private Key");
}
string pvkstr =
pemstr.Replace(pemprivheader, "").Replace(pemprivfooter, "").Trim(); //remove
byte[] binkey;
try { // there aren't any PEM encryption infos => UNencrypted PEM private key
binkey = Convert.FromBase64String(pvkstr) ;
return binkey;
}
catch(System.FormatException) {
//Console.WriteLine("We have a full encrypted OpenSSL PEM private key");
}
string encryptedstr = string.Empty;
// read crypted chiffer lets extract salt
byte[] salt = UserSalzDerAndroschErhalts(pvkstr, out encryptedstr);
if (salt == null || string.IsNullOrEmpty(encryptedstr)) {
throw new ArgumentException("invalid RSA private Key");
}
try { // it's an encrypted RSA key?
binkey = Convert.FromBase64String(encryptedstr);
}
catch(System.FormatException) {
throw; // rethrow the Exception
}
// 3DES SSL key fetch
SecureString passwd3DES = GetSecPswd("Enter password for 3DESkey==>") ;
// Console.Write("\nEnter password for key: ");
// string passwd = Console.ReadLine();
byte[] tripledes = GetOpenSSL3deskey(salt, passwd3DES, 1, 2);
if (tripledes == null) {
throw new ArgumentException("error decrypting 3DES key\n" + salt.ToString());
}
// Decrypt the encrypted 3DES key by using salt from PEM header
// same method how anonymous hacked IV
byte[] rsakey = DecryptKey(binkey, tripledes, salt);
if (rsakey == null) {
throw new ArgumentException("error decrypting 3DES key\n" + salt.ToString());
}
return rsakey; // return decrypted RSA private key
}
Keine Kommentare:
Kommentar veröffentlichen