public EncryptedData()
 {
     this.m_cipherData = new CipherData();
     this.m_encryptionMethod = new EncryptionMethodElement();
 }
        internal static void Encrypt(Stream toEncrypt, RSA key, out KeyInfo keyInfo, out EncryptionMethod encryptionMethod, out CipherData cipherData)
        {
            using (Aes sessionKey = Aes.Create())
            {
                sessionKey.KeySize = 128;
                encryptionMethod   = new EncryptionMethod(EncryptedXml.XmlEncAES128Url);
                keyInfo            = new KeyInfo();

                EncryptedKey encKey;
                keyInfo.AddClause(
                    new KeyInfoEncryptedKey(
                        encKey = new EncryptedKey()
                {
                    CipherData       = new CipherData(EncryptedXml.EncryptKey(sessionKey.Key, key, useOAEP: true)),
                    EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSAOAEPUrl)
                }));

                encKey.KeyInfo.AddClause(new RSAKeyValue(key));

                byte[] dataToEncrypt = new byte[toEncrypt.Length];
                toEncrypt.Read(dataToEncrypt, 0, (int)toEncrypt.Length);

                var encryptedXml = new EncryptedXml();
                encryptedXml.Padding = PaddingMode.PKCS7;
                encryptedXml.Mode    = CipherMode.CBC;
                byte[] encryptedData = encryptedXml.EncryptData(dataToEncrypt, sessionKey);
                cipherData = new CipherData(encryptedData);
            }
        }
        public void CipherReference_Null()
        {
            CipherData cipherData = new CipherData();

            Assert.Throws <ArgumentNullException>(() => cipherData.CipherReference = null);
        }
        public void LoadXml_Null()
        {
            CipherData cipherData = new CipherData();

            Assert.Throws <ArgumentNullException>(() => cipherData.LoadXml(null));
        }
        public void CipherValue_CipherReferenceSet()
        {
            CipherData cipherData = new CipherData(new CipherReference());

            Assert.Throws <System.Security.Cryptography.CryptographicException>(() => cipherData.CipherValue = new byte[0]);
        }
Ejemplo n.º 6
0
        public static void Encrypt(Stream toEncrypt, RsaKeyParameters key, out KeyInfo keyInfo, out EncryptionMethod encryptionMethod, out CipherData cipherData)
        {
            var random  = new SecureRandom();
            var keyData = new byte[128 / 8];
            var ivData  = new byte[128 / 8];

            random.NextBytes(ivData);
            random.NextBytes(keyData);
            var sessionKey = new ParametersWithIV(new KeyParameter(keyData), ivData);

            encryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES128Url);
            keyInfo          = new KeyInfo();

            EncryptedKey encKey;

            keyInfo.AddClause(
                new KeyInfoEncryptedKey(
                    encKey = new EncryptedKey()
            {
                CipherData       = new CipherData(EncryptedXml.EncryptKey(keyData, key, useOAEP: true)),
                EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSAOAEPUrl)
            }));

            encKey.KeyInfo.AddClause(new RSAKeyValue(key));

            byte[] dataToEncrypt = new byte[toEncrypt.Length];
            toEncrypt.Read(dataToEncrypt, 0, (int)toEncrypt.Length);

            var encryptedXml = new EncryptedXml();

            encryptedXml.Padding = "PKCS7";
            encryptedXml.Mode    = "CBC";
            byte[] encryptedData = encryptedXml.EncryptData(dataToEncrypt, sessionKey);
            cipherData = new CipherData(encryptedData);
        }