BouncyCastle tiene archivos DLL FIPS para C# que necesito usar para el cifrado en lugar de archivos DLL normales debido al cumplimiento. ¿Cómo importa claves públicas y privadas y las convierte en un RSACryptoServiceProvider para cifrar y descifrar?
Así es como encripto y descifro usando el BouncyCastle normal. Solo necesito cambiar las funciones ImportPrivateKey e ImportPublicKey
public static string Decrypt(string privateKey, string base64Encrypted) { string ret = null; using (var rsa = ImportPrivateKey(privateKey)) { var cipherBytes = Convert.FromBase64String(base64Encrypted); RSA rsaCng = new RSACng(); rsaCng.ImportParameters(rsa.ExportParameters(true)); byte[] plainBytes = rsaCng.Decrypt(cipherBytes, RSAEncryptionPadding.OaepSHA256); string plaintext = Encoding.UTF8.GetString(plainBytes); ret = plaintext; } return ret; } public static string Encrypt(string publicKey, string toEncrypt) { string cipherText = null; using (var rsa = ImportPublicKey(publicKey)) { var data = Encoding.UTF8.GetBytes(toEncrypt); RSA rsaCng = new RSACng(); rsaCng.ImportParameters(rsa.ExportParameters(false)); byte[] cipherTextBytes = rsaCng.Encrypt(data, RSAEncryptionPadding.OaepSHA256); cipherText = Convert.ToBase64String(cipherTextBytes); } return cipherText; } public RSACryptoServiceProvider ImportPrivateKey(string pem) { PemReader pr = new PemReader(new StringReader(pem)); AsymmetricCipherKeyPair KeyPair = (AsymmetricCipherKeyPair)pr.ReadObject(); RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)KeyPair.Private); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(rsaParams); return rsa; } public RSACryptoServiceProvider ImportPublicKey(string pem) { PemReader pr = new PemReader(new StringReader(pem)); AsymmetricKeyParameter publicKey = (AsymmetricKeyParameter)pr.ReadObject(); RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaKeyParameters)publicKey); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(rsaParams); return rsa; }Así es como lo hice funcionar. Si alguien más tiene algo más simple, ¡sería increíble!. Importe las claves, conviértalas en AsymmetricRsaPublicKey , luego conviértalas en RSAParameters y luego impórtelos.
using System.IO; using System.Security.Cryptography; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto.Asymmetric; using Org.BouncyCastle.Crypto.Fips; using Org.BouncyCastle.OpenSsl; public RSACryptoServiceProvider ImportPublicKey(string key) { var reader = new OpenSslPemReader(new StringReader(key)); var publicKeyInfo = (SubjectPublicKeyInfo)reader.ReadObject(); var rpckp = new AsymmetricRsaPublicKey(FipsRsa.Pkcs1v15.Algorithm, publicKeyInfo); RSAParameters parms = new RSAParameters { Modulus = rpckp.Modulus.ToByteArrayUnsigned(), Exponent = rpckp.PublicExponent.ToByteArrayUnsigned() }; RSACryptoServiceProvider rcsp = new RSACryptoServiceProvider(); rcsp.ImportParameters(parms); return rcsp; } public RSACryptoServiceProvider ImportPrivateKey(string key) { var reader = new OpenSslPemReader(new StringReader(key)); var keyPair = (PemKeyPair)reader.ReadObject(); var rpckp = new AsymmetricRsaPrivateKey(FipsRsa.Pkcs1v15.Algorithm, keyPair.PrivateKeyInfo); RSAParameters parms = new RSAParameters { Modulus = rpckp.Modulus.ToByteArrayUnsigned(), P = rpckp.P.ToByteArrayUnsigned(), Q = rpckp.Q.ToByteArrayUnsigned(), DP = rpckp.DP.ToByteArrayUnsigned(), DQ = rpckp.DQ.ToByteArrayUnsigned(), InverseQ = rpckp.QInv.ToByteArrayUnsigned(), D = rpckp.PrivateExponent.ToByteArrayUnsigned(), Exponent = rpckp.PublicExponent.ToByteArrayUnsigned() }; RSACryptoServiceProvider rcsp = new RSACryptoServiceProvider(); rcsp.ImportParameters(parms); return rcsp; }