Beispiel #1
0
        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, HouseofCat.Encryption.Constants.Aes256.KeySize)
                          .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.Aes256Encrypt(payload, hashKey);

            // Decomcrypt
            var decryptedData = AesEncrypt.Aes256Decrypt(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);
        }
Beispiel #2
0
        // 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.Aes256Decrypt(letter.Body, _hashKey);
                letter.LetterMetadata.Encrypted = false;

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

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

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

            return(true);
        }
Beispiel #3
0
        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.Aes256Encrypt(data, hashKey);

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

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

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

            Assert.Equal(data, decryptedData);
        }
Beispiel #4
0
 public void EncryptDecrypt512()
 {
     var decryptedData = AesEncrypt.Aes256Decrypt(AesEncrypt.Aes256Encrypt(Payload2, HashKey), HashKey);
 }