public void EncryptDecryptBlob_WithPrivateKey()
    {
        using (var options = new SymmetricEncryptionProviderOptions())
            using (var engine = new SymmetricEncryptionProvider(options))
            {
                byte[] privateKey = null;
                using (var rng = new RandomNumberGenerator())
                {
                    privateKey = rng.NextBytes(20);
                }

                ReadOnlySpan <byte> text = Utf8.NoBom.GetBytes("My name Jeff");

                var encryptedBlob = engine.Encrypt(text, privateKey);
                assert.True(!encryptedBlob.IsEmpty);
                assert.True(encryptedBlob != text);

                var data = new byte[encryptedBlob.Length];
                encryptedBlob.CopyTo(data);

                var text2 = engine.Decrypt(encryptedBlob, privateKey);
                assert.True(!text2.IsEmpty);
                assert.Equal(text.Length, text2.Length);
                for (var i = 0; i < text2.Length; i++)
                {
                    assert.Equal(text[i], text2[i]);
                }
            }
    }
Exemple #2
0
        public void DecryptStringTest()
        {
            EncryptionInfo info = new EncryptionInfo();

            info.PassPhrase    = "Pas5pr@se";              // can be any string
            info.SaltValue     = "s@1tValue";              // can be any string
            info.HashAlgorithm = "SHA1";                   // can be "MD5"
            info.Iterations    = 2;                        // can be any number
            info.InitVector    = "@1B2c3D4e5F6g7H8";       // must be 16 bytes
            info.KeySize       = 256;                      // can be 192 or 128

            SymmetricEncryptionProvider provider = new SymmetricEncryptionProvider();

            string plainText2 = provider.Decrypt(cipherText, info);

            Assert.IsNotNull(plainText2);
            Assert.AreEqual(plainText2, plainText);
        }