Beispiel #1
0
        public void Decrypt(Stream inStream, Stream outStream)
        {
            ThrowIfObjectDisposed();

            AssertUtil.ArgumentNotNull(inStream, nameof(inStream));
            AssertUtil.ArgumentNotNull(outStream, nameof(outStream));

            if (!inStream.CanRead)
            {
                throw new ArgumentException($"Argument '{nameof(inStream)}' cannot be read.");
            }

            if (!outStream.CanWrite)
            {
                throw new ArgumentException($"Argument '{nameof(outStream)}' not supports writing.");
            }


            using (var decryptor = this.provider.CreateDecryptor())
                using (var reader = new System.Security.Cryptography.CryptoStream(inStream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read, true))
                {
                    reader.CopyTo(outStream);
                    outStream.Flush();
                }
        }
Beispiel #2
0
        public byte[] Decrypt(byte[] bytes, int index, int count)
        {
            ThrowIfObjectDisposed();

            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(index), $"Argument '{nameof(index)}' value must be >= 0.");
            }

            if (index >= bytes.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index), $"Argument '{nameof(index)}' value exceeds the maximum length of argument '{nameof(bytes)}'.");
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), $"Argument '{nameof(count)}' value must be >= 0.");
            }

            if (index + count > bytes.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count), $"Argument '{nameof(index)} + {nameof(count)}' value exceeds the maximum length of argument '{nameof(bytes)}'.");
            }


            #region 注释的也是一种方法,效果一样

            //using (var decryptor = this.provider.CreateDecryptor())
            //{
            //    return decryptor.TransformFinalBlock(bytes, index, count);
            //}

            #endregion

            using (var outStream = new MemoryStream())
                using (var inStream = new MemoryStream(bytes, index, count))
                    using (var decryptor = this.provider.CreateDecryptor())
                        using (var reader = new System.Security.Cryptography.CryptoStream(inStream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read))
                        {
                            reader.CopyTo(outStream);

                            return(outStream.ToArray());
                        }
        }
 /// <summary>
 /// 具体解密字符串
 /// </summary>
 /// <param name="encryptedValue"></param>
 /// <param name="key"></param>
 /// <param name="iv"></param>
 /// <returns></returns>
 public string DESDecryptString(string encryptedValue, string key, string iv)
 {
     encryptedValue = encryptedValue.Replace("%2B", "+");
     if (encryptedValue.Length < 0x10)
     {
         return("");
     }
     encryptedValue = string.Join(string.Empty
                                  , new string(new[]
     {
         encryptedValue[0], encryptedValue[2], encryptedValue[4], encryptedValue[6], encryptedValue[8],
         encryptedValue[10], encryptedValue[12], encryptedValue[14]
     }),
                                  encryptedValue.Substring(16, encryptedValue.Length - 16));
     key = key + "20444608";
     iv  = iv + "60442215";
     key = key.Substring(0, 8);
     iv  = iv.Substring(0, 8);
     try
     {
         var transform = new System.Security.Cryptography.DESCryptoServiceProvider
         {
             Key = Encoding.UTF8.GetBytes(key),
             IV  = Encoding.UTF8.GetBytes(iv)
         }.CreateDecryptor();
         var buffer       = Convert.FromBase64String(encryptedValue);
         var destination  = new MemoryStream();
         var stream       = new MemoryStream(buffer);
         var cryptoStream = new System.Security.Cryptography.CryptoStream(stream, transform, System.Security.Cryptography.CryptoStreamMode.Read);
         cryptoStream.CopyTo(destination);
         cryptoStream.Close();
         var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
         destination.Position = 0L;
         return((string)formatter.Deserialize(destination));
     }
     catch (Exception)
     {
         return(null);
     }
 }
Beispiel #4
0
        /// <summary>Deszyfruje przekazany strumień danych.</summary>
        /// <param name="cipherStream">Zaszyfrowany strumień danych.</param>
        /// <param name="decryptedStream">Odszyfrowany strumień bajtów.</param>
        /// <param name="key">Klucz deszyfrujący. Maksymalnie 256 bitów, minimalnie 128 bitów, przeskok o 64 bity.</param>
        /// <param name="iv">Wektor inicjalizacji. Rozmiar 128 bitów.</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException"></exception>
        /// <exception cref="System.InvalidOperationException"></exception>
        /// <exception cref="System.Security.Cryptography.CryptographicException"></exception>
        public static void Decrypt(System.IO.Stream cipherStream, System.IO.Stream decryptedStream, byte[] key, byte[] iv)
        {
            if (cipherStream == null)
            {
                throw new System.ArgumentNullException("cipherStream");
            }
            if (cipherStream.CanRead == false)
            {
                throw new System.ArgumentException("Argument 'cipherStream' is not readable.");
            }

            if (decryptedStream == null)
            {
                throw new System.ArgumentNullException("decryptedStream");
            }
            if (decryptedStream.CanWrite == false)
            {
                throw new System.ArgumentException("Argument 'decryptedStream' is not writable.");
            }

            if (key == null)
            {
                throw new System.ArgumentNullException("key");
            }
            if (key.Length != 16 && key.Length != 22 && key.Length != 32)
            {
                throw new System.ArgumentException("Argument 'key' does not mach the block size for this algorithm.");
            }

            if (iv == null)
            {
                throw new System.ArgumentNullException("iv");
            }
            if (iv.Length != 16)
            {
                throw new System.ArgumentException("Argument 'iv' does not mach the block size for this algorithm.");
            }

            cipherStream.Position    = 0;
            decryptedStream.Position = 0;

            System.Security.Cryptography.ICryptoTransform decryptor = null;

            try
            {
                using (System.Security.Cryptography.RijndaelManaged algoritmCrypt = new System.Security.Cryptography.RijndaelManaged())
                {
                    decryptor = algoritmCrypt.CreateDecryptor(key, iv);
                    using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(
                               cipherStream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read))
                    {
                        cs.CopyTo(decryptedStream);
                        decryptedStream.Position = 0;
                    }
                }
            }
            finally
            {
                if (decryptor != null)
                {
                    decryptor.Dispose();
                }
            }
        }