Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 6
0
        public static async Task <Result <AggregateException> > Deprocess(Stream input, Stream output, string password)
        {
            var comp = new LzmaCompressor();

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

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

            return(await pipe.Execute());
        }
Ejemplo n.º 7
0
        public static string AesDecrypt(this string encryptedInput)
        {
            try
            {
                var aes    = new EasyEncrypt(Password, Salt, Aes.Create());
                var result = aes.Decrypt(encryptedInput);
                aes.Dispose();

                return(result);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error AES Decrypt");
                return(null);
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Decrypt message with EasyEncrypt
 /// </summary>
 /// <param name="data"></param>
 /// <param name="encryption">instance of easyEncrypt class</param>
 /// <returns>decrypted data</returns>
 public static T Decrypt <T>(this T data, EasyEncrypt encryption) where T : IEasyTcpPacket
 {
     data.Data = encryption.Decrypt(data.Data);
     return(data);
 }