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();
示例代码
http://www.developer.nokia.com/Community/Wiki/File:BufEnEx.zip