public void When_PlanTextIsGiven_Then_EncyptText()
        {
            //Arrange
            byte[] key  = { 145, 12, 32, 245, 98, 132, 98, 214, 6, 77, 131, 44, 221, 3, 9, 50 };
            byte[] iv   = { 15, 122, 132, 5, 93, 198, 44, 31, 9, 39, 241, 49, 250, 188, 80, 7 };
            string name = "Password";

            //Act
            string chiperText = TextEncryptor.EncryptPlainText(key, iv, name);
            string plainText  = TextEncryptor.DecryptChiperText(key, iv, chiperText);

            //Assert
            Assert.AreEqual(name, plainText);
        }
        public void When_KeyIsNull_Then_EncryptorExceptionIsThrown_With_KeycannotbenullMessage()
        {
            //Arrange
            byte[] key  = null;
            byte[] iv   = { 15, 122, 132, 5, 93, 198, 44, 31, 9, 39, 241, 49, 250, 188, 80, 7 };
            string name = "Password";

            try
            {
                //Act
                string chiperText = TextEncryptor.DecryptChiperText(key, iv, name);
            }
            catch (Exception ex)
            {
                //Assert
                bool exceptiontype = ex is EncryptorExpection;
                Assert.IsTrue(exceptiontype);
                Assert.AreEqual("Key cannot be null", ex.Message);
            }
        }
        public void When_ChiperTestIsNullOrEmpty_Then_ArgumentExceptionIsThrownWith_ChipertextcannotbenulloremptyMessage()
        {
            //Arrange
            byte[] key  = { 145, 12, 32, 245, 98, 132, 98, 214, 6, 77, 131, 44, 221, 3, 9, 50 };
            byte[] iv   = { 15, 122, 132, 5, 93, 198, 44, 31, 9, 39, 241, 49, 250, 188, 80, 7 };
            string name = null;

            try
            {
                //Act
                string chiperText = TextEncryptor.DecryptChiperText(key, iv, name);
            }
            catch (Exception ex)
            {
                //Assert
                bool exceptiontype = ex is EncryptorExpection;
                Assert.IsTrue(exceptiontype);
                Assert.AreEqual(ex.Message, "Chipertext cannot be null or empty");
            }
        }
        public void When_IvIsNull_Then_ArgumentExceptionIsThrownWith_InitializationVectorCannotBeNull()
        {
            //Arrange
            byte[] key  = { 145, 12, 32, 245, 98, 132, 98, 214, 6, 77, 131, 44, 221, 3, 9, 50 };
            byte[] iv   = null;
            string name = "Password";

            try
            {
                //Act
                string chiperText = TextEncryptor.DecryptChiperText(key, iv, name);
            }
            catch (Exception ex)
            {
                //Assert
                bool exceptiontype = ex is EncryptorExpection;
                Assert.IsTrue(exceptiontype);
                Assert.AreEqual(ex.Message, "Initialization vector cannot be null");
            }
        }
Exemplo n.º 5
0
        public void HandleCommunication()
        {
            _sReader = new StreamReader(_client.GetStream(), Encoding.UTF8);
            _sWriter = new StreamWriter(_client.GetStream(), Encoding.UTF8);

            _isConnected = _client.Connected;
            String sData = null;

            while (_isConnected)
            {
                _isConnected = _client.Connected;
                if (_isConnected == false)
                {
                    break;
                }
                Console.Write("(Client)Введите ссобщение ");
                sData = TextEncryptor.Encrypt(Console.ReadLine(), Settings.node.node_encrypt_key);
                if (sData == "exit")
                {
                    _sWriter.WriteLine(sData);
                    _sWriter.Flush();
                    break;
                }
                // write data and make sure to flush, or the buffer will continue to
                // grow, and your data might not be sent when you want it, and will
                // only be sent once the buffer is filled.
                _sWriter.WriteLine(sData);
                _sWriter.Flush();

                // if you want to receive anything
                String sDataIncomming = TextEncryptor.Decrypt(_sReader.ReadLine(), Settings.node.node_private_key);
                Console.WriteLine("(Client)Answer from server: " + sDataIncomming);
            }

            _sReader.Close();
            _sWriter.Close();
            Environment.Exit(-1);
        }
Exemplo n.º 6
0
        public void HandleClient(object obj)
        {
            // retrieve client from parameter passed to thread
            TcpClient client = (TcpClient)obj;

            // sets two streams
            StreamWriter sWriter = new StreamWriter(client.GetStream(), Encoding.UTF8);
            StreamReader sReader = new StreamReader(client.GetStream(), Encoding.UTF8);
            // you could use the NetworkStream to read and write,
            // but there is no forcing flush, even when requested

            Boolean bClientConnected = true;
            String  sData            = null;

            log.Info("Client connected: " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
            while (bClientConnected)
            {
                // reads from stream
                sData = TextEncryptor.Decrypt(sReader.ReadLine(), Settings.node.node_encrypt_key);

                // shows content on the console.
                log.Info("(Server)Client data: " + sData);
                Console.WriteLine("(Server)Client data: " + sData);

                if (sData == "exit")
                {
                    break;
                }

                // to write something back.
                sWriter.WriteLine(sData);
                sWriter.Flush();
            }
            sWriter.Close();
            sReader.Close();
        }
Exemplo n.º 7
0
 public static string Decrypt(this string source, TextEncryptor enc, string code)
 {
     return(enc.Decrypt(source, code));
 }
Exemplo n.º 8
0
 public static string GenerateToken(LoginToken loginToken, TextEncryptor signer)
 {
     AssertUtils.AssertNotNull(loginToken);
     return(signer.Encrypt(JsonConvert.SerializeObject(loginToken)));
 }
Exemplo n.º 9
0
 public static LoginToken ParseToken(string loginToken, TextEncryptor signer)
 {
     AssertUtils.AssertNotNull(loginToken);
     return(JsonConvert.DeserializeObject <LoginToken>(signer.Decrypt(loginToken)));
 }