Вот код
namespace TpcEncryptPkcs7
{
class Program
{
private static readonly string 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#.
Мне нужно добавить key_identifier, алогрифм oaepParams.
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();
Подробнее здесь: https://stackoverflow.com/questions/793 ... oaepwithsh
Данные шифрования в алгоритме шифрования формата PKCS#7: ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как/могу ли я создать файл PKCS#12, используя Python и модуль шифрования?
Anonymous » » в форуме Python - 0 Ответы
- 16 Просмотры
-
Последнее сообщение Anonymous
-