📄 fileencryption.cpp
字号:
/* Copyright (c) 2003, Nokia Mobile Phones. All rights reserved */
#include <s32file.h>
#include <s32crypt.h>
#include "FileEncryption.h"
const TUint16 KArray[] = {0x1234, 0xFABC, 0xD00A, 0x0D8A, 0xA760, 0x0012, 0x2283, 0x1839, 0xBAAD, 0xC623};
// Maximum length of the key data
const TInt KMaxKeyDataLength = 2048;
// Maximum length of the message
const TInt KMaxPlainTextDataLength = 1024;
CFileEncryption* CFileEncryption::NewL()
{
CFileEncryption* self = NewLC();
CleanupStack::Pop(self);
return self;
}
CFileEncryption* CFileEncryption::NewLC()
{
CFileEncryption* self = new (ELeave) CFileEncryption();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CFileEncryption::CFileEncryption()
{
}
CFileEncryption::~CFileEncryption()
{
iFileSession.Close();
}
void CFileEncryption::ConstructL()
{
User::LeaveIfError(iFileSession.Connect());
}
void CFileEncryption::CreateFileL(const TDesC& aFileName, const TDesC& aFileData, const TDesC& aKey)
{
if (aFileData.Length() > KMaxPlainTextDataLength)
{
// leave if the data to encrypt is too long
User::Leave(KErrOverflow);
}
TParse parse;
parse.Set(aFileName, NULL, NULL); // parse the directory from the full pathname
TPtrC path = parse.Path();
iFileSession.MkDir(path); // Ignore return value
RFile file;
User::LeaveIfError(file.Replace(iFileSession, aFileName, EFileWrite));
CleanupClosePushL(file);
// Set up the encryption engine with the supplied key
CBoundedSecurityBase* encrypter = Security::NewL();
CleanupStack::PushL(encrypter);
HBufC* garble = GarbleKeyLC(aKey);
encrypter->SetL(KNullDesC, *garble);
CleanupStack::PopAndDestroy(); // garble
// Write the data to be encrypted to a file stream
RFileWriteStream outputFileStream(file);
CleanupClosePushL(outputFileStream);
// must use streaming api not WriteL
outputFileStream << encrypter->SecurityData();
// Write the data in the stream to an encrypted format
REncryptStream encryptedFileStream;
encryptedFileStream.AttachLC(outputFileStream, *encrypter, KNullDesC8);
encryptedFileStream << aFileData;
encryptedFileStream.Close();
encryptedFileStream.Pop();
// Clean up
CleanupStack::PopAndDestroy(); // close outputFileStream
CleanupStack::PopAndDestroy(); // encrypter
CleanupStack::PopAndDestroy(); // close file
}
HBufC* CFileEncryption::ReadFileL(const TDesC& aFileName, const TDesC& aKey)
{
// open file containing encrypted data
RFile file;
User::LeaveIfError(file.Open(iFileSession, aFileName, EFileRead));
CleanupClosePushL(file);
RFileReadStream inputFileStream(file);
CleanupClosePushL(inputFileStream);
// read the security data
HBufC8* securityData = HBufC8::NewLC(inputFileStream, KMaxKeyDataLength);
// create security object, using the read security data
CBoundedSecurityBase* decrypter = Security::NewL(*securityData);
CleanupStack::PopAndDestroy(); // securityData
CleanupStack::PushL(decrypter);
// authenticate password
HBufC* garble = GarbleKeyLC(aKey);
decrypter->PrepareL(*garble);
CleanupStack::PopAndDestroy(); // garble
// Read the encrypted data
RDecryptStream decryptedFileStream;
decryptedFileStream.AttachLC(inputFileStream, *decrypter, KNullDesC8);
CleanupClosePushL(decryptedFileStream);
HBufC* decryptedData = HBufC::NewL(decryptedFileStream, KMaxPlainTextDataLength);
CleanupStack::PopAndDestroy(); // close decryptedFileStream
decryptedFileStream.Pop(); // removes item placed on cleanup stack by AttachLC
CleanupStack::PopAndDestroy(); // decrypter
CleanupStack::PopAndDestroy(); // close inputFileStream
CleanupStack::PopAndDestroy(); // close file
return decryptedData;
}
HBufC* CFileEncryption::GarbleKeyLC(const TDesC& aKey)
{
TInt len = aKey.Length();
if (!len)
{
User::Leave(KErrArgument);
}
HBufC* ret = HBufC::NewLC(len);
*ret = aKey;
TPtr ptr = ret->Des();
for (TText i = 0; i < len; i++)
{
TText ch = ptr[i];
ch = static_cast<TText>((ch + i) * 2 / (i + 1));
while (ch > 'z')
{
ch = static_cast<TText>(ch - i - 1);
}
while (ch < ' ')
{
ch = static_cast<TText>(ch + i + 1);
}
ptr[i] = ch;
}
return ret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -