AES加解密 aes-256-ecb
Linux 下执行 echo “123” | 加密 ,实际是“123\n” 进行加密
原字符串填充方式 : 补0 或 pkcs7
string formatSrc(const string& strSrc, int iBlockSize, int iPaddingFlag)
{
if (iPaddingFlag != 0 && iPaddingFlag != 1)
{
return strSrc;
}
int iLength = strSrc.length();
int iPackSize = 0;
int iPackValue = 0;
//计算总长度与填充大小 0 --使用0填充 , 其他使用pack7填充
if (iLength % iBlockSize != 0)
{
iPackSize = iBlockSize - iLength%iBlockSize;
iPackValue = iPaddingFlag == 0 ? 0 : iPackSize;
iLength += iPackSize;
}
//填充
Poco::Buffer<unsigned char> buffer(iLength);
copy(strSrc.begin(), strSrc.end(), buffer.begin());
for (int iPackPos = strSrc.length(); iPackPos < iLength; ++iPackPos)
{
buffer[iPackPos] = iPackValue;
}
//转换成字符串
ostringstream oss;
Poco::MemoryInputStream istr((const char*)buffer.begin(), buffer.size());
Poco::StreamCopier::copyStream(istr, oss);
return oss.str();
}
string strSrc = "motdepasse";
//密码,需要转换成64位不足不0
string strKey = "f32b608cf754193cb85ba8e85ed9e36f";
int iLength = strKey.length();
while (iLength++ < 64)
{
strKey += "0";
}
CipherKey::ByteVec key = Poco::DigestEngine::digestFromHex(strKey);
CipherKey::ByteVec iv;
CipherKey cipherKey = CipherKey("aes-256-ecb", key, iv);
Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(cipherKey);
strSrc = formatSrc(strSrc, cipherKey.blockSize(), 0);
string out = pCipher->encryptString(strSrc, Cipher::ENC_BINHEX);
cout << "c++testexe :" << out << endl;