Exemple #1
0
    /// <summary>
    /// aes加混淆解密接口
    /// </summary>
    static public byte[] ExecuteDecrypt(byte[] buffer, string key)
    {
        //这里后续附加混淆规则
        int bufferLen = buffer.Length;

        if (bufferLen > MAX_ENCRYPT_LEN)
        {
            //规定 ECBMode:(明文长度/16 + 1) * 16 = 密文长度
            int    decryLen    = (MAX_ENCRYPT_LEN / BLOCK_SIZE + 1) * BLOCK_SIZE;
            byte[] decryBuffer = new byte[decryLen];
            Array.Copy(buffer, decryBuffer, decryLen);
            byte[] beDecryBuffer = AesEncrypt.Decrypt(decryBuffer, key);
            int    retLen        = bufferLen - decryLen + beDecryBuffer.Length;
            byte[] retBuffer     = new byte[retLen];
            beDecryBuffer.CopyTo(retBuffer, 0);
            Array.Copy(buffer, decryLen, retBuffer, beDecryBuffer.Length, retBuffer.Length - beDecryBuffer.Length);

            for (int i = beDecryBuffer.Length, j = 0; i < retLen; i += CONFUSE_OFFSET)
            {
                retBuffer[i] -= beDecryBuffer[j];
                j            += 2;
                if (j >= beDecryBuffer.Length)
                {
                    j = 0;
                }
            }

            return(retBuffer);
        }
        else
        {
            return(AesEncrypt.Decrypt(buffer, key));
        }
    }
Exemple #2
0
        /// <summary>
        /// 服务端收到数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AsyncNetUdpServer_UdpPacketArrived(object sender, UdpPacketArrivedEventArgs e)
        {
            String dataJson = AesEncrypt.Decrypt(Program.AppSettings.ControlKey, e.PacketData).TrimEnd('\0');

            Program.Logger.Log("UdpSocketServer", $"UdpPacketArrived=>{e.RemoteEndPoint.Address}:{e.RemoteEndPoint.Port}=>{dataJson}");
            if (String.IsNullOrWhiteSpace(dataJson) || dataJson.IndexOf("ActionId") < 0 || dataJson.IndexOf("ActionName") < 0)
            {
                this.AsyncNetUdpServer.Post($"UnknownDataPacket,Logged\"{e.RemoteEndPoint.Address}:{e.RemoteEndPoint.Port}\"".GetBytes_Utf8(), e.RemoteEndPoint);
                return;
            }
            Entity.UdpSocketPacketRecive packet = null;
            try { packet = JsonConvert.DeserializeObject <Entity.UdpSocketPacketRecive>(dataJson); } catch {}
            if (packet == null)
            {
                this.AsyncNetUdpServer.Post($"UnknownDataPacket,Logged\"{e.RemoteEndPoint.Address}:{e.RemoteEndPoint.Port}\"".GetBytes_Utf8(), e.RemoteEndPoint);
                return;
            }
            switch (packet.ActionId)
            {
            case 1: this.PacketAction1(e.RemoteEndPoint); break;

            case 2: this.PacketAction2(e.RemoteEndPoint); break;

            case 1001: this.PacketAction1001(e.RemoteEndPoint); break;

            case 1002: this.PacketAction1002(e.RemoteEndPoint); break;

            case 1003: this.PacketAction1003(e.RemoteEndPoint, packet); break;

            case 1004: this.PacketAction1004(e.RemoteEndPoint); break;

            case 1005: this.PacketAction1005(e.RemoteEndPoint, packet); break;
            }
        }
        public async Task DecomcryptAsync(Letter letter)
        {
            var decryptFailed = false;

            if (letter.LetterMetadata.Encrypted && (_hashKey?.Length > 0))
            {
                try
                {
                    letter.Body = AesEncrypt.Decrypt(letter.Body, _hashKey);
                    letter.LetterMetadata.Encrypted = false;
                }
                catch { decryptFailed = true; }
            }

            if (!decryptFailed && letter.LetterMetadata.Compressed)
            {
                try
                {
                    letter.Body = await Gzip.DecompressAsync(letter.Body).ConfigureAwait(false);

                    letter.LetterMetadata.Compressed = false;
                }
                catch { }
            }
        }
        // Returns Success
        public bool Decrypt(Letter letter)
        {
            if (!letter.LetterMetadata.Encrypted || (_hashKey == null && _hashKey.Length == 0))
            {
                return(false);
            }                 // Don't decrypt without it being encrypted.

            try
            {
                letter.Body = AesEncrypt.Decrypt(letter.Body, _hashKey);
                letter.LetterMetadata.Encrypted = false;

                if (letter.LetterMetadata.CustomFields.ContainsKey(Utils.Constants.HeaderForEncrypt))
                {
                    letter.LetterMetadata.CustomFields.Remove(Utils.Constants.HeaderForEncrypt);
                }

                if (letter.LetterMetadata.CustomFields.ContainsKey(Utils.Constants.HeaderForEncryptDate))
                {
                    letter.LetterMetadata.CustomFields.Remove(Utils.Constants.HeaderForEncryptDate);
                }
            }
            catch { return(false); }

            return(true);
        }
        public async Task ComcryptDecomcryptTest()
        {
            var message = new Message {
                StringMessage = $"Sensitive ReceivedLetter 0", MessageId = 0
            };
            var data = JsonSerializer.SerializeToUtf8Bytes(message);

            var hashKey = await ArgonHash
                          .GetHashKeyAsync(Passphrase, Salt, Constants.EncryptionKeySize)
                          .ConfigureAwait(false);

            _output.WriteLine(Encoding.UTF8.GetString(hashKey));
            _output.WriteLine($"HashKey: {Encoding.UTF8.GetString(hashKey)}");

            // Comcrypt
            var payload = await Gzip.CompressAsync(data);

            var encryptedPayload = AesEncrypt.Encrypt(payload, hashKey);

            // Decomcrypt
            var decryptedData = AesEncrypt.Decrypt(encryptedPayload, hashKey);

            Assert.NotNull(decryptedData);

            var decompressed = await Gzip.DecompressAsync(decryptedData);

            JsonSerializer.SerializeToUtf8Bytes(decompressed);
            _output.WriteLine($"Data: {Encoding.UTF8.GetString(data)}");
            _output.WriteLine($"Decrypted: {Encoding.UTF8.GetString(decryptedData)}");

            Assert.Equal(data, decompressed);
        }
        public bool Decrypt(Letter letter)
        {
            if (letter.LetterMetadata.Encrypted && (_hashKey?.Length > 0))
            {
                try
                {
                    letter.Body = AesEncrypt.Decrypt(letter.Body, _hashKey);
                    letter.LetterMetadata.Encrypted = false;
                }
                catch { }
            }

            return(!letter.LetterMetadata.Encrypted);
        }
        public async Task DecomcryptDataAsync(bool decrypt = false, bool decompress = false)
        {
            if (!_decrypted && decrypt && _hashKey.Length > 0)
            {
                Data       = AesEncrypt.Decrypt(Data, _hashKey);
                _decrypted = true;
            }

            if (!_decompressed && decompress)
            {
                Data = await Gzip
                       .DecompressAsync(Data)
                       .ConfigureAwait(false);

                _decompressed = true;
            }
        }
        public async Task EncryptDecryptTest()
        {
            var data = new byte[] { 0xFF, 0x00, 0xAA, 0xFF, 0x00, 0x00, 0xFF, 0xAA, 0x00, 0xFF, 0x00, 0xFF };

            var hashKey = await ArgonHash
                          .GetHashKeyAsync(Passphrase, Salt, 32)
                          .ConfigureAwait(false);

            _output.WriteLine(Encoding.UTF8.GetString(hashKey));
            _output.WriteLine($"HashKey: {Encoding.UTF8.GetString(hashKey)}");

            var encryptedData = AesEncrypt.Encrypt(data, hashKey);

            _output.WriteLine($"Encrypted: {Encoding.UTF8.GetString(encryptedData)}");

            var decryptedData = AesEncrypt.Decrypt(encryptedData, hashKey);

            _output.WriteLine($"Data: {Encoding.UTF8.GetString(data)}");
            _output.WriteLine($"Decrypted: {Encoding.UTF8.GetString(decryptedData)}");

            Assert.Equal(data, decryptedData);
        }
        public async Task CreateLetterFromDataAsync()
        {
            if (Letter == null)
            {
                Letter = JsonSerializer.Deserialize <Letter>(Data);
            }

            if (!_decrypted && Letter.LetterMetadata.Encrypted && _hashKey.Length > 0)
            {
                Letter.Body = AesEncrypt.Decrypt(Letter.Body, _hashKey);
                Letter.LetterMetadata.Encrypted = false;
                _decrypted = true;
            }

            if (!_decompressed && Letter.LetterMetadata.Compressed)
            {
                Letter.Body = await Gzip
                              .DecompressAsync(Letter.Body)
                              .ConfigureAwait(false);

                Letter.LetterMetadata.Compressed = false;
                _decompressed = true;
            }
        }
 public void EncryptDecrypt1024()
 {
     var decryptedData = AesEncrypt.Decrypt(AesEncrypt.Encrypt(Payload3, HashKey), HashKey);
 }
 public void EncryptDecrypt512()
 {
     var decryptedData = AesEncrypt.Decrypt(AesEncrypt.Encrypt(Payload2, HashKey), HashKey);
 }
 public void EncryptDecrypt2048()
 {
     var decryptedData = AesEncrypt.Decrypt(AesEncrypt.Encrypt(Payload4, HashKey), HashKey);
 }