Esempio n. 1
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();
        }