public static string RSAEncrypt(string pemStreamText, KeyParameter secretKeyParameter) { string result = string.Empty; try { RsaKeyParameters rsaKeyParameters = null; StreamReader reader = new StreamReader(new MemoryStream(Convert.FromBase64String(pemStreamText))); Org.BouncyCastle.OpenSsl.PemReader pemReader = new Org.BouncyCastle.OpenSsl.PemReader(reader); PemObject pemObject = pemReader.ReadPemObject(); if (pemObject != null) { AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(pemObject.Content); rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter; } else { rsaKeyParameters = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(pemStreamText)); } byte[] key = secretKeyParameter.GetKey(); IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/OAEPWithSHA_1AndMGF1Padding"); cipher.Init(forEncryption: true, rsaKeyParameters); byte[] inArray = BlockCipher(key, cipher, isEncrypt: true); result = Convert.ToBase64String(inArray, Base64FormattingOptions.None); } catch (Exception ex) { Debug.LogError("### SwrveManagerUtils::RSAEncrypt: " + ex.Message); } return(result); }
/// <summary> /// Create a certificate for the given onboarding response from the AR. /// </summary> /// <param name="onboardResponse">-</param> /// <returns>A X509 certificate to use for the communication between endpoint and AR.</returns> /// <exception cref="CouldNotCreateCertificateForTypeException">-</exception> public static X509Certificate GetCertificate(OnboardResponse onboardResponse) { switch (onboardResponse.Authentication.Type) { case "P12": return(new X509Certificate2( Convert.FromBase64String(onboardResponse.Authentication.Certificate), onboardResponse.Authentication.Secret)); case "PEM": { var pemReader = new PemReader( new StringReader(onboardResponse.Authentication.Certificate), new PasswordFinder(onboardResponse.Authentication.Secret)); RSA privateKey; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { privateKey = ToRSA((RsaPrivateCrtKeyParameters)pemReader.ReadObject()); } else { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { privateKey = DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)pemReader.ReadObject()); } else { throw new CouldNotCreateCertificateForOsException( $"Could not create a certificate for '${RuntimeInformation.OSDescription}'"); } } var certificate = pemReader.ReadPemObject(); if (certificate.Type == "CERTIFICATE") { return(new X509Certificate2(certificate.Content).CopyWithPrivateKey(privateKey)); } break; } } throw new CouldNotCreateCertificateForTypeException( $"Could not create a certificate for the type '${onboardResponse.Authentication.Type}'"); }