×
Namespaces

Variants
Actions
Revision as of 09:41, 9 August 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

CBC模式下的数据加密和解密

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

代码示例
源文件: Media:CBCEx.zip

兼容于
Dependencies: S60 3rd Edition

文章
翻译:
hoolee
最后由 hamishwillee 在 09 Aug 2012 编辑

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: hamishwillee (10 May 2011)
SymbianCryptographyLibrary appears to be unavailable. Needs to be found, or if no longer needed this article needs to be deleted.

  • 详细描述

Cipher-block chaining(CBC)是一种加密方式,它将纯文本分块和之前加密的密文块进行XOR操作。这里每个密文块都取决于纯文本的这种处理。注意要确保消息独立,并在第一个块中使用初始向量。

Symbian Cryptography library的CModeCBCEncryptor可以用来加密数据,它使用CBlockTransformation子类在它随后拥有的CAESEncryptor中进行初始化。下列代码演示了对CBC模式下的数据块加密和解密的操作。

  • 解决方案

CBC模式下对数据的加密

void CCBCExAppUi::CBCEncryption(TPtr8& aDataPtr)
{
CAESEncryptor* aesEncryptor = CAESEncryptor::NewL(iCipherkey);
CModeCBCEncryptor* cbcEncryptor = CModeCBCEncryptor::NewLC(aesEncryptor, iIV);<br>
for(TInt i = 0; i<3; i++)
{
TPtr8 tempDataptr = aDataPtr.MidTPtr(16*i,16);
cbcEncryptor->Transform(tempDataptr);
cbcEncryptor->SetIV(tempDataptr);
}
CleanupStack::PopAndDestroy();
}


CBC模式下对数据的解密:

void CCBCExAppUi::CBCDecryption(TPtr8& aDataPtr)
{
CAESDecryptor* aesdecryptor = CAESDecryptor::NewL(iCipherkey);
CModeCBCDecryptor* cbcDecryptor = CModeCBCDecryptor::NewLC(aesdecryptor, iIV);<br>
TBuf8<16>tempBuf;
for(TInt i = 0; i<3; i++)
{
TPtr8 tempDataptr = aDataPtr.MidTPtr(16*i,16);
tempBuf.Copy(tempDataptr);
cbcDecryptor->Transform(tempDataptr);
cbcDecryptor->SetIV(tempBuf);
}<br>
CleanupStack::PopAndDestroy();
}


  • 示例程序

File:CBCEx.zip

This page was last modified on 9 August 2012, at 09:41.
142 page views in the last 30 days.
×