public void OpenFile(string file) { SuspendLayout(); try { using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) { try { byte[] pemData = PemReader.ReadPem(fs); using (MemoryStream ms = new MemoryStream(pemData)) { rootNode = Asn1.Decode(ms); } } catch (ArgumentException) { fs.Position = 0; rootNode = Asn1.Decode(fs); } stsFile.Text = string.Concat("File: ", file); stsSize.Text = string.Concat("Size: ", fs.Length.ToString(), " bytes"); Text = string.Concat(TitleBase, " - ", file); } ShowAsn1(rootNode); UpdateHexViewerData(); } catch (Exception ex) { MessageBox.Show(this, "Error while opening file: " + ex.Message, "Error opening file", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } ResumeLayout(); }
public static RSAParameters DecodeRsaParameters(string pkcs8PrivateKey) { const string PrivateKeyPrefix = "-----BEGIN PRIVATE KEY-----"; const string PrivateKeySuffix = "-----END PRIVATE KEY-----"; Utilities.ThrowIfNullOrEmpty(pkcs8PrivateKey, nameof(pkcs8PrivateKey)); pkcs8PrivateKey = pkcs8PrivateKey.Trim(); if (!pkcs8PrivateKey.StartsWith(PrivateKeyPrefix) || !pkcs8PrivateKey.EndsWith(PrivateKeySuffix)) { throw new ArgumentException( $"PKCS8 data must be contained within '{PrivateKeyPrefix}' and '{PrivateKeySuffix}'.", nameof(pkcs8PrivateKey)); } string base64PrivateKey = pkcs8PrivateKey.Substring(PrivateKeyPrefix.Length, pkcs8PrivateKey.Length - PrivateKeyPrefix.Length - PrivateKeySuffix.Length); // FromBase64String() ignores whitespace, so further Trim()ing isn't required. byte[] pkcs8Bytes = Convert.FromBase64String(base64PrivateKey); object ans1 = Asn1.Decode(pkcs8Bytes); object[] parameters = (object[])((object[])ans1)[2]; var rsaParmeters = new RSAParameters { Modulus = TrimLeadingZeroes((byte[])parameters[1]), Exponent = TrimLeadingZeroes((byte[])parameters[2], alignTo8Bytes: false), D = TrimLeadingZeroes((byte[])parameters[3]), P = TrimLeadingZeroes((byte[])parameters[4]), Q = TrimLeadingZeroes((byte[])parameters[5]), DP = TrimLeadingZeroes((byte[])parameters[6]), DQ = TrimLeadingZeroes((byte[])parameters[7]), InverseQ = TrimLeadingZeroes((byte[])parameters[8]), }; return(rsaParmeters); }