示例#1
0
        public void DecryptDetectInvalidCipherText()
        {
            var key1 = new PrivateKey(
                new byte[]
            {
                0xfb, 0xc2, 0x00, 0x42, 0xb3, 0xa7, 0x07, 0xa7, 0xd5, 0xa1,
                0xfa, 0x57, 0x71, 0x71, 0xf4, 0x9c, 0xd3, 0xa9, 0xe6, 0x7a,
                0xb9, 0x29, 0x57, 0x57, 0xc7, 0x14, 0xe3, 0xf2, 0xf8, 0xc2,
                0xd5, 0x73,
            }
                );
            var key2 = new PrivateKey(
                new byte[]
            {
                0xfb, 0xc2, 0x00, 0x42, 0xb3, 0xa7, 0x07, 0xa7, 0xd5, 0xa1,
                0xfa, 0x57, 0x71, 0x71, 0xf4, 0x9c, 0xd3, 0xa9, 0xe6, 0x7a,
                0xb9, 0x29, 0x57, 0x57, 0xc7, 0x14, 0xe3, 0xf2, 0xf8, 0xc2,
                0xd5, 0x37,
            }
                );
            var message    = Encoding.ASCII.GetBytes("test message");
            var cipherText = key1.PublicKey.Encrypt(message);

            Assert.Throws <InvalidCiphertextException>(() => key2.Decrypt(cipherText));
            Assert.Throws <InvalidCiphertextException>(
                () => key2.Decrypt(cipherText.ToImmutableArray())
                );
        }
示例#2
0
        public void Decrypt()
        {
            var prvKey = new PrivateKey(
                new byte[]
            {
                0xfb, 0xc2, 0x00, 0x42, 0xb3, 0xa7, 0x07, 0xa7, 0xd5, 0xa1,
                0xfa, 0x57, 0x71, 0x71, 0xf4, 0x9c, 0xd3, 0xa9, 0xe6, 0x7a,
                0xb9, 0x29, 0x57, 0x57, 0xc7, 0x14, 0xe3, 0xf2, 0xf8, 0xc2,
                0xd5, 0x73,
            }
                );
            var cipherText = new byte[]
            {
                0x03, 0xe3, 0x1a, 0x0d, 0xea, 0x31, 0xe2, 0xb1, 0x32, 0x7b,
                0xd8, 0x70, 0x0a, 0xd3, 0x57, 0xcc, 0x69, 0x31, 0x4e, 0xca,
                0xd7, 0x0a, 0xe2, 0xe4, 0xfa, 0x55, 0x17, 0xa3, 0x3b, 0x67,
                0xcf, 0xb1, 0xc4, 0xfa, 0xa1, 0x10, 0xd4, 0xd2, 0x73, 0x11,
                0xef, 0xf1, 0x47, 0x99, 0xd7, 0x3d, 0x3c, 0xaa, 0xa2, 0x0e,
                0x35, 0x7c, 0x41, 0xc8, 0x8e, 0x14, 0x22, 0xc7, 0x64, 0xed,
                0xcc, 0xe0, 0x6c, 0x06, 0xb5, 0x86, 0x44, 0xc1, 0x68, 0xa5,
                0xab, 0xf3, 0x9d, 0xcb, 0x46, 0xb6, 0xe2,
            };
            var expected = Encoding.ASCII.GetBytes("test message");

            Assert.Equal(expected, prvKey.Decrypt(cipherText));
            Assert.Equal(expected, prvKey.Decrypt(cipherText.ToImmutableArray()));
        }
示例#3
0
        public void EncryptTest()
        {
            var prvKey = new PrivateKey();
            var pubKey = prvKey.PublicKey;
            var bs     = Encoding.ASCII.GetBytes("hello world");

            var encrypted = pubKey.Encrypt(bs);

            TestUtils.AssertBytesEqual(bs, prvKey.Decrypt(encrypted));

            ImmutableArray <byte> immutable = bs.ToImmutableArray();
            var encryptedImmutable          = pubKey.Encrypt(immutable);

            TestUtils.AssertBytesEqual(immutable, prvKey.Decrypt(encryptedImmutable));
        }
示例#4
0
        public void EncryptTest()
        {
            var prvKey    = new PrivateKey();
            var pubKey    = prvKey.PublicKey;
            var bs        = Encoding.ASCII.GetBytes("hello world");
            var encrypted = pubKey.Encrypt(bs);

            Assert.Equal(bs, prvKey.Decrypt(encrypted));
        }
示例#5
0
        public void DecryptTest()
        {
            var prvKey = new PrivateKey(ByteUtil.ParseHex(
                                            "fbc20042b3a707a7d5a1fa577171f49cd3a9e67ab9295757c714e3f2f8c2d573"));
            var cipherText = ByteUtil.ParseHex(
                "03e31a0dea31e2b1327bd8700ad357cc69314ecad70ae2e4fa5517a33b67cfb1c4faa110d4d27311eff14799d73d3caaa20e357c41c88e1422c764edcce06c06b58644c168a5abf39dcb46b6e2");
            var expected = Encoding.ASCII.GetBytes("test message");

            Assert.Equal(prvKey.Decrypt(cipherText), expected);
        }
示例#6
0
        public void EncryptDecrypt_LongTest()
        {
            using SharpRandom rng       = new SharpRandom();
            using PrivateKey privateKey = new PrivateKey(rng);
            PublicKey pubkey = privateKey.ToPublicKey();

            string message = $"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ac lectus metus. Donec ac mollis ex, sed feugiat leo. In aliquet erat et maximus vestibulum. Mauris non purus vitae nibh rhoncus scelerisque id at mauris. Maecenas id condimentum massa, quis fermentum quam. Suspendisse maximus nunc eget dapibus pulvinar. Nam at metus volutpat, molestie velit sit amet, pulvinar libero. Suspendisse eu pellentesque enim. Nullam porta molestie leo at sagittis. Pellentesque lacinia in ipsum et accumsan. Quisque cursus a nisi vel bibendum. Vivamus consectetur leo non eros convallis elementum.\r\nPhasellus diam massa, commodo pulvinar mauris sit amet, pellentesque gravida metus. Nullam dapibus sed tortor sed lobortis. Duis sodales, urna vel tincidunt sagittis, metus nisi sodales massa, at gravida eros ipsum sed tortor. Morbi rutrum augue augue, vel ornare felis faucibus at. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ut gravida libero. Cras consectetur, est ut faucibus euismod, elit massa porta ipsum, in faucibus enim odio non felis. Fusce sed mi eu mi consectetur dignissim ut quis odio. Fusce non sapien mauris. Curabitur viverra tempor auctor. Nullam lobortis eleifend tincidunt. Ut arcu ante, porttitor vitae iaculis ut, condimentum eget purus. Phasellus pretium, dui eget dictum aliquam, lectus turpis gravida erat, in pretium libero neque vel dui.\r\nDonec semper, eros vulputate fringilla pellentesque, nibh ligula vestibulum risus, eu pretium nunc erat vitae purus. Nam maximus lacus vel erat mollis suscipit. Duis eget placerat odio, ac efficitur risus. Phasellus porta blandit bibendum. Aliquam ut odio sed nibh egestas sagittis. Nunc sodales urna velit, ac bibendum lacus pharetra ut. Sed ac sem a odio consequat pretium sed a dui. In ut dapibus lacus. Nam pulvinar nunc et nisl hendrerit mattis. Donec faucibus feugiat lacinia. Fusce congue, mauris vehicula ornare suscipit, dolor leo interdum libero, eu euismod sapien erat dignissim lorem.";
            string enc     = pubkey.Encrypt(message);
            string dec     = privateKey.Decrypt(enc);

            Assert.Equal(dec, message);
        }
示例#7
0
        public void EncryptDecryptTest()
        {
            using SharpRandom rng       = new SharpRandom();
            using PrivateKey privateKey = new PrivateKey(rng);
            PublicKey pubkey = privateKey.ToPublicKey();

            for (int i = 0; i < 100; i++)
            {
                string message = new string(Enumerable.Repeat('a', i).ToArray());
                string enc     = pubkey.Encrypt(message);
                string dec     = privateKey.Decrypt(enc);

                Assert.Equal(dec, message);
            }
        }
示例#8
0
        /// <summary>
        /// Decrypts the symmetric cipher's initialization vector.
        /// </summary>
        /// <param name="encryptedIV">The encrypted initialization vector.</param>
        /// <exception cref="System.InvalidOperationException">The certificate did not contain a private key.</exception>
        /// <remarks>The method is called by the GoF template-methods.</remarks>
        /// <exception cref="InvalidOperationException"></exception>
        /// See also <seealso cref="CloneLightCipher"/>.
        protected override void DecryptIV(
            byte[] encryptedIV)
        {
            if (!IsSymmetricKeyInitialized)
            {
                throw new InvalidOperationException(Resources.UninitializedSymmetricKey);
            }
            if (ShouldEncryptIV && PrivateKey == null)
            {
                throw new InvalidOperationException(PublicKey == null
                                                        ? "The method is not available on light clones."
                                                        : "The certificate does not contain a private key.");
            }

            Symmetric.IV = ShouldEncryptIV
                                ? PrivateKey.Decrypt(encryptedIV, true)
                                : encryptedIV;
        }
示例#9
0
        private void HandleCommonSubset(ProtocolResult <CommonSubsetId, ISet <EncryptedShare> > result)
        {
            Logger.LogTrace($"Common subset finished {result.From}");
            foreach (var share in result.Result)
            {
                var dec = _privateKey.Decrypt(share);
                _taken[share.Id]          = true;
                _receivedShares[share.Id] = share;
                // todo think about async access to protocol method. This may pose threat to protocol internal invariants
                CheckDecryptedShares(share.Id);
                Broadcaster.Broadcast(CreateDecryptedMessage(dec));
            }

            _takenSet = true;

            foreach (var share in result.Result)
            {
                CheckDecryptedShares(share.Id);
            }

            CheckResult();
        }
示例#10
0
        protected virtual void LoadHashToValidate(
            Stream encryptedStream)
        {
            if (encryptedStream == null)
            {
                throw new ArgumentNullException(nameof(encryptedStream));
            }
            if (!encryptedStream.CanRead)
            {
                throw new ArgumentException(Resources.StreamNotReadable, nameof(encryptedStream));
            }
            if (!ShouldEncryptHash && PrivateKey == null)
            {
                throw new InvalidOperationException("The certificate does not contain a private key for decryption.");
            }

            //read the encrypted length and hash
            var lengthBuffer = new byte[sizeof(int)];
            var length       = 0;

            if (encryptedStream.Read(lengthBuffer, 0, sizeof(int)) != sizeof(int))
            {
                throw new ArgumentException(Resources.InvalidInputData + "length of the hash.", nameof(encryptedStream));
            }
            length = BitConverter.ToInt32(lengthBuffer, 0);

            _hash = new byte[length];

            if (encryptedStream.Read(_hash, 0, _hash.Length) != _hash.Length)
            {
                throw new ArgumentException(Resources.InvalidInputData + "hash.", nameof(encryptedStream));
            }

            // decrypt
            if (ShouldEncryptHash)
            {
                _hash = PrivateKey.Decrypt(_hash, true);
            }
        }
示例#11
0
        protected override void DecryptSymmetricKey(
            byte[] encryptedKey)
        {
            if (encryptedKey == null)
            {
                throw new ArgumentNullException(nameof(encryptedKey));
            }
            if (encryptedKey.Length == 0)
            {
                throw new ArgumentException(Resources.InvalidArgument, nameof(encryptedKey));
            }
            if (PublicKey == null)
            {
                throw new InvalidOperationException("The method is not available on light clones.");
            }
            if (PrivateKey == null)
            {
                throw new InvalidOperationException("The certificate does not contain a private key.");
            }

            Symmetric.Key             = PrivateKey.Decrypt(encryptedKey, true);
            IsSymmetricKeyInitialized = true;
        }
示例#12
0
        /// <summary>
        /// 解密
        /// <para>RSA</para>
        /// <para>填充方案: RSAES-OAEP(Optimal Asymmetric Encryption Padding)</para>
        /// </summary>
        /// <param name="data">密文</param>
        /// <returns></returns>
        public string DecryptWithRSA(string data)
        {
            var bytes = PrivateKey.Decrypt(Encoding.UTF8.GetBytes(data), true);

            return(Convert.ToBase64String(bytes));
        }