Example #1
0
        public void Send(MessageProtocols protocol, object obj = null, bool encrypt = true)
        {
            if (writer == null || writer.BaseStream == null)
            {
                return;
            }

            string line = "";

            if (obj != null)
            {
                line = JsonConvert.SerializeObject(obj);
            }

            if (encrypt && cipher != null)
            {
                line = NetworkUtil.InsertEscape(cipher.Encrypt(protocol.ToString())) + "," + NetworkUtil.InsertEscape(cipher.Encrypt(line));
            }
            else
            {
                line = NetworkUtil.InsertEscape(protocol.ToString()) + "," + NetworkUtil.InsertEscape(line);
            }

            try
            {
                writer.WriteLine(line);
                writer.Flush();
            }
            catch (Exception) { }
        }
Example #2
0
        public void HashAndEncrypt(uint hashDigestSize, uint encryptStartOffset, SymmetricCipher theCipher)
        {
            var digestStart = GetBytePosition();

            SetBytePosition(digestStart);

            var hash = new SHA256Managed().ComputeHash(Data, 0, (int)digestStart);

            Write(hashDigestSize, hash);

            theCipher.Encrypt(GetBuffer(), encryptStartOffset, GetBuffer(), encryptStartOffset, GetBytePosition() - encryptStartOffset);
        }
Example #3
0
        public static void TestSymmetricEncrypt1()
        {
            Console.WriteLine("TestSymmetric1");

            var cipher = new SymmetricCipher(SymmetricAlgorithm.Des);

            Console.WriteLine($"secretKey:{cipher.Base64SecretKey}");

            var encrypted = cipher.Encrypt(Encoding.UTF8.GetBytes("对称加密测试1"));

            Console.WriteLine($"encrypted:{Convert.ToBase64String(encrypted)}");

            var decrypted = cipher.Decrypt(encrypted);

            Console.WriteLine($"decrypted:{Encoding.UTF8.GetString(decrypted)}");
        }
Example #4
0
        public static void TestSymmetricEncrypt2()
        {
            Console.WriteLine("TestSymmetric2");

            var secretKey = Convert.FromBase64String("6V2SlGS25bw=");
            var cipher    = new SymmetricCipher(SymmetricAlgorithm.Des, secretKey);

            var encrypted = cipher.Encrypt(Encoding.UTF8.GetBytes("对称加密测试2"));

            Console.WriteLine($"encrypted:{Convert.ToBase64String(encrypted)}");

            // encrypted = Convert.FromBase64String("uMi0zTne1HMv5sU1HB2WI/CsNvBUn5y3");

            var decrypted = cipher.Decrypt(encrypted);

            Console.WriteLine($"decrypted:{Encoding.UTF8.GetString(decrypted)}");
        }
Example #5
0
 /// <summary>
 /// Sends a byte array to a remote peer after encrypting it
 /// </summary>
 public void SendData(byte[] data)
 {
     data = SymmetricCipher.Encrypt(data);
     Connection.SendData(data);
 }
        public void DecryptToString_EncryptAndDecryptToString_Pass()
        {
            string plainText = "Encrypt me but don' forget me.";
            byte[] plaindata = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptedText;
            string decryptedText;
            using (var cipher = new SymmetricCipher<AesManaged>("passwd", "mysalt1337"))
            {
                encryptedText = cipher.Encrypt(plaindata);
                decryptedText = cipher.DecryptToString(encryptedText);
            }

            Assert.AreEqual(plainText, decryptedText);
        }
        public void BasicCipher_TrippleDESSimpleCryption_ShouldPass()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;
            byte[] IV;

            using (var cipher = new SymmetricCipher<TripleDESCryptoServiceProvider>("mypasswd2", "mysalt1337bb44"))
            {
                IV = cipher.IV;
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<TripleDESCryptoServiceProvider>("mypasswd2", "mysalt1337bb44", IV))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }

            CollectionAssert.AreEqual(plainTextArr, decryptArr);
        }
        public void BasicCipher_RijndaelSimpleCryption_ShouldPass()
        {
            string plainText = "Encryption is pretty fun";
            string passwd = "mypasswd2";
            string salt = "mysalt1337bb44";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;
            byte[] IV;

            using (var cipher = new SymmetricCipher<RijndaelManaged>(passwd, salt))
            {
                IV = cipher.IV;
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<RijndaelManaged>(passwd, salt, IV))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }

            CollectionAssert.AreEqual(plainTextArr, decryptArr);
        }
        public void AesBasicCipher_MultiInstance_WrongSaltFail()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "mysalt1337bb44"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "mysalt1337bb"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }
        }
        public void AesBasicCipher3_SameInstance_ComparesOutput()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;
            string decryptStr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "mysalt1337bb"))
            {
                encryptArr = cipher.Encrypt(plainText);
                decryptArr = cipher.Decrypt(encryptArr);
            }
            decryptStr = Encoding.UTF8.GetString(decryptArr);

            Assert.AreEqual(plainText, decryptStr);
        }