{
класс Program
{
частная статическая строка только для чтения DATA_TO_ENCRYPT = "{"fpan":"5342270061823097","issuerCardRefId":"327","exp":"1229","cardholderName":"RABBANI"}";
Код: Выделить всё
static void Main(string[] args)
{
// Load the provided PEM certificate for encryption
var certificate = LoadCertificateFromPem(@"
-----BEGIN CERTIFICATE-----
MIIEKTCCAxGgAwIBAgIMa3BA3M18ovOLDPVJMA0GCSqGSIb3DQEBCwUAMF0xCzAJ
... (rest of your certificate) ...
-----END CERTIFICATE-----";
// Encrypt using the specified certificate
byte[] encData = EncryptPKCS7(Encoding.UTF8.GetBytes(DATA_TO_ENCRYPT),
certificate);
Console.WriteLine("Encrypted data = " + Hex.ToHexString(encData));
}
private static X509Certificate2 LoadCertificateFromPem(string pem)
{
// Remove the header and footer and convert the PEM string to a byte array
var base64 = pem.Replace("-----BEGIN CERTIFICATE-----", "")
.Replace("-----END CERTIFICATE-----", "")
.Replace("\n", "")
.Replace("\r", "");
var certBytes = Convert.FromBase64String(base64);
return new X509Certificate2(certBytes);
}
private static byte[] EncryptPKCS7(byte[] plainData, X509Certificate2 certificate)
{
var generator = new CmsEnvelopedDataGenerator();
// Extract the public key from the certificate
var pubKey = certificate.GetRSAPublicKey();
// Use OAEP with SHA-256 and MGF1
IAsymmetricBlockCipher oaepParams = new OaepEncoding(
new RsaEngine(),
new Sha256Digest(), // Hash algorithm for OAEP and MGF1
new Sha256Digest(), // Mask generation function (MGF1) with SHA256
null // Optional encoding parameter (PSource)
);
var recipientInfoGenerator = new JceKeyTransRecipientInfoGenerator(KEY_IDENTIFIER, oaepParams, pubKey);
generator.AddRecipientInfoGenerator(recipientInfoGenerator);
var data = new CmsProcessableByteArray(plainData);
var encryptor = new BcCMSContentEncryptorBuilder(CmsAlgorithm.Aes256Cbc);
var enveloped = generator.Generate(data, encryptor.Build());
return enveloped.GetEncoded();
}
}
У меня есть вопросы, как я могу добавить параметры ниже в c#
Подробнее здесь: https://stackoverflow.com/questions/793 ... oaepwithsh