Пример #1
0
            public string Decrypt(string str)
            {
                byte[]       bytes = Convert.FromBase64String(str);
                MemoryStream ms    = new MemoryStream();
                CryptoStream cs    = new CryptoStream(ms, rijndael.CreateDecryptor(), CryptoStreamMode.Write);

                cs.Write(bytes, 0, bytes.Length);
                cs.Close();
                bytes = ms.ToArray();
                return(Encoding.Unicode.GetString(bytes));
            }
Пример #2
0
 public byte[] Decrypt(byte[] bytes)
 {
     using (MemoryStream ms = new MemoryStream())
         using (CryptoStream cs = new CryptoStream(ms, rijndael.CreateDecryptor(), CryptoStreamMode.Write))
         {
             cs.Write(bytes, 0, bytes.Length);
             //cs.FlushFinalBlock();
             cs.Close();
             return(ms.ToArray());
         }
 }
Пример #3
0
        /// <summary>
        /// Constructs a new instance of <see cref="AesCrypto"/> from the specified values.
        /// </summary>
        /// <param name="salt">The <see cref="Salt"/> used in encryption.</param>
        /// <param name="passphrase">The <see cref="Passphrase"/> used in encryption.</param>
        public AesCrypto(string passphrase, byte[] salt)
        {
            Passphrase = passphrase;
            Salt = salt;

            _encryptor = Rijndael.Create();
            _encryptor.Padding = PaddingMode.PKCS7;
            _pdb = new Rfc2898DeriveBytes(Passphrase, Salt);
            _encryptor.Key = _pdb.GetBytes(_encryptor.KeySize / 8);
            _encryptor.IV = _pdb.GetBytes(_encryptor.BlockSize / 8);

            _encryptorTransform = _encryptor.CreateEncryptor();
            _decryptorTransform = _encryptor.CreateDecryptor();
        }
Пример #4
0
 public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
 {
     System.IO.MemoryStream ms = new System.IO.MemoryStream();
     System.Security.Cryptography.Rijndael alg = System.Security.Cryptography.Rijndael.Create();
     alg.Key = Key;
     alg.IV  = IV;
     System.Security.Cryptography.CryptoStream cs =
         new System.Security.Cryptography.CryptoStream(ms, alg.CreateDecryptor(),
                                                       System.Security.Cryptography.CryptoStreamMode.Write);
     cs.Write(cipherData, 0, cipherData.Length);
     cs.Close();
     byte[] decryptedData = ms.ToArray();
     return(decryptedData);
 }
Пример #5
0
        /// <summary>
        /// Decode a string endoced using Rijndael with specified password and IV strings.
        /// </summary>
        /// <param name="sourceString">The string to decode</param>
        /// <param name="password">The password string</param>
        /// <param name="IV">The IV string</param>
        /// <returns>The decoded string</returns>
        public string DecodeString(string sourceString, string password, String IV)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException("Please specify the password", nameof(password));
            }

            if (string.IsNullOrEmpty(IV))
            {
                throw new ArgumentException("Please specify the Initialize Vector", nameof(IV));
            }

            if (!string.IsNullOrEmpty(sourceString))
            {
                byte[] cipherText = Convert.FromBase64String(sourceString);

                string plaintext = null;

                // Create an Rijndael object
                // with the specified key and IV.
                using (System.Security.Cryptography.Rijndael rijAlg = System.Security.Cryptography.Rijndael.Create())
                {
                    rijAlg.Key = GeneratePassword(password);
                    rijAlg.IV  = GeneratePassword(IV);

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

                    // Create the streams used for decryption.
                    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, 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);
            }
            else
            {
                return(string.Empty);
            }
        }
Пример #6
0
    /// <summary>
    /// Desencripta un objecto
    /// </summary>
    /// <param name="pObject">Objecto Encriptado</param>
    /// <param name="pKey">Clave 1</param>
    /// <param name="pIV">Clave 2</param>
    /// <returns>Retorna el Objecto Desencriptado</returns>
    private static object DecryptObjectFromBytes(byte[] pObject, byte[] pKey, byte[] pIV)
    {
        //Verifica si los parametros son nulo
        if (pObject == null || pObject.Length <= 0)
        {
            throw new ArgumentNullException();
        }
        if (pKey == null || pKey.Length <= 0)
        {
            throw new ArgumentNullException();
        }
        if (pIV == null || pIV.Length <= 0)
        {
            throw new ArgumentNullException();
        }

        //Variable que almacenara el valor del objecto encritado
        object vlDecodeObject = null;

        //Creamos el algoritmo
        System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create();

        //Usamos el algoritmo
        using (rijndael)
        {
            //Le pasamos los valores de las claves al algoritmno
            rijndael.Key = pKey;
            rijndael.IV  = pIV;

            //Creamos el Desencriptador
            System.Security.Cryptography.ICryptoTransform decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV);

            //Se crean las corrientes para el cifrado
            using (MemoryStream msDecrypt = new MemoryStream(pObject))
            {
                using (System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, decryptor, System.Security.Cryptography.CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        //Escribe los datos en la secuencia, como un objecto (string)
                        vlDecodeObject = srDecrypt.ReadToEnd();
                    }
                }
            }
        }

        //Retornamos el objecto desencriptado
        return(vlDecodeObject);
    }
Пример #7
0
        public static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
            {
                throw new ArgumentNullException("cipherText");
            }
            if (Key == null || Key.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }
            if (IV == null || IV.Length <= 0)
            {
                throw new ArgumentNullException("IV");
            }

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

            // Create an Rijndael object
            // with the specified key and IV.
            using (System.Security.Cryptography.Rijndael rijAlg = System.Security.Cryptography.Rijndael.Create())
            {
                rijAlg.Key = Key;
                rijAlg.IV  = IV;

                // Create a decryptor to perform the stream transform.
                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, 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);
        }
Пример #8
0
        public string Decode(string input)
        {
            System.Security.Cryptography.Rijndael alg = System.Security.Cryptography.Rijndael.Create();
            alg.Key     = Encoding.UTF8.GetBytes("kaudkwudhtbenwnakaudkwudhtbenwna");
            alg.IV      = Encoding.UTF8.GetBytes("HR$2pIjHR$2pIj12");
            alg.Padding = PaddingMode.Zeros;
            string           result;
            ICryptoTransform decryptor = alg.CreateDecryptor(alg.Key, alg.IV);

            using (MemoryStream msDecrypt = new MemoryStream(Encoding.Unicode.GetBytes(input)))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        result = srDecrypt.ReadToEnd();
                    }
                }
            }
            return(result);
        }
Пример #9
0
        public bool Decrypt(ref string value)
        {
            string result = string.Empty;

            System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create();
            ICryptoTransform decryptor = rijndael.CreateDecryptor(key, iv);

            using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(value))) {
                try {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) {
                        StreamReader reader = new StreamReader(cs);
                        result = reader.ReadLine();
                        reader.Close();
                    }
                } catch (System.Security.Cryptography.CryptographicException ex) {
                    value = ex.Message;
                    return(false);
                }
            }
            value = result;
            return(true);
        }
Пример #10
0
 private static byte[] AES_Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
 {
     System.IO.MemoryStream memoryStream            = new System.IO.MemoryStream();
     System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create();
     rijndael.Key = Key;
     rijndael.IV  = IV;
     System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, rijndael.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
     cryptoStream.Write(cipherData, 0, cipherData.Length);
     cryptoStream.Close();
     return(memoryStream.ToArray());
 }
Пример #11
0
    internal static void Decrypt(string fileIn, string fileOut)
    {
        System.IO.FileStream fsIn  = new System.IO.FileStream(fileIn, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        System.IO.FileStream fsOut = new System.IO.FileStream(fileOut, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);

        System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(Password,
                                                                                                                    new byte[] { 0x49, 0x49, 0x35, 0x6e, 0x76, 0x4d,
                                                                                                                                 0x65, 0x64, 0x76, 0x76, 0x64, 0x65, 0x76 });
        System.Security.Cryptography.Rijndael alg = System.Security.Cryptography.Rijndael.Create();
        alg.Key = pdb.GetBytes(32);
        alg.IV  = pdb.GetBytes(16);

        System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(fsOut, alg.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

        int bufferLen = 4096;

        byte[] buffer = new byte[bufferLen];
        int    bytesRead;

        do
        {
            // read a chunk of data from the input file
            bytesRead = fsIn.Read(buffer, 0, bufferLen);
            // Decrypt it
            cs.Write(buffer, 0, bytesRead);
        } while (bytesRead != 0);
        cs.Close();
        fsIn.Close();
    }
Пример #12
0
        private static void SetupEncryption()
        {
            rijn = Rijndael.Create();
            rijn.KeySize = ENC_Key.Length * 8;
            rijn.Key = ENC_Key;
            rijn.IV = ENC_IV;
            rijn.Padding = PaddingMode.ISO10126;
            rijn.Mode = CipherMode.CBC;

            encryptor = rijn.CreateEncryptor();
            decryptor = rijn.CreateDecryptor();
        }
Пример #13
0
            public static string Decrypt(string strDecrypt, bool retStrOriginal)
            {
                string result = string.Empty;

                System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create();
                try
                {
                    byte[] bytes  = System.Text.Encoding.UTF8.GetBytes(Secret.AES.Key);
                    byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(Secret.AES.IV);
                    byte[] array  = Convert.FromBase64String(strDecrypt);
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, rijndael.CreateDecryptor(bytes, bytes2), System.Security.Cryptography.CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(array, 0, array.Length);
                            cryptoStream.FlushFinalBlock();
                            result = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
                        }
                    }
                }
                catch
                {
                    if (retStrOriginal)
                    {
                        result = strDecrypt;
                    }
                    else
                    {
                        result = string.Empty;
                    }
                }
                rijndael.Clear();
                return(result);
            }
Пример #14
0
        public static BinaryReader Decrypt(String file, Rijndael crypt)
        {
            ICryptoTransform transform = crypt.CreateDecryptor();
            FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
            CryptoStream cs = new CryptoStream(fs, transform, CryptoStreamMode.Read);

            return new BinaryReader(cs);
        }
Пример #15
0
 public void setUpRijndael(byte[] key, byte[] iv)
 {
     _rijndael = new RijndaelManaged();
     _rijndael.Key = key;
     _rijndael.IV = iv;
     _rijEncryptor = _rijndael.CreateEncryptor(_rijndael.Key, _rijndael.IV);
     _rijDecryptor = _rijndael.CreateDecryptor(_rijndael.Key, _rijndael.IV);
 }