public void SignAndVerifyPlainWithTamperTest(string testCase, bool isSignatureValid)
        {
            #region Arange

            var plainData = GetPlainData();

            #endregion

            #region Act

            var signature = AsymmetricCryptographyRsa.Sign(AlicePrivatePublicXml, plainData);
            switch (testCase)
            {
            case "TamperSignature":
                signature[signature.Length / 2] ^= signature[signature.Length / 2];
                break;

            case "TamperPlainData":
                plainData[plainData.Length / 2] ^= plainData[plainData.Length / 2];
                break;
            }

            var verify = AsymmetricCryptographyRsa.Verify(AlicePublicXml, plainData, signature);

            #endregion

            #region Assert

            // the RSA signature size is dependent on the key size
            signature.Should().HaveCount(i => i == 512);
            signature.Should().NotContainNulls();
            verify.Should().Be(isSignatureValid);

            #endregion
        }
Esempio n. 2
0
        public void UseCase_Asymmetric()
        {
            string alicePlainText;
            string bobPlainText;

            outputHelper.WriteLine($"Alice is creating a RSA key pair");
            var rsaAlicePrivate = AsymmetricCryptographyRsa.CreateRsaKeyPair();
            var rsaAlicePublic  = AsymmetricCryptographyRsa.ExtractPublicKey(rsaAlicePrivate);

            outputHelper.WriteLine($"Bob is creating a RSA key pair");
            var rsaBobPrivate = AsymmetricCryptographyRsa.CreateRsaKeyPair();
            var rsaBobPublic  = AsymmetricCryptographyRsa.ExtractPublicKey(rsaBobPrivate);

            byte[] encryptedKey;
            byte[] signature;
            SymmetricEncryption.EncryptedDataContainer encryptedDataContainer;

            // Alice
            {
                alicePlainText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
                outputHelper.WriteLine($"Alice wants to encrypt: {alicePlainText}");

                var key = Sample.KeyGeneration.CreateRandom(256);
                outputHelper.WriteLine($"Alice is creating a random secret: {key.ToText()}");

                encryptedKey = AsymmetricCryptographyRsa.Encrypt(rsaBobPublic, key);
                outputHelper.WriteLine($"Alice encrypts the key with bobs public key: {encryptedKey.ToText()}");

                encryptedDataContainer = SymmetricEncryption.Encrypt(key, alicePlainText.ToData(), new byte[0]);
                outputHelper.WriteLine($"Alice encrypts the data with the secret key");

                signature = AsymmetricCryptographyRsa.Sign(rsaAlicePrivate, GetData(encryptedDataContainer));
                outputHelper.WriteLine($"Alice sign the cipher text");

                outputHelper.WriteLine($"Alice sends cipher text, signature and her public key to bob");
            }

            // Bob
            {
                var isValid = AsymmetricCryptographyRsa.Verify(rsaAlicePublic, GetData(encryptedDataContainer), signature);
                outputHelper.WriteLine($"Bobs verify the signature of the cipher text, it is valid: {isValid}");

                var decryptedKey = AsymmetricCryptographyRsa.Decrypt(rsaBobPrivate, encryptedKey);
                outputHelper.WriteLine($"Bobs decrypt the decrypted key from Alice with his private key: {decryptedKey.ToText()}");

                var decryptedData = SymmetricEncryption.Decrypt(decryptedKey, encryptedDataContainer);
                bobPlainText = decryptedData.ToUtf8String();
                outputHelper.WriteLine($"Bobs decrypt the data from Alice with his private key: {bobPlainText}");
            }

            alicePlainText.Should().BeEquivalentTo(bobPlainText, "The plain text must be the same.");
        }
        public AsymmetricCryptographyRsaTest()
        {
            AlicePrivatePublicXml = AsymmetricCryptographyRsa.CreateRsaKeyPair();
            AlicePublicXml        = AsymmetricCryptographyRsa.ExtractPublicKey(AlicePrivatePublicXml);

            BobPrivatePublicXml = AsymmetricCryptographyRsa.CreateRsaKeyPair();
            BobPublicXml        = AsymmetricCryptographyRsa.ExtractPublicKey(BobPrivatePublicXml);

            EvePrivatePublicXml = AsymmetricCryptographyRsa.CreateRsaKeyPair();
            EvePublicXml        = AsymmetricCryptographyRsa.ExtractPublicKey(EvePrivatePublicXml);

            PlainString = Guid.NewGuid().ToString();
        }
        public void SignAndVerifyPlainTest()
        {
            #region Arange

            var plainData = GetPlainData();

            #endregion

            #region Act

            var signature = AsymmetricCryptographyRsa.Sign(AlicePrivatePublicXml, plainData);
            var verify    = AsymmetricCryptographyRsa.Verify(AlicePublicXml, plainData, signature);

            #endregion

            #region Assert

            // the RSA signature size is dependent on the key size
            signature.Should().HaveCount(i => i == 512);
            signature.Should().NotContainNulls();
            verify.Should().Be(true);

            #endregion
        }