public static byte[] DecryptData(System.Security.Cryptography.RSACryptoServiceProvider full_rsa, byte[] data)
    {
        System.IO.BinaryReader br = new System.IO.BinaryReader(new System.IO.MemoryStream(data));
        int encryptedkeylength    = br.ReadInt32();
        int aeskeylength          = br.ReadInt32();
        int aesivlength           = br.ReadInt32();

        byte[] encryptedaeskey = br.ReadBytes(encryptedkeylength);
        byte[] encrypteddata   = br.ReadBytes((int)(data.Length - br.BaseStream.Position));
        br.Close();
        byte[] decryptedkey = full_rsa.Decrypt(encryptedaeskey, false);
        br = new System.IO.BinaryReader(new System.IO.MemoryStream(decryptedkey));
        using (System.Security.Cryptography.Aes myAes = System.Security.Cryptography.Aes.Create())
        {
            byte[] aeskey = br.ReadBytes(aeskeylength);
            byte[] aesiv  = br.ReadBytes(aesivlength);
            System.Security.Cryptography.ICryptoTransform decryptor = myAes.CreateDecryptor(aeskey, aesiv);
            using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream())
            {
                using (System.Security.Cryptography.CryptoStream csEncrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, decryptor, System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(csEncrypt))
                    {
                        bw.Write(encrypteddata);
                    }
                    return(msDecrypt.ToArray());
                }
            }
        }
    }
示例#2
0
        } // End Function Encrypt

        public string DeCrypt(string encryptedInput)
        {
            string returnValue = null;

            if (string.IsNullOrEmpty(encryptedInput))
            {
                throw new System.ArgumentNullException("encryptedInput", "encryptedInput may not be string.Empty or NULL, because these are invid values.");
            }

            using (System.Security.Cryptography.Aes objRijndael = System.Security.Cryptography.Aes.Create())
            {
                byte[] baCipherTextBuffer     = HexStringToByteArray(encryptedInput);
                byte[] baDecryptionKey        = HexStringToByteArray(this.m_key);
                byte[] baInitializationVector = HexStringToByteArray(this.m_iv);

                // This is where the message would be transmitted to a recipient
                // who already knows your secret key. Optionally, you can
                // also encrypt your secret key using a public key algorithm
                // and pass it to the mesage recipient along with the RijnDael
                // encrypted message.
                //Get a decryptor that uses the same key and IV as the encryptor.
                using (System.Security.Cryptography.ICryptoTransform transform = objRijndael.CreateDecryptor(baDecryptionKey, baInitializationVector))
                {
                    //Now decrypt the previously encrypted message using the decryptor
                    // obtained in the above step.
                    using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(baCipherTextBuffer))
                    {
                        using (System.Security.Cryptography.CryptoStream csDecrypt =
                                   new System.Security.Cryptography.CryptoStream(
                                       msDecrypt
                                       , transform
                                       , System.Security.Cryptography.CryptoStreamMode.Read)
                               )
                        {
                            byte[] baPlainTextBuffer = new byte[baCipherTextBuffer.Length];

                            //Read the data out of the crypto stream.
                            csDecrypt.Read(baPlainTextBuffer, 0, baPlainTextBuffer.Length);

                            //Convert the byte array back into a string.
                            returnValue = this.m_encoding.GetString(baPlainTextBuffer);
                            System.Array.Clear(baPlainTextBuffer, 0, baPlainTextBuffer.Length);
                            baPlainTextBuffer = null;

                            if (!string.IsNullOrEmpty(returnValue))
                            {
                                returnValue = returnValue.Trim('\0');
                            }

                            csDecrypt.Clear();
                        } // End Using csDecrypt
                    }     // End Using msDecrypt
                }         // End Using transform
            }             // End Using aes

            return(returnValue);
        } // End Function DeCrypt
        /// <summary>
        /// 解密(二进制)
        /// </summary>
        /// <param name="data">需要解密的数据。</param>
        /// <param name="password">密钥</param>
        /// <returns></returns>
        public static byte[] Decrypt(byte[] data, byte[] password)
        {
#if netcore
            System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create();
#else
            System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create("AES");
#endif
            aes.Mode = System.Security.Cryptography.CipherMode.ECB;
            aes.Key  = password;
            System.Security.Cryptography.ICryptoTransform encryptor = aes.CreateDecryptor();
            return(encryptor.TransformFinalBlock(data, 0, data.Length));
        }
示例#4
0
 internal static byte[] AES256Decrypt(byte[] block, byte[] key)
 {
     using (System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create())
     {
         aes.Key     = key;
         aes.Mode    = System.Security.Cryptography.CipherMode.ECB;
         aes.Padding = System.Security.Cryptography.PaddingMode.None;
         using (System.Security.Cryptography.ICryptoTransform decryptor = aes.CreateDecryptor())
         {
             return(decryptor.TransformFinalBlock(block, 0, block.Length));
         }
     }
 }
示例#5
0
        } // End Function Encrypt

        public static string DeCrypt(string strEncryptedInput)
        {
            string strReturnValue = null;

            if (string.IsNullOrEmpty(strEncryptedInput))
            {
                throw new System.ArgumentNullException("strEncryptedInput", "strEncryptedInput may not be string.Empty or NULL, because these are invid values.");
            }

            // System.Text.Encoding enc = System.Text.Encoding.ASCII;
            System.Text.Encoding enc = System.Text.Encoding.UTF8;

            using (System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create())
            {
                byte[] baCipherTextBuffer = HexStringToByteArray(strEncryptedInput);


                byte[] baDecryptionKey        = HexStringToByteArray(strKey);
                byte[] baInitializationVector = HexStringToByteArray(strIV);

                // This is where the message would be transmitted to a recipient
                // who already knows your secret key. Optionally, you can
                // also encrypt your secret key using a public key algorithm
                // and pass it to the mesage recipient along with the RijnDael
                // encrypted message.
                // Get a decryptor that uses the same key and IV as the encryptor.
                System.Security.Cryptography.ICryptoTransform ifaceAESdecryptor = aes.CreateDecryptor(baDecryptionKey, baInitializationVector);

                // Now decrypt the previously encrypted message using the decryptor
                // obtained in the above step.
                System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(baCipherTextBuffer);
                System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, ifaceAESdecryptor, System.Security.Cryptography.CryptoStreamMode.Read);

                // byte[] baPlainTextBuffer
                // byte[] baPlainTextBuffer = new byte[baCipherTextBuffer.Length;
                byte[] baPlainTextBuffer = new byte[baCipherTextBuffer.Length + 1];

                //Read the data out of the crypto stream.
                csDecrypt.Read(baPlainTextBuffer, 0, baPlainTextBuffer.Length);

                //Convert the byte array back into a string.
                strReturnValue = enc.GetString(baPlainTextBuffer);
                if (!string.IsNullOrEmpty(strReturnValue))
                {
                    strReturnValue = strReturnValue.Trim('\0');
                }
            }
            return(strReturnValue);
        } // End Function DeCrypt
        public static string Decrypt(string cipherString)
        {
            byte[] cipherText = Convert.FromBase64String(cipherString);
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
            {
                throw new ArgumentNullException("cipherText");
            }
            if (aesProperties.Key == null || aesProperties.Key.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }
            if (aesProperties.InitializationVector == null || aesProperties.InitializationVector.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an Aes object
            // with the specified key and IV.
            using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create())
            {
                aesAlg.Key = aesProperties.Key;
                aesAlg.IV  = aesProperties.InitializationVector;

                // Create a decrytor to perform the stream transform.
                System.Security.Cryptography.ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, decryptor, System.Security.Cryptography.CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return(plaintext);
        }
示例#7
0
        //복호화
        public static byte[] Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
        {
            byte[] plainText;
            // Create an Aes object
            // with the specified key and IV.
            using (System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create())
            {
                aesAlg.BlockSize = 128;
                aesAlg.Key       = Key;
                aesAlg.IV        = IV;
                aesAlg.Mode      = System.Security.Cryptography.CipherMode.CBC;
                aesAlg.Padding   = System.Security.Cryptography.PaddingMode.PKCS7;

                System.Security.Cryptography.ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                plainText = decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length);
            }

            return(plainText);
        }
示例#8
0
 public byte[] Decrypt(byte[] inBytes)
 {
     return(Crypt(inBytes, aes.CreateDecryptor()));
 }
示例#9
0
        public static string Decrypt(string cipherText, string encryptionKey)
        {
            try
            {
                if (cipherText == null)
                {
                    return(string.Empty);
                }

                byte[] cipherBytes = HttpServerUtility.UrlTokenDecode(cipherText.ToString());

                using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create())
                {
                    System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });

                    encryptor.Key = pdb.GetBytes(32);

                    encryptor.IV = pdb.GetBytes(16);

                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
                        {
                            cs.Write(cipherBytes, 0, cipherBytes.Length);
                            cs.Close();
                        }
                        cipherText = System.Text.Encoding.Unicode.GetString(ms.ToArray());
                    }
                }
            }
            catch
            {
                cipherText = string.Empty;
            }
            return(cipherText.ToString());
        }
示例#10
0
        /// <summary>
        /// Reads and decrypts the dataset from the given file
        /// </summary>
        /// <param name="dataset">Dataset to read</param>
        /// <param name="username">Username for decryption</param>
        /// <param name="password">Password for decryption</param>
        /// <param name="fileName">File name to read</param>
        /// <param name="compressed">Is the file compressed</param>
        /// <returns>XmlReadMode used for reading</returns>
        internal static System.Data.XmlReadMode DecryptDataSet(System.Data.DataSet dataset, string username, string password, string fileName, bool compressed)
        {
            System.Data.XmlReadMode xmlReadMode;

            // Check the parameters
            if (dataset == null ||
                string.IsNullOrEmpty(username) ||
                string.IsNullOrEmpty(password) ||
                string.IsNullOrEmpty(fileName))
            {
                throw new System.ArgumentNullException("All arguments must be supplied.");
            }

            // Read the dataset and encrypt it
            using (System.Security.Cryptography.Aes aes = Cryptography.InitAes(username, password)) {
                using (System.IO.FileStream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open)) {
                    using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(fileStream, aes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read)) {
                        if (compressed)
                        {
                            // when decompression is requested, use GZip
                            using (System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(cryptoStream, System.IO.Compression.CompressionMode.Decompress)) {
                                xmlReadMode = dataset.ReadXml(zipStream, System.Data.XmlReadMode.ReadSchema);
                            }
                        }
                        else
                        {
                            xmlReadMode = dataset.ReadXml(cryptoStream, System.Data.XmlReadMode.ReadSchema);
                        }
                    }
                }
            }

            return(xmlReadMode);
        }
示例#11
0
文件: utilData.cs 项目: Puiatmp/b2e
 public static string Decrypt2(string cipherText, string encryptionKey)
 {
     byte[] cipherBytes = Convert.FromBase64String(cipherText);
     using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create())
     {
         System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6E, 0x20, 0x4D, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
         encryptor.Key = pdb.GetBytes(32);
         encryptor.IV  = pdb.GetBytes(16);
         using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
         {
             using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
             {
                 cs.Write(cipherBytes, 0, cipherBytes.Length);
                 cs.Close();
             }
             cipherText = System.Text.Encoding.Unicode.GetString(ms.ToArray());
         }
     }
     return(cipherText);
 }
示例#12
0
        } // End Function Encrypt

        public static string Decrypt(string encryptedInput)
        {
            string returnValue = null;

            if (string.IsNullOrEmpty(encryptedInput))
            {
                return(encryptedInput);
            }

            // System.Text.Encoding enc = System.Text.Encoding.ASCII;
            System.Text.Encoding enc = System.Text.Encoding.UTF8;

            byte[] cipherTextBuffer     = HexStringToByteArray(encryptedInput);
            byte[] decryptionKey        = HexStringToByteArray(s_key);
            byte[] initializationVector = HexStringToByteArray(s_IV);

            using (System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create())
            {
                // This is where the message would be transmitted to a recipient
                // who already knows your secret key. Optionally, you can
                // also encrypt your secret key using a public key algorithm
                // and pass it to the mesage recipient along with the RijnDael
                // encrypted message.
                //Get a decryptor that uses the same key and IV as the encryptor.
                using (System.Security.Cryptography.ICryptoTransform aesDecryptor = aes.CreateDecryptor(decryptionKey, initializationVector))
                {
                    //Now decrypt the previously encrypted message using the decryptor
                    // obtained in the above step.
                    using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(cipherTextBuffer))
                    {
                        using (System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, aesDecryptor, System.Security.Cryptography.CryptoStreamMode.Read))
                        {
                            //Dim baPlainTextBuffer() As Byte
                            //baPlainTextBuffer = New Byte(baCipherTextBuffer.Length) {}
                            byte[] baPlainTextBuffer = new byte[cipherTextBuffer.Length + 1];

                            //Read the data out of the crypto stream.
                            csDecrypt.Read(baPlainTextBuffer, 0, baPlainTextBuffer.Length);

                            //Convert the byte array back into a string.
                            returnValue = enc.GetString(baPlainTextBuffer);
                            if (!string.IsNullOrEmpty(returnValue))
                            {
                                returnValue = returnValue.Trim('\0');
                            }
                        } // End Using csDecrypt
                    }     // End Using msDecrypt
                }         // End Using aesDecryptor
            }             // End Using aes

            System.Array.Clear(cipherTextBuffer, 0, cipherTextBuffer.Length);
            cipherTextBuffer = null;

            System.Array.Clear(decryptionKey, 0, decryptionKey.Length);
            decryptionKey = null;

            System.Array.Clear(initializationVector, 0, initializationVector.Length);
            initializationVector = null;

            return(returnValue);
        } // End Function DeCrypt