关于DES的密文长度的问题 是不是DES只加密8位的明文?我要加密一个CString,它的长度很长 该如何搞把CString,分成一个一个的8位, 然后加密? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把CString,分成一个一个的8位, 然后加密!正解!用ECB,CBC模式都可以 谢谢, 楼上说的对我后来该成AES了, 然后写了个类封装一下, 这次是16字节的分组代码如下:#include "Rijndael.h"namespace utility{ class CMyAes { public: CMyAes(void); public: ~CMyAes(void); BOOL Encrypt(const CString&key, const CString &in, CString &out); BOOL Decrypt(const CString&key, const CString &in, CString &out); private: //Function to convert unsigned char to string of length 2 void Char2Hex(unsigned char ch, char* szHex); //Function to convert string of length 2 to unsigned char void Hex2Char(char const* szHex, unsigned char& rch); //Function to convert string of unsigned chars to string of chars void CharStr2HexStr(unsigned char const* pucCharStr, char* pszHexStr, int iSize); void HexStr2CharStr(char const* pszHexStr, unsigned char* pucCharStr, int iSize); };}-------------------namespace utility{ CMyAes::CMyAes(void) { } CMyAes::~CMyAes(void) { } //Function to convert unsigned char to string of length 2 void CMyAes::Char2Hex(unsigned char ch, char* szHex) { unsigned char byte[2]; byte[0] = ch/16; byte[1] = ch%16; for(int i=0; i<2; i++) { if(byte[i] >= 0 && byte[i] <= 9) szHex[i] = '0' + byte[i]; else szHex[i] = 'A' + byte[i] - 10; } szHex[2] = 0; } //Function to convert string of length 2 to unsigned char void CMyAes::Hex2Char(char const* szHex, unsigned char& rch) { rch = 0; for(int i=0; i<2; i++) { if(*(szHex + i) >='0' && *(szHex + i) <= '9') rch = (rch << 4) + (*(szHex + i) - '0'); else if(*(szHex + i) >='A' && *(szHex + i) <= 'F') rch = (rch << 4) + (*(szHex + i) - 'A' + 10); else break; } } //Function to convert string of unsigned chars to string of chars void CMyAes::CharStr2HexStr(unsigned char const* pucCharStr, char* pszHexStr, int iSize) { int i; char szHex[3]; pszHexStr[0] = 0; for(i=0; i<iSize; i++) { Char2Hex(pucCharStr[i], szHex); strcat(pszHexStr, szHex); } } //Function to convert string of chars to string of unsigned chars void CMyAes::HexStr2CharStr(char const* pszHexStr, unsigned char* pucCharStr, int iSize) { int i; unsigned char ch; for(i=0; i<iSize; i++) { Hex2Char(pszHexStr+2*i, ch); pucCharStr[i] = ch; } } BOOL CMyAes::Encrypt(const CString &key, const CString &in, CString &out) { try { char szHex[33] = {0}; const int LEN = 16; int iLenStr = in.GetLength(); int iBei = iLenStr / LEN; int iYu = iLenStr % LEN; CString newIn = in; bool bHasTail = false; if (iYu != 0) { for (int i=0; i<LEN - iYu; ++i) { newIn += "0"; } bHasTail = true; ++iBei; } CRijndael oRijndael; char *pKey = CMyUtility::GetAnsiString(key); oRijndael.MakeKey(pKey, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", LEN, LEN); delete []pKey; CString strBuffer; for (int i=0; i<iBei; ++i) { char szDataIn[LEN + 1]; memset(szDataIn, 0, sizeof(szDataIn)); strBuffer = newIn.Mid(i*LEN, LEN); char *pIn = CMyUtility::GetAnsiString(strBuffer); strncpy(szDataIn, pIn, LEN); delete [] pIn; pIn = NULL; char szDataOut[LEN+1] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; oRijndael.EncryptBlock(szDataIn, szDataOut); //CharStr2HexStr((unsigned char*)szDataIn, szHex, LEN); CharStr2HexStr((unsigned char*)szDataOut, szHex, LEN); out += szHex; //memset(szDataIn, 0, LEN); //oRijndael.DecryptBlock(szDataOut, szDataIn); } if(bHasTail) out.Format(_T("%s{[]}%d"), out, LEN - iYu); else out.Format(_T("%s{[]}%d"), out, 0); //CharStr2HexStr(unsigned char*)szDataIn, szHex, LEN); } catch(exception& roException) { roException.what(); } return TRUE; } BOOL CMyAes::Decrypt(const CString &key, const CString &in, CString &out) { try { char szHex[33] = {0}; int iPos = in.Find(_T("{[]}")); CString newIn = in.Left(iPos); CString strTail = in.Right(in.GetLength() - iPos - 4); int iTailLen = _ttoi(strTail); const int LEN = 16; int iLenStr = newIn.GetLength(); int iBei = iLenStr / (LEN*2); CRijndael oRijndael; char *pKey = CMyUtility::GetAnsiString(key); oRijndael.MakeKey(pKey, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", LEN, LEN); delete []pKey; CString strBuffer; for (int i=0; i<iBei; ++i) { char szDataIn[LEN + 1]; memset(szDataIn, 0, sizeof(szDataIn)); strBuffer = newIn.Mid(i*LEN*2, LEN*2); char *pIn = CMyUtility::GetAnsiString(strBuffer); strncpy(szHex, pIn, LEN*2); delete [] pIn; pIn = NULL; HexStr2CharStr(szHex, (unsigned char*)szDataIn, LEN); char szDataOut[LEN+1] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; oRijndael.DecryptBlock(szDataIn, szDataOut); out += szDataOut; } if (iTailLen > 0) { out = out.Left(out.GetLength() - iTailLen); } } catch(exception& roException) { roException.what(); } return TRUE; }} canvas中图形事件绑定 _variant_t 变量析构出错 请教一个界面问题---XDJM请回答分不够再加 用CLASSWIZARD怎样建立一个基类是COBJECT的派生类? 1000元,买一段源码!!! 知道文件名或文件夹的名称,怎样获得该文件或文件夹的绝对路径? 请教: 如何得到音频设备的唯一标识符. 基本功不扎实,请问怎样做一个带参数运行的命令行程序啊? windows 系统服务 抢分吧,Installshield的问题。 SetWindowTextA(hEdit_Correct_A, "123");总是引起堆栈溢出的异常 F10调试时出现的错误
namespace utility
{ class CMyAes
{
public:
CMyAes(void);
public:
~CMyAes(void); BOOL Encrypt(const CString&key, const CString &in, CString &out);
BOOL Decrypt(const CString&key, const CString &in, CString &out); private: //Function to convert unsigned char to string of length 2
void Char2Hex(unsigned char ch, char* szHex); //Function to convert string of length 2 to unsigned char
void Hex2Char(char const* szHex, unsigned char& rch); //Function to convert string of unsigned chars to string of chars
void CharStr2HexStr(unsigned char const* pucCharStr, char* pszHexStr, int iSize);
void HexStr2CharStr(char const* pszHexStr, unsigned char* pucCharStr, int iSize);
};
}
-------------------namespace utility
{ CMyAes::CMyAes(void)
{
} CMyAes::~CMyAes(void)
{
}
//Function to convert unsigned char to string of length 2
void CMyAes::Char2Hex(unsigned char ch, char* szHex)
{
unsigned char byte[2];
byte[0] = ch/16;
byte[1] = ch%16;
for(int i=0; i<2; i++)
{
if(byte[i] >= 0 && byte[i] <= 9)
szHex[i] = '0' + byte[i];
else
szHex[i] = 'A' + byte[i] - 10;
}
szHex[2] = 0;
} //Function to convert string of length 2 to unsigned char
void CMyAes::Hex2Char(char const* szHex, unsigned char& rch)
{
rch = 0;
for(int i=0; i<2; i++)
{
if(*(szHex + i) >='0' && *(szHex + i) <= '9')
rch = (rch << 4) + (*(szHex + i) - '0');
else if(*(szHex + i) >='A' && *(szHex + i) <= 'F')
rch = (rch << 4) + (*(szHex + i) - 'A' + 10);
else
break;
}
} //Function to convert string of unsigned chars to string of chars
void CMyAes::CharStr2HexStr(unsigned char const* pucCharStr, char* pszHexStr, int iSize)
{
int i;
char szHex[3];
pszHexStr[0] = 0;
for(i=0; i<iSize; i++)
{
Char2Hex(pucCharStr[i], szHex);
strcat(pszHexStr, szHex);
}
} //Function to convert string of chars to string of unsigned chars
void CMyAes::HexStr2CharStr(char const* pszHexStr, unsigned char* pucCharStr, int iSize)
{
int i;
unsigned char ch;
for(i=0; i<iSize; i++)
{
Hex2Char(pszHexStr+2*i, ch);
pucCharStr[i] = ch;
}
} BOOL CMyAes::Encrypt(const CString &key, const CString &in, CString &out)
{
try
{
char szHex[33] = {0}; const int LEN = 16;
int iLenStr = in.GetLength();
int iBei = iLenStr / LEN;
int iYu = iLenStr % LEN; CString newIn = in;
bool bHasTail = false;
if (iYu != 0)
{
for (int i=0; i<LEN - iYu; ++i)
{
newIn += "0";
}
bHasTail = true; ++iBei;
} CRijndael oRijndael;
char *pKey = CMyUtility::GetAnsiString(key);
oRijndael.MakeKey(pKey,
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", LEN, LEN);
delete []pKey; CString strBuffer;
for (int i=0; i<iBei; ++i)
{
char szDataIn[LEN + 1];
memset(szDataIn, 0, sizeof(szDataIn));
strBuffer = newIn.Mid(i*LEN, LEN);
char *pIn = CMyUtility::GetAnsiString(strBuffer);
strncpy(szDataIn, pIn, LEN);
delete [] pIn;
pIn = NULL;
char szDataOut[LEN+1] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
oRijndael.EncryptBlock(szDataIn, szDataOut);
//CharStr2HexStr((unsigned char*)szDataIn, szHex, LEN);
CharStr2HexStr((unsigned char*)szDataOut, szHex, LEN); out += szHex;
//memset(szDataIn, 0, LEN);
//oRijndael.DecryptBlock(szDataOut, szDataIn);
}
if(bHasTail)
out.Format(_T("%s{[]}%d"), out, LEN - iYu);
else
out.Format(_T("%s{[]}%d"), out, 0);
//CharStr2HexStr(unsigned char*)szDataIn, szHex, LEN);
}
catch(exception& roException)
{
roException.what();
} return TRUE;
}
BOOL CMyAes::Decrypt(const CString &key, const CString &in, CString &out)
{
try
{
char szHex[33] = {0}; int iPos = in.Find(_T("{[]}"));
CString newIn = in.Left(iPos);
CString strTail = in.Right(in.GetLength() - iPos - 4);
int iTailLen = _ttoi(strTail); const int LEN = 16;
int iLenStr = newIn.GetLength();
int iBei = iLenStr / (LEN*2); CRijndael oRijndael;
char *pKey = CMyUtility::GetAnsiString(key);
oRijndael.MakeKey(pKey,
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", LEN, LEN);
delete []pKey; CString strBuffer;
for (int i=0; i<iBei; ++i)
{
char szDataIn[LEN + 1];
memset(szDataIn, 0, sizeof(szDataIn));
strBuffer = newIn.Mid(i*LEN*2, LEN*2);
char *pIn = CMyUtility::GetAnsiString(strBuffer);
strncpy(szHex, pIn, LEN*2);
delete [] pIn;
pIn = NULL;
HexStr2CharStr(szHex, (unsigned char*)szDataIn, LEN);
char szDataOut[LEN+1] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
oRijndael.DecryptBlock(szDataIn, szDataOut); out += szDataOut;
} if (iTailLen > 0)
{
out = out.Left(out.GetLength() - iTailLen);
}
}
catch(exception& roException)
{
roException.what();
} return TRUE;
}
}