Cifrar archivos de Excel con JavaScript vía C++
Microsoft Excel (97 - 365) te permite cifrar y proteger con contraseña tus hojas de cálculo. Utiliza algoritmos proporcionados por un proveedor de servicios criptográficos, o CSP, un conjunto de algoritmos criptográficos con diferentes propiedades. El CSP predeterminado es ‘Compatible con Office 97/2000’ o ‘Cifrado débil (XOR)’. Es importante elegir la longitud adecuada de la clave de cifrado. Algunos CSP no admiten más de 40 o 56 bits, considerándose un cifrado débil. Para un cifrado fuerte, se requiere una longitud mínima de clave de 128 bits. Microsoft Windows contiene CSP que ofrecen tipos de cifrado fuertes, como por ejemplo el ‘Proveedor Criptográfico Fuerte de Microsoft’. Para darte una idea, los bancos utilizan un cifrado de 128 bits para encriptar la conexión con sus sistemas de Banca por Internet.
Aspose.Cells te permite cifrar y proteger con contraseña archivos de Microsoft Excel con el tipo de cifrado que desees.
Usar Microsoft Excel
Para configurar los ajustes de cifrado de archivos en Microsoft Excel (aquí Microsoft Excel 2003):
- Desde el menú Herramientas, selecciona Opciones. Aparecerá un cuadro de diálogo.
- Selecciona la pestaña Seguridad.
- Ingresa una contraseña y haz clic en Avanzado.
- Elige el tipo de cifrado y confirma la contraseña.
Cifrado con Aspose.Cells for JavaScript vía C++
El siguiente ejemplo muestra cómo cifrar y proteger con contraseña un archivo de Excel utilizando la API de Aspose.Cells.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Encrypt Workbook Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Utils, EncryptionType } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate a Workbook object by opening the uploaded Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Specify XOR encryption type.
workbook.encryptionOptions = { type: EncryptionType.XOR, keyLength: 40 };
// Specify Strong Encryption type (RC4, Microsoft Strong Cryptographic Provider).
workbook.encryptionOptions = { type: EncryptionType.StrongCryptographicProvider, keyLength: 128 };
// Password protect the file.
workbook.settings.password = "1234";
// Save the excel file (Excel97To2003 format for .xls)
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'encryptedBook1.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Encrypted Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">File encrypted successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Especificar la opción de Contraseña para modificar
El siguiente ejemplo muestra cómo establecer la opción de Contraseña para modificar en Microsoft Excel para un archivo existente utilizando la API de Aspose.Cells.
<!DOCTYPE html>
<html>
<head>
<title>Specify Password To Modify Option Example</title>
</head>
<body>
<h1>Specify Password To Modify Option Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate a Workbook object by opening the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Set the password for modification.
workbook.settings.writeProtection.password = "1234";
// Save the excel file in Excel97-2003 format (.xls)
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'SpecifyPasswordToModifyOption.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Password to modify set successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Verificar la contraseña del archivo cifrado
Para verificar la contraseña del archivo cifrado, Aspose.Cells for JavaScript vía C++ proporciona el método FileFormatUtil.verifyPassword(Uint8Array, string). Estos métodos aceptan dos parámetros, la transmisión del archivo y la contraseña que necesita ser verificada. El siguiente fragmento de código muestra el uso del método FileFormatUtil.verifyPassword(Uint8Array, string) para verificar si la contraseña proporcionada es válida o no.
<!DOCTYPE html>
<html>
<head>
<title>Verify Password Example</title>
</head>
<body>
<h1>Verify Password Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv,.xlsm" />
<button id="runExample">Verify Password</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const uint8 = new Uint8Array(arrayBuffer);
const isPasswordValid = AsposeCells.FileFormatUtil.verifyPassword(uint8, "1234");
document.getElementById('result').innerHTML = '<p>Password is Valid: ' + isPasswordValid + '</p>';
});
</script>
</html>
Cifrado/Descifrado de archivos ODS con Aspose.Cells
Aspose.Cells te permite encriptar y desencriptar archivos ODS. El archivo ODS desencriptado puede abrirse tanto en Excel como en OpenOffice, sin embargo, el archivo ODS encriptado solo puede abrirse en OpenOffice tras proporcionar la contraseña. Excel no puede abrir el archivo encriptado de ODS y puede mostrar un mensaje de advertencia. Las opciones de encriptación no son aplicables a archivos ODS, a diferencia de otros tipos de archivos. Para encriptar un archivo ODS, carga el archivo y establece el valor de WorkbookSettings.password con la contraseña real antes de guardarlo. El archivo ODS encriptado resultante solo puede abrirse en OpenOffice.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Encrypt ODS File Example</h1>
<input type="file" id="fileInput" accept=".ods" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an ODS file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate Workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Password protect the file
workbook.settings.password = "1234";
// Save the ODS file
const outputData = workbook.save(SaveFormat.Ods);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputEncryptedODSFile.ods';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Encrypted ODS File';
document.getElementById('result').innerHTML = '<p style="color: green;">File encrypted successfully! Click the download link to get the encrypted file.</p>';
});
</script>
</html>
Para descifrar un archivo ODS, carga el archivo proporcionando una contraseña en el LoadOptions.password. Una vez que el archivo está cargado, establece el valor de la cadena WorkbookSettings.password como nulo.
<!DOCTYPE html>
<html>
<head>
<title>Decrypt ODS Example</title>
</head>
<body>
<h1>Decrypt ODS Example</h1>
<input type="file" id="fileInput" accept=".ods" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, LoadOptions, LoadFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an ODS file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Create load options for ODS and set original password
const loadOptions = new LoadOptions(LoadFormat.Ods);
loadOptions.password = "1234";
// Load the encrypted ODS file with the appropriate load options
const workbook = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
// Set the password to null to remove encryption/password
workbook.settings.password = null;
// Save the decrypted ODS file
const outputData = workbook.save(SaveFormat.Ods);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputDecryptedODSFile.ods';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Decrypted ODS File';
document.getElementById('result').innerHTML = '<p style="color: green;">File decrypted successfully! Click the download link to get the decrypted file.</p>';
});
</script>
</html>