/*
* DesEncrypt.cpp
*
* Created on: 2010-4-28
* Author: Administrator
*/
#include "DesEncrypt.h"
CDesEncrypt* CDesEncrypt::NewL()
{
CDesEncrypt* self = NewLC();
CleanupStack::Pop(self);
return self;
}
CDesEncrypt* CDesEncrypt::NewLC()
{
CDesEncrypt* self = new (ELeave)CDesEncrypt;
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CDesEncrypt::~CDesEncrypt()
{
if(iKeyDes)
{
delete iKeyDes;
iKeyDes = NULL;
}
if(iMingDes)
{
delete iMingDes;
iMingDes = NULL;
}
if(iEncryptDes)
{
delete iEncryptDes;
iEncryptDes = NULL;
}
if(iEncryptDesHex)
{
delete iEncryptDesHex;
iEncryptDesHex = NULL;
}
if(iDESEncryptor)
{
delete iDESEncryptor;
iDESEncryptor = NULL;
}
}
HBufC8* CDesEncrypt::KeyDes()
{
return iKeyDes;
}
void CDesEncrypt::SetKeyDesL(const TDesC8& aKeyDes)
{
if(aKeyDes.Length() == 8)//密钥必须是8个字节的
{
delete iKeyDes;
iKeyDes = NULL;
iKeyDes = aKeyDes.AllocL();
CreateDESEncryptorL();
}
}
HBufC8* CDesEncrypt::MingDes()
{
return iMingDes;
}
void CDesEncrypt::SetMingDesL(const TDesC8& aMingDes)
{
if(aMingDes.Length() > 0)
{
delete iMingDes;
iMingDes = NULL;
iMingDes = aMingDes.AllocL();
int len = iMingDes->Length();
int remain = len % 8;
if(remain)
{
iMingDes = iMingDes->ReAllocL(iMingDes->Length() + 8 - remain);
for (TInt i = 0 ;i < 8 - remain ; i++)
{
iMingDes->Des().Append(_L8(" "));
}
}
}
}
HBufC8* CDesEncrypt::EncryptDes()
{
return iEncryptDes;
}
void CDesEncrypt::SetEncryptDesL(const TDesC8& aEncryptDes)
{
delete iEncryptDes;
iEncryptDes = NULL;
iEncryptDes = aEncryptDes.AllocL();
}
HBufC8* CDesEncrypt::EncryptDesHex()
{
return iEncryptDesHex;
}
void CDesEncrypt::SetEncryptDesHexL(const TDesC8& aEncryptDesHex)
{
delete iEncryptDesHex;
iEncryptDesHex = NULL;
iEncryptDesHex = aEncryptDesHex.AllocL();
}
void CDesEncrypt::Encrypt() //加密
{
if(iKeyDes->Length() == 8 && iMingDes->Length() > 0)
{
delete iEncryptDes;
iEncryptDes =NULL;//清空iEncryptDes,保证没有上一个密文
int len = iMingDes->Length();
int pos = 0;
while (pos < len)
{
TBuf8<8> buf;
TPtrC8 part = iMingDes->Mid(pos,8);
buf.Copy(part);
iDESEncryptor->Transform(buf);//加密
if(iEncryptDes == NULL)
{
iEncryptDes = buf.AllocL();
}
else
{
iEncryptDes = iEncryptDes->ReAllocL(iEncryptDes->Length() + buf.Length());
iEncryptDes->Des().Append(buf);
}
iDESEncryptor->Reset();
TInt patrlength = part.Length();
pos += patrlength;
}
//转换成iEncryptDesHex
ChangeToEncryptDesHex();
}
}
void CDesEncrypt::ChangeToEncryptDesHex() //转换成iEncryptDesHex
{
TInt DSA = iEncryptDes->Length();
if(DSA > 0)
{
delete iEncryptDesHex;
iEncryptDesHex = NULL;
for(int i = 0; i < DSA; i++)
{
TBuf8<10> buf;
TInt val = (*iEncryptDes)[i];
To16(val, buf);
if(iEncryptDesHex == NULL)
{
iEncryptDesHex = buf.AllocL();
}
else
{
iEncryptDesHex = iEncryptDesHex->ReAllocL(iEncryptDesHex->Length() + buf.Length());
iEncryptDesHex->Des().Append(buf);
}
}
}
}
CDesEncrypt::CDesEncrypt()
{
}
void CDesEncrypt::ConstructL()
{
}
TChar CDesEncrypt::GetHex(TInt aVal) //根据余数返回对应的十六进制字符
{
switch (aVal)
{
case 0:
return TChar('0');
case 1:
return TChar('1');
case 2:
return TChar('2');
case 3:
return TChar('3');
case 4:
return TChar('4');
case 5:
return TChar('5');
case 6:
return TChar('6');
case 7:
return TChar('7');
case 8:
return TChar('8');
case 9:
return TChar('9');
case 10:
return TChar('a');
case 11:
return TChar('b');
case 12:
return TChar('c');
case 13:
return TChar('d');
case 14:
return TChar('e');
case 15:
return TChar('f');
default:
return TChar('0');
}
}
void CDesEncrypt::To16(TInt& val, TDes8& aDes)//将val装换成十六进制如:"af", "04"
{
while(val)
{
TBuf8<16> buf;
buf.Append(GetHex(val % 16));
aDes.Insert(0, buf);
val /= 16;
}
if(aDes.Length() == 1)
{
aDes.Insert(0, _L8("0"));
}
}
void CDesEncrypt::CreateDESEncryptorL() //只能在设置密钥时,调用该函数创建加密对象
{
delete iDESEncryptor;
iDESEncryptor = NULL;
iDESEncryptor = CDESEncryptor::NewL(*iKeyDes,EFalse);
}
评论1
最新资源