网站禁用复制深圳市文化广电旅游体育局
主题思想是数据的每一个Byte值都跟密钥相关联,当密钥的长度等于1时,每个_byte都加上密
钥的值,这有几十种可能,很容易破解,这就是电脑密码长度不能太少的原因。下面上代码,时间
有点仓促,欢迎指正:
/// <summary>
/// 加密二进制数据
/// </summary>
/// <param name="pData"></param>
/// <param name="nLength"></param>
/// <param name="sKey"></param>
/// <returns></returns>
/// 创建时间: 2015-03-23 最后一修改时间:2015-03-23
_ByteArray _MyApp::toEncrypt(const _byte* pData, const size_t nLength, const char* sKey)
{assert(sKey != null);//加密时: 每个byte值 + skey[byte索引 % sKey的长度] 的字符值//解码时: 每个byte值 - skey[byte索引 % sKey的长度] 的字符值 //校验码_uint64 dataSumValue = 0; //数据权重值,为校验作准备_uint16 nKeySumValue = 0;const char* pKey = sKey;_byte nKeyLength = 0;while (*pKey != 0) {nKeySumValue += *pKey;++pKey;++nKeyLength;}_ByteArray result(nLength + 20);result.add_t(nKeySumValue); //sKey的权重值 2for (size_t n = 0; n < nLength; ++n) {dataSumValue += pData[n];_byte index = n % nKeyLength;result.add( pData[n] + sKey[index]);//result.add(pData[n]);}result.add_t(nLength); //写入数据长度 8result.add_t(dataSumValue); //写入数据权重值 8 return result;
}/// <summary>
/// 解密二进制数据,不成功返回空的_ByteArray
/// </summary>
/// <param name="pData"></param>
/// <param name="nLength"></param>
/// <param name="sKey"></param>
/// <returns></returns>
/// 创建时间: 2015-03-23 最后一修改时间:2015-03-23
_ByteArray _MyApp::toDecrypt(const _byte* pData, const size_t nLength, const char* sKey)
{assert(sKey != null);_ByteArray result(nLength);if (nLength < 18) {std::cout << "错误:数据长度不对!" << "\n";result.ZeroBufferOne();return result;}//校验码_uint64 dataSumValue = *((_uint64*)(pData + nLength - 8)); //数据权重值,为校验作准备_uint16 nKeySumValue = *((_uint16*)(pData));_uint64 nDataLength = *((_uint64*)(pData + nLength - 16));_uint16 nKeySumValue2 = 0;const char* pKey = sKey;_byte nKeyLength = 0;while (*pKey != 0) {nKeySumValue2 += *pKey;++pKey;++nKeyLength;}if (nKeySumValue2 != nKeySumValue) {std::cout << "错误:密钥不对!" << "\n";result.clearData();result.ZeroBufferOne();return result;}_uint64 dataSumValue2 = 0;const _byte* pData2 = pData + 2;for (size_t n = 0; n < nDataLength; ++n) { _byte index = n % nKeyLength; _byte b = pData2[n] - sKey[index];result.add(b);dataSumValue2 += b; }if (dataSumValue2 != dataSumValue) {std::cout << "错误:数据校验失败!" << "\n";result.clearData();result.ZeroBufferOne();return result;}return result;
}