public void RsaCipherCipher_Encryption_Test()
        {
            var rsaKey  = RsaCipher.GetKey();
            var encrypt = RsaCipher.Encrypt("VeyselMUTLU", rsaKey["PublicKey"]);

            var decrypt = RsaCipher.Decrypt(encrypt, rsaKey["PrivateKey"]);

            Assert.AreEqual("VeyselMUTLU", decrypt);
        }
Esempio n. 2
0
        public void DecryptTest()
        {
            RsaKey    key    = null;               // TODO: Initialize to an appropriate value
            RsaCipher target = new RsaCipher(key); // TODO: Initialize to an appropriate value

            byte[] data     = null;                // TODO: Initialize to an appropriate value
            byte[] expected = null;                // TODO: Initialize to an appropriate value
            byte[] actual;
            actual = target.Decrypt(data);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
        public bool Decrypt(EncryptedMessage encryptedMessage, int senderId, out string messageText)
        {
            if (encryptedMessage == null)
            {
                throw new ArgumentException("Encrypted message cannot be null");
            }

            if (encryptedMessage.Body == null || encryptedMessage.DigitalSignature == null ||
                encryptedMessage.SymmetricKey == null || encryptedMessage.Iv == null)
            {
                throw new ArgumentException("Not all encrypted message fields are initialized");
            }

            IContactModel senderContact = _storageService.GetContacts().FirstOrDefault(c => c.Id == senderId);

            if (senderContact == null)
            {
                throw new ArgumentException("Contact with id of senderId does not exist");
            }

            string receiverKeyPair = _storageService.GetUser().KeyPair;
            string senderPublicKey = senderContact.PublicKey;

            try
            {
                // decrypt symmetric key with receivers private key
                RsaCipher rsa = new RsaCipher(receiverKeyPair);
                byte[]    encryptedSymmetricKeyBytes = FormatConverter.String64ToBytes(encryptedMessage.SymmetricKey);
                byte[]    decryptedSymmetricKeyBytes = rsa.Decrypt(encryptedSymmetricKeyBytes);

                // decrypt message text with jsut decrypted symmetric key
                byte[]    ivBytes = FormatConverter.String64ToBytes(encryptedMessage.Iv);
                AesCipher aes     = new AesCipher(decryptedSymmetricKeyBytes, ivBytes);
                byte[]    encryptedMessageBytes = FormatConverter.String64ToBytes(encryptedMessage.Body);
                byte[]    decryptedMessageBytes = aes.Decrypt(encryptedMessageBytes);

                // set message text out parameter
                messageText = FormatConverter.BytesToString(decryptedMessageBytes);

                // verify digital signature
                rsa = new RsaCipher(senderPublicKey);
                byte[] digitalSignatureBytes = FormatConverter.String64ToBytes(encryptedMessage.DigitalSignature);
                bool   signatureOk           = rsa.VerifyDigitalSignature(decryptedMessageBytes, digitalSignatureBytes);

                return(signatureOk);
            }
            catch (Exception ex)
            {
                messageText = null;
                return(false);
            }
        }
Esempio n. 4
0
        public void EncryptAndDecrypt()
        {
            var pair = new RsaKeyPair();

            var str = "Hello";

            var encoded = RsaCipher.Encrypt(str, pair.PublicKey);

            Assert.NotEqual(str, encoded);

            var decoded = RsaCipher.Decrypt(encoded, pair.PrivateKey);

            Assert.Equal(str, decoded);
        }
Esempio n. 5
0
        public byte[] DecryptSsh1(BigInteger e, BigInteger n, BigInteger encryptedChallenge, byte[] sessionId)
        {
            var decryptKey = GetKey(e, n);

            if (decryptKey == null)
            {
                return(null);
            }

            RsaCipher cipher = new RsaCipher((RsaKey)decryptKey.Key.Key);

            byte[] decryptedChallenge = cipher.Decrypt(encryptedChallenge.ToByteArray().Reverse().ToArray());

            var md5 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);

            byte[] response;
            CryptographicBuffer.CopyToByteArray(md5.HashData(CryptographicBuffer.CreateFromByteArray(decryptedChallenge.Concat(sessionId).ToArray())), out response);
            return(response);
        }