public static byte[] KrypterData(byte[] ukryptertBytes, X509Certificate sertifikat)
        {
/*            AlgorithmIdentifier hash = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256, DerNull.Instance);
*           AlgorithmIdentifier mask = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, hash);
*           AlgorithmIdentifier pSource = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdPSpecified, new DerOctetString(new byte[0]));
*           RsaesOaepParameters parameters = new RsaesOaepParameters(hash, mask, pSource);
*           AlgorithmIdentifier keyEncryptionScheme = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdRsaesOaep, parameters);*/

            CmsEnvelopedDataGenerator envelopedDataGenerator = new CmsEnvelopedDataGenerator();

            envelopedDataGenerator.AddKeyTransRecipient(sertifikat);
            CmsEnvelopedData cmsData = envelopedDataGenerator.Generate(new CmsProcessableByteArray(ukryptertBytes), CmsEnvelopedGenerator.Aes256Cbc);

            return(cmsData.GetEncoded());
        }
示例#2
0
        public string Encrypt(string plainText)
        {
            byte[] plainBytes            = Encoding.UTF8.GetBytes(plainText);
            CmsProcessableByteArray cpba = new CmsProcessableByteArray(plainBytes);

            CmsEnvelopedDataGenerator envelopedGen = new CmsEnvelopedDataGenerator();

            foreach (X509Certificate2 cert in publicCerts)
            {
                Org.BouncyCastle.X509.X509Certificate bouncyCert = DotNetUtilities.FromX509Certificate(cert);
                AsymmetricKeyParameter keyParameter = bouncyCert.GetPublicKey();
                envelopedGen.AddKeyTransRecipient(bouncyCert);
            }

            CmsEnvelopedData envelopedData = envelopedGen.Generate(cpba, CmsEnvelopedGenerator.Aes256Cbc);
            string           cipherString  = Convert.ToBase64String(envelopedData.GetEncoded());

            return(cipherString);
        }