public void DecryptExceptionTest()
        {
            string text     = "Teststring";
            string password = string.Empty;
            string salt     = "Teststring";
            string actual;

            actual = CipherUtility.Decrypt <AesManaged>(text, password, salt);
            Assert.Fail("An Exception should have been thrown");
        }
Ejemplo n.º 2
0
        public void CipherUtilitySymmetric()
        {
            string plaintext = "IamAPl4inTextSTring!@#$%^&*()<>";
            string salt      = CipherUtility.GetNewSalt();

            string encryptedText = CipherUtility.Encrypt(plaintext, salt);
            string decryptedText = CipherUtility.Decrypt(encryptedText, salt);

            Assert.AreEqual(plaintext, decryptedText);
        }
Ejemplo n.º 3
0
        private string ExtractPayloadFromGram(string payloadGram)
        {
            try
            {
                var gram = Serializer <PayloadGram> .FromJson(payloadGram);

                var originalPayload       = gram.Payload;
                var originalPayloadHash   = gram.HashValidation;
                int originalPayloadLength = -1;
                if (gram.IsEncrypted)
                {
                    originalPayload     = _Cipher.Decrypt(gram.Payload, _DropZoneConfig.Password, _DropZoneConfig.Salt);
                    originalPayloadHash = _Cipher.Decrypt(gram.HashValidation, _DropZoneConfig.Password, _DropZoneConfig.Salt);
                    var payloadLength = _Cipher.Decrypt(gram.Length, _DropZoneConfig.Password, _DropZoneConfig.Salt);
                    int.TryParse(payloadLength.Split(new char[] { ',' })[1], out originalPayloadLength);
                }
                else
                {
                    int.TryParse(gram.Length, out originalPayloadLength);
                }
                if (originalPayloadLength != originalPayload.Length)
                {
                    throw new ArgumentOutOfRangeException($"Expected length of {originalPayload.Length}, but got {originalPayloadLength}");
                }
                if (string.Compare(
                        Hasher.GetHashFromStringContent(originalPayload, Encoding.UTF8, Hasher.HashMethod.SHA256),
                        originalPayloadHash, false) != 0)
                {
                    throw new ArgumentOutOfRangeException($"Expected hash validation value of {gram.HashValidation}, but got {originalPayloadHash}");
                }
                return(originalPayload);
            }
            catch (Exception err)
            {
                throw new ArgumentOutOfRangeException("Failure to unpack payload from datagram", err);
            }
        }
        public void DecryptionEmptyAesKeyTest()
        {
            String  tempAeskey     = CipherUtility.TemporaryAESKeyDelete();
            Boolean testSuccessful = false;

            try
            {
                CipherUtility.Decrypt("Teststring", "Teststting");
            }
            catch
            {
                testSuccessful = CipherUtility.TemporaryAESKeyRestore(tempAeskey);
            }
            Assert.IsTrue(testSuccessful);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Decrypt method
        /// </summary>
        /// <param name="algorithm">Encryption algorithm to use</param>
        /// <param name="encryptedText">Encrypted text to decrypt</param>
        /// <param name="password">A password is a word or string of characters used for user authentication to prove identity or access approval to gain access to a resource, which is to be kept secret from those not allowed access.</param>
        /// <param name="salt">In cryptography, a salt is random data that is used as an additional input to a one-way function that "hashes" data, a password or passphrase.</param>
        /// <returns>Decrypted text (plain text)</returns>
        public static string Decrypt(ServiceProvider algorithm, string encryptedText, string password, string salt)
        {
            switch (algorithm)
            {
            case ServiceProvider.AES:
                return(CipherUtility.Decrypt <AesManaged>(encryptedText, password, salt));

            case ServiceProvider.DES:
                return(CipherUtility.Decrypt <DESCryptoServiceProvider>(encryptedText, password, salt));

            case ServiceProvider.RC2:
                return(CipherUtility.Decrypt <RC2CryptoServiceProvider>(encryptedText, password, salt));

            case ServiceProvider.Rijndael:
                return(CipherUtility.Decrypt <RijndaelManaged>(encryptedText, password, salt));

            case ServiceProvider.TripleDES:
                return(CipherUtility.Decrypt <TripleDESCryptoServiceProvider>(encryptedText, password, salt));

            default:
                return(null);
            }
        }
Ejemplo n.º 6
0
 private void btnDecrypt_Click(object sender, EventArgs e)
 {
     try
     {
         var type   = Type.GetType((string)this.cbxEncryptionMethod.Items[this.cbxEncryptionMethod.SelectedIndex]);
         var cipher = new CipherUtility((System.Security.Cryptography.SymmetricAlgorithm)Activator.CreateInstance(type));
         if (this.rbString.Checked)
         {
             this.txtResult.Text = cipher.Decrypt(this.txtSource.Text, this.txtPassword.Text, this.txtSalt.Text);
         }
         else
         {
             this.txtResult.Text = Encoding.UTF8.GetString(
                 cipher.DecryptByteArray(
                     this.txtSource.Text,
                     this.txtPassword.Text,
                     this.txtSalt.Text));
         }
     }
     catch (Exception err)
     {
         MessageBox.Show(BOG.Framework.DetailedException.WithUserContent(ref err), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Ejemplo n.º 7
0
        public static void HandleClient(object obj)
        {
            TcpClient    client  = (TcpClient)obj;
            StreamReader sReader = new StreamReader(client.GetStream(), Encoding.ASCII);
            StreamWriter sWriter = new StreamWriter(client.GetStream(), Encoding.ASCII);
            string       sData   = null;

            IPEndPoint endPoint      = (IPEndPoint)client.Client.RemoteEndPoint;
            IPEndPoint localEndPoint = (IPEndPoint)client.Client.LocalEndPoint;

            Console.WriteLine(endPoint + " connected!");

            infoSender.Add(endPoint, sWriter);

            string simon      = "Enter username: "******"password", "salt");

            sWriter.WriteLine(encryptedd);
            sWriter.Flush();
            try
            {
                sData = sReader.ReadLine();
                string dedcrypt = CipherUtility.Decrypt <AesManaged>(sData, "password", "salt");
                usernames.Add(endPoint, dedcrypt);
            }
            catch (Exception e)
            {
                Console.WriteLine(client.Client.RemoteEndPoint + " disconnected!");
                UserDisconnected(endPoint);
            }
            lock (connectedUsersLock)
            {
                connectedUsers.Add(endPoint);
                Console.WriteLine("Queued users: {0}", connectedUsers.Count);
            }
            while (client.Connected)
            {
                try
                {
                    sData = sReader.ReadLine();
                    sData = CipherUtility.Decrypt <AesManaged>(sData, "password", "salt");
                    //Console.WriteLine("Encrypted data recieved: " + sData);
                    if (sData.Length > 2)
                    {
                        foreach (GameWorld gw in gwList)
                        {
                            if (gw.playerOneEP == endPoint || gw.playerTwoEP == endPoint)
                            {
                                gw.StringToMap(endPoint, sData);
                            }
                        }
                    }

                    #region
                    if (!dataReceived)
                    {
                        if (sData != "baae866ac1607d6fe25b3b57406d22d28e1bd2f9027daabfbbc44cf11e5e6050")
                        {
                            dataReceived = true;
                            Console.WriteLine("Client doesnt have the right MD5!");
                            string errorOne = "Your MD5 is not RIGHT!, disconnecting.";
                            string MD5Error = CipherUtility.Encrypt <AesManaged>(errorOne, "password", "salt");
                            sWriter.WriteLine(MD5Error);
                            sWriter.Flush();
                            client.Close();
                            break;
                        }
                    }
                    #endregion
                    Console.WriteLine("Data recieved and decrypted: " + sData);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(client.Client.RemoteEndPoint + " disconnected!");
                    UserDisconnected(endPoint);
                }

                if (matchedUsers.Contains(endPoint))
                {
                    try
                    {
                        foreach (GameWorld gw in gwList)
                        {
                            if (gw.playerOneEP == endPoint)
                            {
                                if (gw.PlayerOneTurn)
                                {
                                    gw.TurnMaster(endPoint, sData);
                                }
                                break;
                            }
                            else if (gw.playerTwoEP == endPoint)
                            {
                                if (!gw.PlayerOneTurn)
                                {
                                    gw.TurnMaster(endPoint, sData);
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public void Read()
        {
            string incomingData = null;

            _sReader = new StreamReader(_client.GetStream(), Encoding.ASCII);

            while (_isConnected)
            {
                try
                {
                    incomingData = _sReader.ReadLine();
                    string decrypted = CipherUtility.Decrypt <AesManaged>(incomingData, "password", "salt");
                    if (decrypted == "Matched")
                    {
                        Console.Clear();
                        Console.SetCursorPosition(40, 40);
                        Program.Matched = true;
                        Console.Clear();
                        gw.GameSetup();
                        gw.Play();
                        string sData = CipherUtility.Encrypt <AesManaged>(gw.YourMap.ReadMap(), "password", "salt");
                        _sWriter.WriteLine(sData);
                        _sWriter.Flush();
                    }
                    else
                    {
                        string tileShot = decrypted.Substring(decrypted.Length - 2);
                        int    posY     = 0;
                        #region Switch
                        switch (tileShot.Remove(1))
                        {
                        case "a":
                            posY = 0;
                            break;

                        case "b":
                            posY = 1;
                            break;

                        case "c":
                            posY = 2;
                            break;

                        case "d":
                            posY = 3;
                            break;

                        case "e":
                            posY = 4;
                            break;

                        case "f":
                            posY = 5;
                            break;

                        case "g":
                            posY = 6;
                            break;

                        case "h":
                            posY = 7;
                            break;

                        case "i":
                            posY = 8;
                            break;

                        case "j":
                            posY = 9;
                            break;
                        }
                        #endregion

                        if (decrypted.Contains("missed"))
                        {
                            if (decrypted.Contains(username))
                            {
                                gw.EnemyMap.MarkTile(int.Parse(tileShot.Substring(1)), posY, 'M', ConsoleColor.Red);
                            }
                            else
                            {
                                gw.YourMap.MarkTile(int.Parse(tileShot.Substring(1)), posY, 'M', ConsoleColor.Green);
                            }
                        }
                        else if (decrypted.Contains("hit"))
                        {
                            if (decrypted.Contains(username))
                            {
                                gw.EnemyMap.MarkTile(int.Parse(tileShot.Substring(1)), posY, 'H', ConsoleColor.Green);
                            }
                            else
                            {
                                gw.YourMap.MarkTile(int.Parse(tileShot.Substring(1)), posY, 'H', ConsoleColor.Red);
                            }
                        }

                        Console.WriteLine(decrypted);
                        ClearCurrentConsoleLine();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("\n" + e.Message);
                    Console.ReadLine();
                }
            }
        }