Example #1
0
 public CipherResultText(EncryptionOptions eO, byte[] cipherText)
 {
     if (eO != null)
     {
         DerivationType       = eO.DerivationType;
         Salt                 = ScryptHandler.ByteArrayToString(eO.Salt);
         Cost                 = eO.Cost;
         BlockSize            = eO.BlockSize;
         Parallel             = eO.Parallel;
         KeySizeInBytes       = eO.KeySizeInBytes;
         DerivationIterations = eO.DerivationIterations;
         CipherOutputText     = Convert.ToBase64String(cipherText);
         AesRijndaelIv        = Convert.ToBase64String(eO.RijndaelIv);
     }
 }
        public CipherResultText ConvertToCipherTextObject()
        {
            var cipherTextObject = new CipherResultText()
            {
                AesRijndaelIv        = Convert.ToBase64String(AesRijndaelIv),
                BlockSize            = BlockSize,
                CipherOutputText     = (CipherOutput == null) ? null : Convert.ToBase64String(CipherOutput),
                Cost                 = Cost,
                DerivationIterations = DerivationIterations,
                DerivationType       = DerivationType,
                KeySizeInBytes       = KeySizeInBytes,
                Parallel             = Parallel,
                Salt                 = ScryptHandler.ByteArrayToString(Salt)
            };

            return(cipherTextObject);
        }
        static void Main(string[] args)
        {
            //Encrypt plain text in C# with a random password
            string plainText = "This is my secret text!";
            //You can also use the built in password generator!!
            string passPhrase = PasswordGenerator.GenerateRandomPassword(20);

            passPhrase = "This_is_my_password!";


            // Uses by default "Scrypt" as the Password Derivation method. If you want to change this, you have to set
            // EncryptionOptions (create an object of the class)
            var enc = EncryptionHandler.Encrypt(plainText, passPhrase);

            Console.WriteLine("Encryption / Decryption with key derivation via SCRYPT");
            Console.WriteLine("Plaintext: 'This is my secret text' with password 'This_is_my_password!' results in ciphertext: " + enc);

            var dec3 = EncryptionHandler.Decrypt(enc, passPhrase);

            Console.WriteLine("And decrypting again: " + dec3);

            Console.WriteLine("Encryption / Decryption with key derivation via PBKDF2");
            var eO   = new EncryptionOptions("pbkdf2");
            var enc4 = EncryptionHandler.Encrypt(plainText, passPhrase, eO);

            Console.WriteLine("Plaintext: 'This is my secret text' with password 'This_is_my_password!' results in ciphertext: " + enc4);


            var dec4 = EncryptionHandler.Decrypt(enc4, passPhrase);

            Console.WriteLine("And decrypting again: " + dec4);
            Console.WriteLine("Please start the index.html to see the same in Javascript. Encryption / Decryption run in both ways and can be interchanged between C# and JS!");


            /*
             * Testing binary encryption
             *
             */

            var file = File.ReadAllBytes(@"cartman.png");
            var iv   = Encoding.ASCII.GetBytes("iv_is_16_long___"); // Usually, don't produce your own IVs!
            var salt = Encoding.UTF8.GetBytes("This_is_my_salt");   // Usually, don't produce your own Salt!
            var enc2 = EncryptionHandler.BinaryEncryptWithStaticIv(file, "This_is_my_password!", new EncryptionOptions("scrypt", salt, iv));

            File.WriteAllBytes("cartman.enc", enc2.CipherOutput);
            enc2.CipherOutput = null;
            var enc3 = enc2.ConvertToCipherTextObject();
            var json = JsonConvert.SerializeObject(enc3, Formatting.None);

            File.WriteAllText("cartman-settings.txt", json);

            /*
             * Testing Scrypt
             * The recommended parameters for interactive logins as of 2009 are
             * iterationCount=16384, blockSize=8, threadCount=1, those are the default values.
             * They should be increased as memory latency and CPU parallelism increases.
             */

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            // NOW RUNNING SCRYPT
            string hashString = ScryptHandler.Hash(passPhrase, "This_is_my_SALT!", 16384);

            stopWatch.Stop();

            Console.WriteLine("\r\nTesting Scrypt with the password 'This_is_my_password!': " + hashString);
            bool compare = ScryptHandler.ComparePasswordWithHash("This_is_my_password!", hashString);

            if (compare)
            {
                Console.WriteLine("The password matches with the stored hash!");
            }
            else
            {
                Console.WriteLine("The password does not match with the stored hash!");
            }

            // Get the elapsed time as a TimeSpan value.
            TimeSpan ts = stopWatch.Elapsed;

            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                               ts.Hours, ts.Minutes, ts.Seconds,
                                               ts.Milliseconds / 10);

            Console.WriteLine("Time elapsed in HH:MM:SS (only for creating the hash, not checking): " + elapsedTime);
        }