Example #1
0
        static void Main(string[] args)
        {
            /*                 Readme examples            */

            // Create encrypter with default algorithm (AES) and generate a new random key
            var encrypter = new EasyEncrypt();

            // Encrypt and decrypt a string
            var encryptedString = encrypter.Encrypt("Example data");
            var decryptedString = encrypter.Decrypt(encryptedString);

            /*                 AES encryption            */

            // Get key from encrypter
            byte[] key = encrypter.GetKey();

            // Create encrypter with default algorithm (AES) and use an existing key
            using var encrypterWithKey = new EasyEncrypt(key: key);

            // Create encrypter with default algorithm (AES) and create encryption key from password and salt (with PBKDF2)
            using var encrypterWithPassword = new EasyEncrypt("Password", "Salt12345678");

            // Encrypt and decrypt a byte[]
            var encryptedArray = encrypter.Encrypt(Encoding.UTF8.GetBytes("Example data"));
            var decryptedArray = encrypter.Decrypt(encryptedArray);

            /*                 Custom algorithms encryption            */

            // Create encrypter with DES encryption
            using var DESencrypter = new EasyEncrypt(DES.Create());

            // Create encryptor with TripleDES encryption and create encryption key from password and salt (with PBKDF2)
            using var tripleDeSencrypter = new EasyEncrypt("Password", "Salt12345678", TripleDES.Create());

            /*                Encrypting streams            */

            // Readable input stream, gets disposed when encrypted
            using var inputStream = new MemoryStream(Encoding.UTF8.GetBytes("Example data"));
            // Writable output stream, encrypted data gets written to this stream
            using var encryptedStream = new MemoryStream();
            // Encrypt our stream
            encrypter.EncryptStream(inputStream, encryptedStream);

            // Writable output stream, decrypted data gets written to this stream
            using var decryptedStream = new MemoryStream();
            // Decrypt our stream, encrypted data gets disposed
            encrypter.DecryptStream(encryptedStream, decryptedStream);

            /*            Encrypting files        */
            const string inputFile     = "Data.txt",
                         encryptedFile = "EncryptedData.txt",
                         decryptedFile = "DecryptedData.txt";

            // Encrypt a file, encrypted file gets created with encrypted data
            encrypter.EncryptFile(inputFile, encryptedFile);
            // Decrypt a file, decrypted file gets created with decrypted data
            encrypter.DecryptFile(encryptedFile, decryptedFile);
        }
Example #2
0
        public void EncryptDecryptTest()
        {
            var testData = Enumerable.Range(1, 3)
                           .SelectMany(i => Enumerable.Range(i, 65536))
                           .Select(x => (byte)x)
                           .ToArray();

            var expected = testData.ToArray();

            var password = "******";

            var encStream = new MemoryStream();
            var encRes    = EasyEncrypt.Encrypt(testData.ToStream(), encStream, password);

            Assert.True(encRes);
            var enc = encStream.ToArray();

            var decStream = new MemoryStream();
            var decRes    = EasyEncrypt.Decrypt(enc.ToStream(), decStream, password);

            Assert.True(decRes);
            var dec = decStream.ToArray();

            CollectionAssert.AreEqual(expected, dec);
        }
Example #3
0
        public void TestEncryptionAndDecryption()
        {
            var encryption = new EasyEncrypt();

            var testData  = "testData";
            var encrypted = encryption.Encrypt(testData);
            var decrypted = encryption.Decrypt(encrypted);

            Assert.AreEqual(testData, decrypted);
        }
Example #4
0
        public void TestGenerateKeyConstructor()
        {
            var encryption = new EasyEncrypt("password", "salt12345678");

            var testData  = "testData";
            var encrypted = encryption.Encrypt(testData);
            var decrypted = encryption.Decrypt(encrypted);

            Assert.AreEqual(32, encryption.GetKey().Length);
            Assert.AreEqual(testData, decrypted);
        }
Example #5
0
        public void TestEncryptDecrypt()
        {
            using var encrypter = new EasyEncrypt();

            string data      = "fasfeaw12fewavgffewa4rvar31242`343e12123`";
            string encrypted = encrypter.Encrypt(data);
            string decrypted = encrypter.Decrypt(encrypted);

            Assert.AreEqual(data, decrypted);
            Assert.AreNotEqual(data, encrypted);
        }
        public static string AesEncrypt(this string input)
        {
            try
            {
                var aes    = new EasyEncrypt(Password, Salt, Aes.Create());
                var result = aes.Encrypt(input);
                aes.Dispose();

                return(result);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error AES Encrypt");
                return(null);
            }
        }
Example #7
0
        public static async Task <Result <AggregateException> > Process(Stream input, Stream output, string password)
        {
            var comp = new LzmaCompressor {
                Level = 9
            };

            var pipe = new Pipe(
                (i, o) => comp.Compress(i, o),
                (i, o) => EasyEncrypt.Encrypt(i, o, password)
                );

            pipe.SetInput(input);
            pipe.SetOutput(output);
            pipe.BufferSize = 1024 * 1024;

            return(await pipe.Execute());
        }
Example #8
0
 /// <summary>
 /// Encrypt message with EasyEncrypt
 /// </summary>
 /// <param name="data"></param>
 /// <param name="encryption">instance of easyEncrypt class</param>
 /// <returns>encrypted data</returns>
 public static T Encrypt <T>(this T data, EasyEncrypt encryption) where T : IEasyTcpPacket
 {
     data.Data = encryption.Encrypt(data.Data);
     return(data);
 }