Example #1
0
        public byte[] Encrypt(string plainText, out byte[] keyBytes, out byte[] ivBytes)
        {
            if (plainText == null || plainText.Length <= 0)
            {
                throw new ArgumentNullException("plainText");
            }

            System.IO.MemoryStream memoryStream;

            using (System.Security.Cryptography.AesCryptoServiceProvider csp = new System.Security.Cryptography.AesCryptoServiceProvider())
            {
                csp.GenerateKey();
                csp.GenerateIV();

                keyBytes = csp.Key;
                ivBytes  = csp.IV;
                //csp.CreateEncryptor

                // Create an encryptor to perform the stream transform.
                System.Security.Cryptography.ICryptoTransform cryptoTransformer = csp.CreateEncryptor(csp.Key, csp.IV);

                // Create the streams used for encryption.
                using (memoryStream = new System.IO.MemoryStream())
                    using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, cryptoTransformer, System.Security.Cryptography.CryptoStreamMode.Write))
                        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptoStream))
                        {
                            sw.AutoFlush = true;
                            sw.Write(plainText);
                        }
            }

            return(memoryStream.ToArray());
        }
Example #2
0
        byte[] getstandardkey()
        {
            System.Security.Cryptography.AesCryptoServiceProvider helper =
                new System.Security.Cryptography.AesCryptoServiceProvider();
            helper.GenerateKey();
            if (File.Exists("dispenser.bin") == false)
            {
                File.WriteAllBytes("dispenser.bin", helper.Key);
            }
            byte[] keyend = File.ReadAllBytes("dispenser.bin");
            byte[] sentryFile;
            if (File.Exists("sentry.bin"))
            {
                sentryFile = File.ReadAllBytes("sentry.bin");
            }
            else
            {
                byte[] dummysent = new byte[18];
                rnd.NextBytes(dummysent);
                File.WriteAllBytes("sentry.bin", dummysent);
                sentryFile = dummysent;
            }
            byte[] sentryHash = CryptoHelper.SHAHash(sentryFile);
            byte[] fullkey    = new byte[helper.Key.Length];
            int    y          = 0;

            while (y < sentryHash.Length)
            {
                fullkey[y] = sentryHash[y];
                y          = y + 1;
            }
            while (y < fullkey.Length)
            {
                fullkey[y] = keyend[y - 18];
                y          = y + 1;
            }
            return(fullkey);
        }