AES的文件加密算法

 
  • 开发伙伴平台:

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值