Beispiel #1
0
        public void Decrypt_Should_BeEqualToTestData()
        {
            var testData   = compatibilityData["encrypt_single_recipient"];
            var bytes      = Bytes.FromString(testData["private_key"], StringEncoding.BASE64);
            var privateKey = cryptoSHA256.ImportPrivateKey(bytes);
            var publicKey  = cryptoSHA256.ExtractPublicKey(privateKey);
            var data       = Bytes.FromString(testData["original_data"], StringEncoding.BASE64);
            var cipherData = Bytes.FromString(testData["cipher_data"], StringEncoding.BASE64);

            Assert.AreEqual(cryptoSHA256.Decrypt(cipherData, privateKey), (byte[])data);
        }
Beispiel #2
0
        public void EncryptData_MultiplePublicKeysWithDifferentTypesGiven_ShouldCipherDataBeDecrypted()
        {
            var crypto   = new VirgilCrypto();
            var keyPairs = new List <KeyPair>
            {
                crypto.GenerateKeys(KeyPairType.EC_SECP256R1),
                crypto.GenerateKeys(KeyPairType.EC_SECP384R1),
                crypto.GenerateKeys(KeyPairType.EC_SECP521R1),
                crypto.GenerateKeys(KeyPairType.EC_BP256R1),
                crypto.GenerateKeys(KeyPairType.EC_BP384R1),
                crypto.GenerateKeys(KeyPairType.EC_BP512R1),
                crypto.GenerateKeys(KeyPairType.EC_SECP256K1),
                crypto.GenerateKeys(KeyPairType.EC_CURVE25519),
                crypto.GenerateKeys(KeyPairType.FAST_EC_ED25519),
                crypto.GenerateKeys(KeyPairType.FAST_EC_X25519)
            };

            var data          = Encoding.UTF8.GetBytes("Encrypt me!!!");
            var encryptedData = crypto.Encrypt(data, keyPairs.Select(it => it.PublicKey).ToArray());

            foreach (var keyPair in keyPairs)
            {
                var decryptedData = crypto.Decrypt(encryptedData, keyPair.PrivateKey);
                data.ShouldAllBeEquivalentTo(decryptedData);
            }
        }
Beispiel #3
0
        public void EncryptStream_SmallStreamBufferGiven_ShouldCipherStreamBeDecrypted()
        {
            var crypto  = new VirgilCrypto();
            var keyPair = crypto.GenerateKeys();

            var originalData = Encoding.UTF8.GetBytes("Hello There :)");

            byte[] cipherData;
            byte[] resultData;

            using (var inputStream = new MemoryStream(originalData))
                using (var cipherStream = new MemoryStream())
                {
                    crypto.Encrypt(inputStream, cipherStream, keyPair.PublicKey);
                    cipherData = cipherStream.ToArray();
                }

            using (var cipherStream = new MemoryStream(cipherData))
                using (var resultStream = new MemoryStream())
                {
                    crypto.Decrypt(cipherStream, resultStream, keyPair.PrivateKey);
                    resultData = resultStream.ToArray();
                }

            originalData.ShouldAllBeEquivalentTo(resultData);
        }
Beispiel #4
0
        public static byte[] DecryptMessage(byte[] senderPublicKey, byte[] receiverPrivateKey, byte[] cipherText)
        {
            var crypto     = new VirgilCrypto(KeyPairType.EC_SECP256K1);
            var ecdhKey    = Ecdh(receiverPrivateKey, senderPublicKey);
            var newKeyPair = crypto.GenerateKeys(KeyPairType.EC_SECP256K1, ecdhKey);

            return(crypto.Decrypt(cipherText, newKeyPair.PrivateKey));
        }
        public void DecryptEncryptedMessage_Should_ReturnEquivalentMessage()
        {
            var crypto        = new VirgilCrypto();
            var keyPair       = crypto.GenerateKeys();
            var messageBytes  = Encoding.UTF8.GetBytes("hi");
            var encryptedData = crypto.Encrypt(messageBytes, keyPair.PublicKey);

            Assert.AreEqual(messageBytes, crypto.Decrypt(encryptedData, keyPair.PrivateKey));
        }
Beispiel #6
0
        public void DecryptEncryptedMessageByGeneratedFromKeyMateria_Should_ReturnEquivalentMessage()
        {
            var crypto        = new VirgilCrypto();
            var keyMateria    = Encoding.UTF8.GetBytes("26dfhvnslvdsfkdfvnndsb234q2xrFOuY5EDSAFGCCXHCJSHJAD");
            var keyPair       = crypto.GenerateKeys(keyMateria);
            var messageBytes  = Encoding.UTF8.GetBytes("hi");
            var encryptedData = crypto.Encrypt(messageBytes, keyPair.PublicKey);

            Assert.AreEqual(messageBytes, crypto.Decrypt(encryptedData, keyPair.PrivateKey));
        }
        public void DecryptEncryptedMessageWithWrongPassword_Should_RaiseException()
        {
            var crypto       = new VirgilCrypto();
            var aliceKeyPair = crypto.GenerateKeys();
            var bobKeyPair   = crypto.GenerateKeys();

            var messageBytes          = Encoding.UTF8.GetBytes("hi");
            var encryptedDataForAlice = crypto.Encrypt(messageBytes, aliceKeyPair.PublicKey);

            Assert.Throws <VirgilCryptoException>(() => crypto.Decrypt(encryptedDataForAlice, bobKeyPair.PrivateKey));
        }
Beispiel #8
0
        public void EncryptData_SinglePublicKeyGiven_ShouldCipherDataBeDecrypted()
        {
            var crypto = new VirgilCrypto();

            var keyPair = crypto.GenerateKeys();

            var data          = Encoding.UTF8.GetBytes("Encrypt me!!!");
            var encryptedData = crypto.Encrypt(data, keyPair.PublicKey);
            var decryptedData = crypto.Decrypt(encryptedData, keyPair.PrivateKey);

            data.ShouldAllBeEquivalentTo(decryptedData);
        }
Beispiel #9
0
        public void EncryptData_MultiplePublicKeysGiven_ShouldCipherDataBeDecrypted()
        {
            var crypto   = new VirgilCrypto();
            var keyPairs = new List <KeyPair>();

            for (var index = 0; index < 10; index++)
            {
                keyPairs.Add(crypto.GenerateKeys());
            }

            var data          = Encoding.UTF8.GetBytes("Encrypt me!!!");
            var encryptedData = crypto.Encrypt(data, keyPairs.Select(it => it.PublicKey).ToArray());

            foreach (var keyPair in keyPairs)
            {
                var decryptedData = crypto.Decrypt(encryptedData, keyPair.PrivateKey);
                data.ShouldAllBeEquivalentTo(decryptedData);
            }
        }
Beispiel #10
0
        public void EncryptStream_SinglePublicKeyGiven_ShouldCipherStreamBeDecrypted()
        {
            var crypto  = new VirgilCrypto();
            var keyPair = crypto.GenerateKeys();

            var originalData = Encoding.UTF8.GetBytes(IntegrationHelper.RandomText);

            using (var inputStream = new MemoryStream(originalData))
                using (var cipherStream = new MemoryStream())
                    using (var resultStream = new MemoryStream())
                    {
                        crypto.Encrypt(inputStream, cipherStream, keyPair.PublicKey);

                        using (var cipherStream1 = new MemoryStream(cipherStream.ToArray()))
                        {
                            crypto.Decrypt(cipherStream1, resultStream, keyPair.PrivateKey);

                            resultStream.ToArray().ShouldAllBeEquivalentTo(originalData);
                        }
                    }
        }
Beispiel #11
0
        public void EncryptStream_MultiplePublicKeysGiven_ShouldCipherStreamBeDecrypted()
        {
            var crypto   = new VirgilCrypto();
            var keyPairs = new List <KeyPair>
            {
                crypto.GenerateKeys(KeyPairType.EC_SECP256R1),
                crypto.GenerateKeys(KeyPairType.EC_SECP384R1),
                crypto.GenerateKeys(KeyPairType.EC_SECP521R1),
                crypto.GenerateKeys(KeyPairType.EC_BP256R1),
                crypto.GenerateKeys(KeyPairType.EC_BP384R1),
                crypto.GenerateKeys(KeyPairType.EC_BP512R1),
                crypto.GenerateKeys(KeyPairType.EC_SECP256K1),
                crypto.GenerateKeys(KeyPairType.EC_CURVE25519),
                crypto.GenerateKeys(KeyPairType.FAST_EC_ED25519),
                crypto.GenerateKeys(KeyPairType.FAST_EC_X25519)
            };

            var originalData = Encoding.UTF8.GetBytes(IntegrationHelper.RandomText);

            foreach (var keyPair in keyPairs)
            {
                using (var inputStream = new MemoryStream(originalData))
                    using (var cipherStream = new MemoryStream())
                        using (var resultStream = new MemoryStream())
                        {
                            crypto.Encrypt(inputStream, cipherStream, keyPairs.Select(it => it.PublicKey).ToArray());

                            using (var cipherStream1 = new MemoryStream(cipherStream.ToArray()))
                            {
                                crypto.Decrypt(cipherStream1, resultStream, keyPair.PrivateKey);

                                resultStream.ToArray().ShouldAllBeEquivalentTo(originalData);
                            }
                        }
            }
        }
        public IReadOnlyCollection <Valuable> DecryptValuablesCiphertext
        (
            FindValuablesProjection projection
        )
        {
            var virgilCrypto = new VirgilCrypto();

            SerilogLogger.Instance.Debug
            (
                "Generating the key hash. AppId='{AppId}'",
                _appId
            );

            var appKey = Convert.ToBase64String
                         (
                virgilCrypto.GenerateHash
                (
                    Encoding.UTF8.GetBytes
                    (
                        _appPassword
                    ),
                    HashAlgorithm.SHA512
                )
                         );

            SerilogLogger.Instance.Debug
            (
                "Importing the private key. AppId='{AppId}'",
                _appId
            );

            var privateKey = virgilCrypto.ImportPrivateKey
                             (
                Convert.FromBase64String(projection.PrivateKeyCiphertext),
                appKey
                             );

            SerilogLogger.Instance.Debug
            (
                "Decrypting the app settings. AppId='{AppId}'",
                _appId
            );

            var decrypted = Encoding.UTF8.GetString
                            (
                virgilCrypto.Decrypt
                (
                    Convert.FromBase64String(projection.Ciphertext),
                    privateKey
                )
                            );

            var valuables = JsonConvert.DeserializeObject <IReadOnlyCollection <Valuable> >
                            (
                decrypted
                            );

            AddOption
            (
                ConfigurKeys.SignalRAccessToken,
                projection.SignalR.AccessToken
            );

            AddOption
            (
                ConfigurKeys.SignalRUrl,
                projection.SignalR.Url
            );

            return(valuables);
        }