Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

AES的文件加密算法

From Wiki
Jump to: navigation, search
Article Metadata

代码示例
文章
huwell 在 05 May 2009 创建
最后由 hamishwillee 在 15 Dec 2011 编辑
  • 开发伙伴平台:

S60 3rd Edition FP1, S60 3rd Edition FP2


  • 详细描述

根据Advanced Encryption Standard(AES),我们可以使用Symbian Cryptography API中的CAESEncryptor和CAESDecryptor类对一组数据分别进行加密解密。但这个类只能对16 bytes数据组进行转换,无法对大容量数据源进行加密解密操作,这样的操作需要使用到CBufferedEncryptor和CBufferedDecryptor。这些类需要一个解密加密类对象进行初始化,根据操作模式,还需要CPadding的子类。

  • 解决方案

下面示例演示了如何使用AES对一个纯文本进行加密

_LIT( KPlainInputFileName, "C:\\Data\\PlainInput.txt" );
_LIT( KEncryptedOutputFileName, "C:\\Data\\EnOutput.txt" );
_LIT8(KAESKey,"1234567890abcdef"); <br>
//Reading the plain text from the file
RFs fsSession;
User::LeaveIfError(fsSession.Connect());
RFile file;
User::LeaveIfError(file.Open(fsSession,KPlainInputFileName,EFileShareExclusive|EFileRead));
TInt plainTextsize;
file.Size(plainTextsize);
HBufC8* plainText = HBufC8::NewLC(plainTextsize);
TPtr8 plainTextPtr = plainText->Des();
file.Read(plainTextPtr);
file.Close(); <br>
//Initializing the encrytor and padding objects
CPaddingSSLv3* encryptPadding = CPaddingSSLv3::NewL(16);
CAESEncryptor* aesEncryptor = CAESEncryptor::NewL(KAESKey);
CBufferedEncryptor* bufEncryptor = CBufferedEncryptor::NewL(aesEncryptor, encryptPadding);<br>
 
 
HBufC8* encryptData = HBufC8::NewLC(bufEncryptor->MaxFinalOutputLength(plainTextPtr.Size()));
 
 
TPtr8 encryptDataPtr = encryptData->Des();<br>
//Encrypting the text and writing into an output file
bufEncryptor->ProcessFinalL(plainTextPtr,encryptDataPtr);<br>
TInt err=file.Open(fsSession,KEncryptedOutputFileName,EFileShareExclusive|EFileWrite);
if (err==KErrNotFound) // file does not exist - create it
err=file.Create(fsSession,KEncryptedOutputFileName,EFileShareExclusive|EFileWrite);
file.Write(encryptDataPtr);
file.Close();
CleanupStack::PopAndDestroy2);//encryptData,plainText
fsSession.Close();

使用下列代码进行文件解密:

_LIT( KEncryptedOutputFileName, "C:\\Data\\EnOutput.txt" );
_LIT( KPlainOutputFileName, "C:\\Data\\PlainOutput.txt" );
_LIT8(KAESKey,"1234567890abcdef"); <br>
//Reading the encrypted text from the file
RFs fsSession;
User::LeaveIfError(fsSession.Connect());
RFile file;
User::LeaveIfError(file.Open(fsSession,KEncryptedOutputFileName,EFileShareExclusive|EFileRead));
TInt fileSize;
file.Size(fileSize);<br>
HBufC8* encryptData = HBufC8::NewLC(fileSize);
TPtr8 encryptDataPtr = encryptData->Des();
file.Read(encryptDataPtr);
file.Close();<br>
//Initializing the encrytor and padding objects
CPaddingSSLv3* decryptPadding = CPaddingSSLv3::NewL(16);
CAESDecryptor* aesDecryptor = CAESDecryptor::NewL(KAESKey);
CBufferedDecryptor* bufDecryptor = CBufferedDecryptor::NewL(aesDecryptor, decryptPadding);<br>
HBufC8* decryptedText = HBufC8::NewLC(bufDecryptor->MaxFinalOutputLength(encryptDataPtr.Size()));
 
 
TPtr8 decryptedTextPtr = decryptedText->Des();<br>
//Decrypting the text and writing the output into a file.
bufDecryptor->ProcessFinalL(encryptDataPtr,decryptedTextPtr); <br>
TInt err=file.Open(fsSession,KPlainOutputFileName,EFileShareExclusive|EFileWrite);
if (err==KErrNotFound) // file does not exist - create it
err=file.Create(fsSession,KPlainOutputFileName,EFileShareExclusive|EFileWrite);
file.Write(decryptedTextPtr);
file.Close();<br>
CleanupStack::PopAndDestroy(2); // encryptData,decryptedText
fsSession.Close();
  • 示例代码

File:BufEnEx.zip

This page was last modified on 15 December 2011, at 05:42.
284 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×