Esempio n. 1
0
        public string CifrarCadena(string CadenaOriginal)
        {
            MemoryStream memStream = new MemoryStream();

            try
            {
                if (_key != null && _IV != null)
                {
                    byte[] bytKey = MakeKeyByteArray();
                    byte[] bytIV  = MakeIVByteArray();

                    byte[] textoPlano = Encoding.UTF8.GetBytes(CadenaOriginal);

                    memStream = new MemoryStream(CadenaOriginal.Length * 2);
                    CryptoServiceProvider _cryptoProvider = new CryptoServiceProvider((CryptoServiceProvider.EnumCryptoProvider)_algorithm, CryptoServiceProvider.EnumCryptoAction.Encrypt);
                    ICryptoTransform      transform       = _cryptoProvider.GetServiceProvider(bytKey, bytIV);
                    CryptoStream          cryptoStream    = new CryptoStream(memStream, transform, CryptoStreamMode.Write);
                    cryptoStream.Write(textoPlano, 0, textoPlano.Length);

                    cryptoStream.Close();
                }
                else
                {
                    throw new Exception("Error al inicializar la clave y el vector");
                }
            }
            catch
            {
                throw;
            }

            return(Convert.ToBase64String(memStream.ToArray()));
        }
Esempio n. 2
0
        public string DescifrarCadena(string CadenaCifrada)
        {
            MemoryStream memStream = null;

            try
            {
                if (_key != null && _IV != null)
                {
                    byte[] _bytkey          = MakeKeyByteArray();
                    byte[] _bytIV           = MakeIVByteArray();
                    byte[] _byttextocifrado = Convert.FromBase64String(CadenaCifrada);

                    memStream = new MemoryStream(CadenaCifrada.Length);
                    CryptoServiceProvider _cryptoProvider = new CryptoServiceProvider((CryptoServiceProvider.EnumCryptoProvider)_algorithm, CryptoServiceProvider.EnumCryptoAction.Desencrypt);
                    ICryptoTransform      transform       = _cryptoProvider.GetServiceProvider(_bytkey, _bytIV);
                    CryptoStream          _cryptoStream   = new CryptoStream(memStream, transform, CryptoStreamMode.Write);
                    _cryptoStream.Write(_byttextocifrado, 0, _byttextocifrado.Length);
                    _cryptoStream.Close();
                }
                else
                {
                    throw new Exception("Error al inicializar la clave y el vector.");
                }
            }
            catch
            {
                throw;
            }
            return(Encoding.UTF8.GetString(memStream.ToArray()));
        }
Esempio n. 3
0
        public void CifrarDescifrarArchivo(string InFileName, string OutFileName, EnumCryptoAction EnumAction)
        {
            if (!File.Exists(InFileName))
            {
                throw new Exception("No se ha encontrado el archivo.");
            }
            try
            {
                if (_key != null && _IV != null)
                {
                    FileStream fsIn  = new FileStream(InFileName, FileMode.Open, FileAccess.Read);
                    FileStream fsOut = new FileStream(OutFileName, FileMode.OpenOrCreate, FileAccess.Write);
                    fsOut.SetLength(0);

                    byte[] _bytKey       = MakeKeyByteArray();
                    byte[] _bytIV        = MakeIVByteArray();
                    byte[] _bytebuffer   = new byte[4096];
                    long   fileLength    = fsIn.Length;
                    long   bytesProcesed = 0;
                    int    bytesInBlock  = 0;


                    CryptoServiceProvider _cryptoProvider = new CryptoServiceProvider((CryptoServiceProvider.EnumCryptoProvider)_algorithm, (CryptoServiceProvider.EnumCryptoAction)EnumAction);

                    ICryptoTransform _transform    = _cryptoProvider.GetServiceProvider(_bytKey, _bytIV);
                    CryptoStream     _cryptoStream = null;
                    switch (EnumAction)
                    {
                    case EnumCryptoAction.Encrypt:
                        _cryptoStream = new CryptoStream(fsOut, _transform, CryptoStreamMode.Write);
                        break;

                    case EnumCryptoAction.Desencrypt:
                        _cryptoStream = new CryptoStream(fsOut, _transform, CryptoStreamMode.Write);
                        break;
                    }

                    while (bytesProcesed < fileLength)
                    {
                        bytesInBlock = fsIn.Read(_bytebuffer, 0, 4096);
                        _cryptoStream.Write(_bytebuffer, 0, bytesInBlock);
                        bytesProcesed += Convert.ToInt64(bytesInBlock);
                    }

                    if (_cryptoStream != null)
                    {
                        _cryptoStream.Close();
                    }
                    fsIn.Close();
                    fsOut.Close();
                }
                else
                {
                    throw new Exception("Error al inicializar la clave y el vector.");
                }
            }
            catch
            {
                throw;
            }
        }