public string EncryptString(string CadenaOriginal) { MemoryStream memoryStream; try { bool flag = stringKey == null || stringIV == null; if (flag) { throw new Exception("Error al inicializar la clave y el vector"); } byte[] key = MakeKeyByteArray(); byte[] iV = MakeIVByteArray(); byte[] bytes = Encoding.UTF8.GetBytes(CadenaOriginal); memoryStream = new MemoryStream(checked (CadenaOriginal.Length * 2)); ICryptoTransform serviceProvider = new CryptoServiceProvider((CryptoServiceProvider.CryptoProvider)algorithm, CryptoServiceProvider.CryptoAction.Encrypt).GetServiceProvider(key, iV); CryptoStream cryptoStream = new CryptoStream(memoryStream, serviceProvider, CryptoStreamMode.Write); cryptoStream.Write(bytes, 0, bytes.Length); cryptoStream.Close(); } catch (Exception ex) { throw ex; } return(Convert.ToBase64String(memoryStream.ToArray())); }
public string DecryptString(string CadenaCifrada) { MemoryStream memoryStream; try { bool flag = stringKey == null || stringIV == null; if (flag) { throw new Exception("Error al inicializar la clave y el vector."); } byte[] key = MakeKeyByteArray(); byte[] iV = MakeIVByteArray(); byte[] array = Convert.FromBase64String(CadenaCifrada); memoryStream = new MemoryStream(CadenaCifrada.Length); ICryptoTransform serviceProvider = new CryptoServiceProvider((CryptoServiceProvider.CryptoProvider)algorithm, CryptoServiceProvider.CryptoAction.Desencrypt).GetServiceProvider(key, iV); CryptoStream cryptoStream = new CryptoStream(memoryStream, serviceProvider, CryptoStreamMode.Write); cryptoStream.Write(array, 0, array.Length); cryptoStream.Close(); } catch (Exception ex) { throw ex; } return(Encoding.UTF8.GetString(memoryStream.ToArray())); }
public void EncryptDecryptFile(string InFileName, string OutFileName, CryptoAction Action) { bool flag = !File.Exists(InFileName); if (flag) { throw new Exception("No se ha encontrado el archivo."); } checked { try { bool flag2 = stringKey == null || stringIV == null; if (flag2) { throw new Exception("Error al inicializar la clave y el vector."); } FileStream fileStream = new FileStream(InFileName, FileMode.Open, FileAccess.Read); FileStream fileStream2 = new FileStream(OutFileName, FileMode.OpenOrCreate, FileAccess.Write); fileStream2.SetLength(0L); byte[] key = MakeKeyByteArray(); byte[] iV = MakeIVByteArray(); byte[] buffer = new byte[4097]; long length = fileStream.Length; long num = 0L; ICryptoTransform serviceProvider = new CryptoServiceProvider((CryptoServiceProvider.CryptoProvider)algorithm, (CryptoServiceProvider.CryptoAction)Action).GetServiceProvider(key, iV); CryptoStream cryptoStream = null; if (Action != CryptoAction.Encrypt) { if (Action == CryptoAction.Desencrypt) { cryptoStream = new CryptoStream(fileStream2, serviceProvider, CryptoStreamMode.Write); } } else { cryptoStream = new CryptoStream(fileStream2, serviceProvider, CryptoStreamMode.Write); } while (num < length) { int num2 = fileStream.Read(buffer, 0, 4096); cryptoStream.Write(buffer, 0, num2); num += unchecked ((long)num2); } bool flag3 = cryptoStream != null; if (flag3) { cryptoStream.Close(); } fileStream.Close(); fileStream2.Close(); } catch (Exception ex) { throw ex; } } }