Discuss this help topic in SecureBlackbox Forum
Encrypt document with CryptoAPI ARCFOUR encryption handler
To encrypt a binary document using CryptoAPI RC4 encryption handler you need to
C#:
void EncryptBinaryRC4CryptoAPI(string sourceFilename, string destFilename, string password)
{
TElOfficeDocument Document = new TElOfficeDocument();
TElOfficeBinaryRC4CryptoAPIEncryptionHandler RC4CryptoAPIEncryptionHandler = new TElOfficeBinaryRC4CryptoAPIEncryptionHandler();
try
{
Document.Open(sourceFilename);
if ((Document.DocumentFormat != TSBOfficeDocumentFormat.Binary) || !Document.Encryptable)
throw new Exception("Cannot encrypt document using Binary RC4 CryptoAPI encryption handler");
RC4CryptoAPIEncryptionHandler.Password = password;
RC4CryptoAPIEncryptionHandler.KeyLength = 128;
RC4CryptoAPIEncryptionHandler.HardenedKeyGeneration = true;
RC4CryptoAPIEncryptionHandler.EncryptDocumentProperties = false;
Document.EncryptionHandler = RC4CryptoAPIEncryptionHandler;
using (FileStream f = new FileStream(destFilename, FileMode.CreateNew))
{
Document.EncryptTo(f);
}
}
finally
{
Document.Dispose();
RC4CryptoAPIEncryptionHandler.Dispose();
}
}
Delphi:
procedure EncryptBinaryRC4CryptoAPI(const SourceFilename, DestFilename, Password : string);
var
Document : TElOfficeDocument;
RC4CryptoAPIEncryptionHandler : TElOfficeBinaryRC4CryptoAPIEncryptionHandler;
F : TFileStream;
begin
Document := TElOfficeDocument.Create(nil);
RC4CryptoAPIEncryptionHandler := TElOfficeBinaryRC4CryptoAPIEncryptionHandler.Create(nil);
try
Document.Open(SourceFilename);
if (Document.DocumentFormat <> dfBinary) or not Document.Encryptable then
raise Exception.Create('Cannot encrypt document using Binary RC4 CryptoAPI encryption handler');
RC4CryptoAPIEncryptionHandler.Password := Password;
RC4CryptoAPIEncryptionHandler.KeyLength := 128;
RC4CryptoAPIEncryptionHandler.HardenedKeyGeneration := true;
RC4CryptoAPIEncryptionHandler.EncryptDocumentProperties := false;
Document.EncryptionHandler := RC4CryptoAPIEncryptionHandler;
F := TFileStream.Create(DestFilename, fmCreate or fmShareDenyWrite);
try
Document.EncryptTo(F);
finally
FreeAndNil(F);
end;
finally
FreeAndNil(Document);
FreeAndNil(RC4CryptoAPIEncryptionHandler);
end;
end;