public bool fileOperation(string inFileName, string outFileName, cryptoAction action, ref string _returnErrorCode) { if (!File.Exists(inFileName)) { return(false); } try { if (((_key != null) && (_IV != null))) { var fsIn = new FileStream(inFileName, FileMode.Open, FileAccess.Read); var fsOut = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write); fsOut.SetLength(0); byte[] key = makeKeyByteArray(); byte[] IV = makeIVByteArray(); byte[] byteBuffer = null; long fileLength = fsIn.Length; long bytesProcesed = 0; int bytesInBlock = 0; ICryptoTransform transform = getServiceProvider(action, key, IV); CryptoStream cryptoStream = null; switch (action) { case cryptoAction.Encrypt: cryptoStream = new CryptoStream(fsOut, transform, CryptoStreamMode.Write); break; case cryptoAction.Decrypt: cryptoStream = new CryptoStream(fsOut, transform, CryptoStreamMode.Write); break; } while ((bytesProcesed < fileLength)) { bytesInBlock = fsIn.Read(byteBuffer, 0, 4096); cryptoStream.Write(byteBuffer, 0, bytesInBlock); bytesProcesed = (bytesProcesed + long.Parse(bytesInBlock.ToString())); } if (!(cryptoStream == null)) { cryptoStream.Close(); } fsIn.Close(); fsOut.Close(); _returnErrorCode = ""; return(true); } else { return(false); } } catch (Exception ex) { ISException.RegisterExcepcion(ex); return(false); } }
internal ICryptoTransform getServiceProvider(cryptoAction cAction, byte[] key, byte[] IV) { ICryptoTransform transform = null; switch (algorithm) { case cryptoProvider.DES: var des = new DESCryptoServiceProvider(); switch (cAction) { case cryptoAction.Encrypt: // si estamos cifrando, creamos el objeto cifrador. transform = des.CreateEncryptor(key, IV); break; case cryptoAction.Decrypt: // si estamos descifrando, creamos el objeto descifrador. transform = des.CreateDecryptor(key, IV); break; } return transform; case cryptoProvider.TripleDES: var des3 = new TripleDESCryptoServiceProvider(); switch (cAction) { case cryptoAction.Encrypt: transform = des3.CreateEncryptor(key, IV); break; case cryptoAction.Decrypt: transform = des3.CreateDecryptor(key, IV); break; } return transform; case cryptoProvider.RC2: var rc2 = new RC2CryptoServiceProvider(); switch (cAction) { case cryptoAction.Encrypt: transform = rc2.CreateEncryptor(key, IV); break; case cryptoAction.Decrypt: transform = rc2.CreateDecryptor(key, IV); break; } return transform; case cryptoProvider.Rijndael: var rijndael = new RijndaelManaged(); switch (cAction) { case cryptoAction.Encrypt: transform = rijndael.CreateEncryptor(key, IV); break; case cryptoAction.Decrypt: transform = rijndael.CreateDecryptor(key, IV); break; } return transform; default: throw new CryptographicException("Error al inicializar al proveedor de cifrado"); } }
public bool fileOperation(string inFileName, string outFileName, cryptoAction action, ref string _returnErrorCode) { if (!File.Exists(inFileName)) { return false; } try { if (((_key != null) && (_IV != null))) { var fsIn = new FileStream(inFileName, FileMode.Open, FileAccess.Read); var fsOut = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write); fsOut.SetLength(0); byte[] key = makeKeyByteArray(); byte[] IV = makeIVByteArray(); byte[] byteBuffer = null; long fileLength = fsIn.Length; long bytesProcesed = 0; int bytesInBlock = 0; ICryptoTransform transform = getServiceProvider(action, key, IV); CryptoStream cryptoStream = null; switch (action) { case cryptoAction.Encrypt: cryptoStream = new CryptoStream(fsOut, transform, CryptoStreamMode.Write); break; case cryptoAction.Decrypt: cryptoStream = new CryptoStream(fsOut, transform, CryptoStreamMode.Write); break; } while ((bytesProcesed < fileLength)) { bytesInBlock = fsIn.Read(byteBuffer, 0, 4096); cryptoStream.Write(byteBuffer, 0, bytesInBlock); bytesProcesed = (bytesProcesed + long.Parse(bytesInBlock.ToString())); } if (!(cryptoStream == null)) { cryptoStream.Close(); } fsIn.Close(); fsOut.Close(); _returnErrorCode = ""; return true; } else { return false; } } catch (Exception ex) { ISException.RegisterExcepcion(ex); return false; } }
internal ICryptoTransform getServiceProvider(cryptoAction cAction, byte[] key, byte[] IV) { ICryptoTransform transform = null; switch (algorithm) { case cryptoProvider.DES: var des = new DESCryptoServiceProvider(); switch (cAction) { case cryptoAction.Encrypt: // si estamos cifrando, creamos el objeto cifrador. transform = des.CreateEncryptor(key, IV); break; case cryptoAction.Decrypt: // si estamos descifrando, creamos el objeto descifrador. transform = des.CreateDecryptor(key, IV); break; } return(transform); case cryptoProvider.TripleDES: var des3 = new TripleDESCryptoServiceProvider(); switch (cAction) { case cryptoAction.Encrypt: transform = des3.CreateEncryptor(key, IV); break; case cryptoAction.Decrypt: transform = des3.CreateDecryptor(key, IV); break; } return(transform); case cryptoProvider.RC2: var rc2 = new RC2CryptoServiceProvider(); switch (cAction) { case cryptoAction.Encrypt: transform = rc2.CreateEncryptor(key, IV); break; case cryptoAction.Decrypt: transform = rc2.CreateDecryptor(key, IV); break; } return(transform); case cryptoProvider.Rijndael: var rijndael = new RijndaelManaged(); switch (cAction) { case cryptoAction.Encrypt: transform = rijndael.CreateEncryptor(key, IV); break; case cryptoAction.Decrypt: transform = rijndael.CreateDecryptor(key, IV); break; } return(transform); default: throw new CryptographicException("Error al inicializar al proveedor de cifrado"); } }