Encrypting Excel Files with JavaScript via C++
Microsoft Excel (97 - 365) enables you to encrypt and password protect your spreadsheets. It uses algorithms provided by a cryptographic service provider, or CSP, a set of cryptographic algorithms with different properties. The default CSP is ‘Office 97/2000 Compatible’ or ‘Weak Encryption (XOR)’. It’s important to choose the proper encryption key length. Some CSPs don’t support more than 40 or 56 bits. That’s considered to be weak encryption. For strong encryption, a minimum key length of 128 bits is required. Microsoft Windows contains CSPs that offer strong encryption types as well, for example the ‘Microsoft Strong Cryptographic Provider’. To give you an idea, 128 bits encryption is what banks use to encrypt the connection with their Internet Banking systems.
Aspose.Cells allows you to encrypt and password protect Microsoft Excel files with your desired encryption type.
Using Microsoft Excel
To set file encryption settings in Microsoft Excel (here Microsoft Excel 2003):
- From the Tools menu, select Options. A dialog will appear.
- Select the Security tab.
- Input a password and click Advanced
- Choose the encryption type and confirm the password.
Encryption with Aspose.Cells for JavaScript via C++
The following example shows how to encrypt and password protect an excel file using the Aspose.Cells API.
<!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>
Specifying Password to modify Option
The following example shows how to set the Password to modify Microsoft Excel option for an existing file using the Aspose.Cells API.
<!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>
Verify the password of the encrypted file
To verify the password of the encrypted file, Aspose.Cells for JavaScript via C++ provides the FileFormatUtil.verifyPassword(Uint8Array, string) method. These methods accept two parameters, the file stream and the password that needs to be verified. The following code snippet demonstrates the use of the FileFormatUtil.verifyPassword(Uint8Array, string) method to verify whether the provided password is valid or not.
<!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>
Encryption/Decryption of ODS file with Aspose.Cells
Aspose.Cells allows you to encrypt and decrypt ODS file. Decrypted ODS file can be opened both in Excel and OpenOffice, however encrypted ODS file can only be opened by OpenOffice after providing the password. Excel cannot open the encrypted ODS file and may raise a warning message. The Encryption options are not applicable for ODS file unlike other file types. For encrypting an ODS file, load the file and set the WorkbookSettings.password value to the actual password before saving it. The output encrypted ODS file can be opened in OpenOffice only.
<!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>
For decrypting an ODS file, load the file by providing a password in the LoadOptions.password. Once the file is loaded, set the WorkbookSettings.password string to null.
<!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>