public void TestFileEncryption()
        {
            // Load the file to encrypt
            byte[] imgData = File.ReadAllBytes(IMG_FILE_NAME);

            AESEncryptor aesEncryptor = new AESEncryptor(PASSWORD);

            RSACryptoServiceProvider rsaProviderOlivierCodepro = new RSACryptoServiceProvider();
            RSAOAEPEncryptor rsaDigestEncrypt = new RSAOAEPEncryptor(rsaProviderOlivierCodepro);

            RSACryptoServiceProvider rsaProviderOlivierRouit = new RSACryptoServiceProvider();
            RSASHA1Signature rsaDigestSigned = new RSASHA1Signature(rsaProviderOlivierRouit);

            // Encrypt the file data, the key and sign the original file data
            EncryptedFile encryptFile = new EncryptedFile(imgData,
                new FileDescription(IMG_FILE_NAME, MIME_JPG, APP_SLIDESHOW, ALGO_AES),
                aesEncryptor,
                new Recipient[] { new Recipient(USER_ID_DEST1, rsaDigestEncrypt) },
                new Owner(USER_ID_SRCE, rsaDigestSigned));

            // Build an EncryptedFile instance from the encrypted content with header
            EncryptedFile encryptFileOut = new EncryptedFile(encryptFile.EncryptedContent);

            EncryptedDataHeader encryptedHeader = encryptFileOut.EncryptedHeader;

            // Process the encrypted DigestData to extract the AES key
            IDigestEncryptor encryptDigest = RSADigestFactory.CreateDigestData(encryptedHeader.EncryptedKeys.Where(k => k.UserID == USER_ID_DEST1).First().Encrypted, rsaProviderOlivierCodepro) as IDigestEncryptor;
            byte[] decryptedKeyAndIV = encryptDigest.Decrypt();

            IEncryptProcess aesDecryptor = new AESEncryptor(decryptedKeyAndIV);
            byte[] decryptedFileData = aesDecryptor.DecryptData(encryptFileOut.EncryptedFileData);

            // Process the Signature DigestData
            IDigestSignature signDigest = RSADigestFactory.CreateDigestData(encryptedHeader.Signature, rsaProviderOlivierRouit) as IDigestSignature;
            bool verified = signDigest.Verify(decryptedFileData);
            Assert.IsTrue(verified);
        }
        public void TestEncryptDecryptKey()
        {
            AesCryptoServiceProvider aesServiceProvider = new AesCryptoServiceProvider();
            aesServiceProvider.KeySize = 256;
            aesServiceProvider.GenerateKey();
            aesServiceProvider.GenerateIV();

            byte[] key = aesServiceProvider.Key;
            byte[] iv = aesServiceProvider.IV;

            byte[] aesKeyAndIV = new byte[key.Length + iv.Length];
            Buffer.BlockCopy(key, 0, aesKeyAndIV, 0, key.Length);
            Buffer.BlockCopy(iv, 0, aesKeyAndIV, key.Length, iv.Length);

            RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
            DigestData rsaDigestEncrypt = new RSAOAEPEncryptor(rsaProvider);
            ((IDigestEncryptor)rsaDigestEncrypt).Encrypt(aesKeyAndIV);

            string jsonSerialized = JsonConvert.SerializeObject(rsaDigestEncrypt);

            DigestData rsaJsonDigest = (DigestData)JsonConvert.DeserializeObject(jsonSerialized, typeof(DigestData));

            IDigestEncryptor rsaDigestDecryp = RSADigestFactory.CreateDigestData(rsaJsonDigest, rsaProvider) as IDigestEncryptor;
            Assert.IsNotNull(rsaDigestDecryp);
            byte[] decryptedKeyAndIV = rsaDigestDecryp.Decrypt();
            bool equals = aesKeyAndIV.HasSameContent(decryptedKeyAndIV);
            Assert.IsTrue(equals);
        }
        public void TestEncrypteDatadHeader()
        {
            EncryptedDataHeader encryptedHeader = new EncryptedDataHeader(FILE_NAME, USER_ID_SRCE);

            encryptedHeader.Application = APP_NOTEPAD;
            encryptedHeader.EncryptionAlgorithm = ALGO_AES;
            encryptedHeader.MIME = MIME_TEXT;

            AesCryptoServiceProvider aesServiceProvider = new AesCryptoServiceProvider();
            aesServiceProvider.KeySize = 256;
            aesServiceProvider.GenerateKey();
            aesServiceProvider.GenerateIV();

            byte[] key = aesServiceProvider.Key;
            byte[] iv = aesServiceProvider.IV;

            byte[] aesKeyAndIV = new byte[key.Length + iv.Length];
            Buffer.BlockCopy(key, 0, aesKeyAndIV, 0, key.Length);
            Buffer.BlockCopy(iv, 0, aesKeyAndIV, key.Length, iv.Length);

            // Encrypt the AES key with the public key of the OlivierCodepro certificate
            RSACryptoServiceProvider rsaProviderOlivierCodepro = new RSACryptoServiceProvider();
            DigestData rsaDigestEncrypt = new RSAOAEPEncryptor(rsaProviderOlivierCodepro);
            ((IDigestEncryptor)rsaDigestEncrypt).Encrypt(aesKeyAndIV);

            EncryptedKey encryptedAesKeyForOlivierCodepro = new EncryptedKey(USER_ID_DEST1, rsaDigestEncrypt);
            encryptedHeader.EncryptedKeys = new EncryptedKey[] { encryptedAesKeyForOlivierCodepro };

            // Sign the test BEFORE it is encrypted using OlivierRouit private key
            byte[] byteText = ASCIIEncoding.ASCII.GetBytes(TEXT_TO_SIGN);

            RSACryptoServiceProvider rsaProviderOlivierRouit = new RSACryptoServiceProvider();
            DigestData rsaDigestSigned = new RSASHA1Signature(rsaProviderOlivierRouit);

            ((IDigestSignature)rsaDigestSigned).Sign(byteText);

            encryptedHeader.Signature = rsaDigestSigned;

            string jsonSerialized = JsonConvert.SerializeObject(encryptedHeader);

            EncryptedDataHeader encryptedHeaderDeserialized = JsonConvert.DeserializeObject<EncryptedDataHeader>(jsonSerialized);

            // Process the Signature DigestData
            IDigestSignature signDigest = RSADigestFactory.CreateDigestData(encryptedHeaderDeserialized.Signature, rsaProviderOlivierRouit) as IDigestSignature;
            bool verified = signDigest.Verify(byteText);
            Assert.IsTrue(verified);

            // Process the encrypted DigestData
            IDigestEncryptor encryptDigest = RSADigestFactory.CreateDigestData(encryptedHeaderDeserialized.EncryptedKeys.Where(k => k.UserID == USER_ID_DEST1).First().Encrypted, rsaProviderOlivierCodepro) as IDigestEncryptor;
            byte[] decryptedKeyAndIV = encryptDigest.Decrypt();
            bool equals = aesKeyAndIV.HasSameContent(decryptedKeyAndIV);
            Assert.IsTrue(equals);
        }