Exemple #1
0
        /// <summary>
        /// Decrypts cipherText value with IV and text and returns decrypted string
        /// </summary>
        public string Decrypt(byte[] cipherText, byte[] iv, long ticks)
        {
            try
            {
                string plaintext = null;
                // Create AesManaged
                if (singleKeyManager)
                {
                    throw new InvalidOperationException("This instance of EncryptionManager was not expecting parameter of ticks. Use constructor with a, b and c instead.");
                }
                if (AESManager == null)
                {
                    AESManager = new AesManaged();
                }
                Random random;
                if (ticks % 2 == 0)
                {
                    random = new Random(BaseEvenIntKey + (int)(ticks % 1000000000));
                }
                else
                {
                    random = new Random(BaseOddIntKey + (int)(ticks % 1000000000));
                }
                byte[] bytes = new byte[32];
                random.NextBytes(bytes);
                AESManager.Key = bytes;

                // Create a decryptor
                ICryptoTransform decryptor = AESManager.CreateDecryptor(AESManager.Key, iv);
                // Create the streams used for decryption.
                using (MemoryStream ms = new MemoryStream(cipherText))
                {
                    // Create crypto stream
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        // Read crypto stream
                        using (StreamReader reader = new StreamReader(cs))
                        {
                            plaintext = reader.ReadToEnd();
                        }
                    }
                }

                return(plaintext);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw ex;
            }
        }
Exemple #2
0
        /// <summary>
        /// Decrypts cipherText value with IV and returns decrypted string
        /// </summary>
        public string Decrypt(byte[] cipherText, byte[] iv)
        {
            try
            {
                string plaintext = null;
                // Create AesManaged
                if (!singleKeyManager)
                {
                    throw new InvalidOperationException("This instance of EncryptionManager was expecting parameter of ticks.");
                }
                if (AESManager == null)
                {
                    AESManager = new AesManaged();
                }
                // Create a decryptor
                ICryptoTransform decryptor = AESManager.CreateDecryptor(AESManager.Key, iv);
                // Create the streams used for decryption.
                using (MemoryStream ms = new MemoryStream(cipherText))
                {
                    // Create crypto stream
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        // Read crypto stream
                        using (StreamReader reader = new StreamReader(cs))
                        {
                            plaintext = reader.ReadToEnd();
                        }
                    }
                }

                return(plaintext);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw ex;
            }
        }