Discuss this help topic in SecureBlackbox Forum
Decrypt document
To decrypt a binary document you need to
C#:
void DecryptBinaryRC4(string sourceFilename, string destFilename, string password)
{
using (TElOfficeDocument Document = new TElOfficeDocument())
{
Document.Open(sourceFilename);
if ((Document.DocumentFormat != TSBOfficeDocumentFormat.Binary) || !Document.IsEncrypted)
throw new Exception("Cannot decrypt document using Binary RC4 encryption handler");
if (Document.EncryptionHandler is TElOfficeBinaryRC4CryptoAPIEncryptionHandler)
{
TElOfficeBinaryRC4CryptoAPIEncryptionHandler RC4CryptoAPIEncryptionHandler = (TElOfficeBinaryRC4CryptoAPIEncryptionHandler)Document.EncryptionHandler;
RC4CryptoAPIEncryptionHandler.Password = password;
if (!RC4CryptoAPIEncryptionHandler.IsPasswordValid())
throw new Exception("Invalid password");
}
else if (Document.EncryptionHandler is TElOfficeBinaryRC4EncryptionHandler)
{
TElOfficeBinaryRC4EncryptionHandler RC4EncryptionHandler = (TElOfficeBinaryRC4EncryptionHandler)Document.EncryptionHandler;
RC4EncryptionHandler.Password = password;
if (!RC4EncryptionHandler.IsPasswordValid())
throw new Exception("Invalid password");
}
else
throw new Exception("Unknown encryption handler");
using (FileStream f = new FileStream(destFilename, FileMode.CreateNew))
{
Document.DecryptTo(f);
}
}
}
Delphi:
procedure DecryptBinaryRC4(const SourceFilename, DestFilename, Password : string);
var
Document : TElOfficeDocument;
F : TFileStream;
begin
Document := TElOfficeDocument.Create(nil);
try
Document.Open(SourceFilename);
if (Document.DocumentFormat <> dfBinary) or not Document.IsEncrypted then
raise Exception.Create('Cannot decrypt document using Binary RC4 encryption handler');
if Document.EncryptionHandler is TElOfficeBinaryRC4CryptoAPIEncryptionHandler then
begin
TElOfficeBinaryRC4CryptoAPIEncryptionHandler(Document.EncryptionHandler).Password := Password;
if not TElOfficeBinaryRC4CryptoAPIEncryptionHandler(Document.EncryptionHandler).IsPasswordValid then
raise Exception.Create('Invalid password');
end
else if Document.EncryptionHandler is TElOfficeBinaryRC4EncryptionHandler then
begin
TElOfficeBinaryRC4EncryptionHandler(Document.EncryptionHandler).Password := Password;
if not TElOfficeBinaryRC4EncryptionHandler(Document.EncryptionHandler).IsPasswordValid then
raise Exception.Create('Invalid password');
end
else
raise Exception.Create('Unknown encryption handler');
F := TFileStream.Create(DestFilename, fmCreate or fmShareDenyWrite);
try
Document.DecryptTo(F);
finally
FreeAndNil(F);
end;
finally
FreeAndNil(Document);
end;
end;