CBC模式下的数据加密和解密
文章信息
代码示例
源文件: Media:CBCEx.zip
Dependencies: S60 3rd Edition
文章
翻译:
由 hoolee
最后由 hamishwillee
在 09 Aug 2012 编辑
This 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.
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();
}
- 示例程序


(no comments yet)