BIRDandFISH123
BIRDandFISH123
2016-08-20 03:18

关于MFC中对ini文件的加密

5

以下是我使用的加密函数, 对ini中的文本进行了加密和解密 但是解密函数调用后解密的文字有的文本会成功,有的解密一部分就乱码了 然后工程是使用的多字节文本 请各位大神帮忙看看 !感激不尽

CString Encrypt(CString S, WORD Key) //加密函数
{
CString Result, str;
int i, j;
Result = S; //初始化结果字符串
for (i = 0; i < S.GetLength(); i++) //依次对字符串中各字符进行操作
{
Result.SetAt(i, S.GetAt(i) ^ (Key >> 8)); //将密钥移位后与字符异或
Key = ((BYTE)Result.GetAt(i) + Key)*C1 + C2; //产生下一个密钥
}
S = Result; //保存结果
Result.Empty(); //清除结果
for ( i = 0; i < S.GetLength(); i++) //对加密结果进行转换
{
j = (BYTE)S.GetAt(i);//提取字符
str = "12"; //设置str长度为2
str.SetAt(0, 100+ j / 26); //65可以改的更大 变乱码
str.SetAt(1, 100+ j % 26);
Result += str;

}
return Result;

}

CString Decrypt(CString S, WORD Key) //解密函数
{
CString Result, str;
int i, j;
Result.Empty(); //清除结果
for (i = 0; i < S.GetLength() / 2; i++) //将字符串俩个一组
{
j = (((BYTE)S.GetAt(2 * i) - 100) * 26); //必须与加密一致
j += (BYTE)S.GetAt(2 * i + 1) - 100;
str = "1";//设置str长度为1
str.SetAt(0,j);
Result += str;
}
S = Result;//保存中间结果
for (i = 0; i {
Result.SetAt(i, (BYTE)S.GetAt(i) ^ (Key >> 8)); //将密钥
Key = ((BYTE)S.GetAt(i) + Key)*C1 + C2;

}
return Result; 

}

以下是提取文本的函数

CString text1, text2, text3, text4, text5, text6;
GetPrivateProfileString("01", "text", NULL, text1.GetBuffer(10000), 10000, "ini\\content.ini");
GetPrivateProfileString("02", "text", NULL, text2.GetBuffer(10000), 10000, "ini\\content.ini");
GetPrivateProfileString("03", "text", NULL, text3.GetBuffer(10000), 10000, "ini\\content.ini");
GetPrivateProfileString("04", "text", NULL, text4.GetBuffer(10000), 10000, "ini\\content.ini");
GetPrivateProfileString("05", "text", NULL, text5.GetBuffer(10000), 10000, "ini\\content.ini");
GetPrivateProfileString("06", "text", NULL, text6.GetBuffer(10000), 10000, "ini\\content.ini");
text1.ReleaseBuffer();
text2.ReleaseBuffer();
text3.ReleaseBuffer();
text4.ReleaseBuffer();
text5.ReleaseBuffer();
text6.ReleaseBuffer();
CString AddKeyStr = Encrypt(text1, key);
WritePrivateProfileString("01", "text", AddKeyStr, "ini\\content1.ini");
    AddKeyStr = "";
    AddKeyStr = Encrypt(text2, key);
    WritePrivateProfileString("02", "text", AddKeyStr, "ini\\content1.ini");
    AddKeyStr = "";
    AddKeyStr = Encrypt(text3, key);
    WritePrivateProfileString("03", "text", AddKeyStr, "ini\\content1.ini");
    AddKeyStr = "";
    AddKeyStr = Encrypt(text4, key);
    WritePrivateProfileString("04", "text", AddKeyStr, "ini\\content1.ini");
    AddKeyStr = "";
    AddKeyStr = Encrypt(text5, key);
    WritePrivateProfileString("05", "text", AddKeyStr, "ini\\content1.ini");
    AddKeyStr = "";
    AddKeyStr = Encrypt(text6, key);
    WritePrivateProfileString("06", "text", AddKeyStr, "ini\\content1.ini");

//加密







        CString text1, text2, text3, text4, text5, text6;

GetPrivateProfileString("01", "text", NULL, text1.GetBuffer(10000), 10000, "ini\\content1.ini");
GetPrivateProfileString("02", "text", NULL, text2.GetBuffer(10000), 10000, "ini\\content1.ini");
GetPrivateProfileString("03", "text", NULL, text3.GetBuffer(10000), 10000, "ini\\content1.ini");
GetPrivateProfileString("04", "text", NULL, text4.GetBuffer(10000), 10000, "ini\\content1.ini");
GetPrivateProfileString("05", "text", NULL, text5.GetBuffer(10000), 10000, "ini\\content1.ini");
GetPrivateProfileString("06", "text", NULL, text6.GetBuffer(10000), 10000, "ini\\content1.ini");
text1.ReleaseBuffer();
text2.ReleaseBuffer();
text3.ReleaseBuffer();
text4.ReleaseBuffer();
text5.ReleaseBuffer();
text6.ReleaseBuffer();
CString AddKeyStr = Decrypt(text1, key);
WritePrivateProfileString("01", "text", AddKeyStr, "ini\\content1.ini");
AddKeyStr = "";
AddKeyStr = Decrypt(text2, key);
WritePrivateProfileString("02", "text", AddKeyStr, "ini\\content1.ini");
AddKeyStr = "";
AddKeyStr = Decrypt(text3, key);
WritePrivateProfileString("03", "text", AddKeyStr, "ini\\content1.ini");
AddKeyStr = "";
AddKeyStr = Decrypt(text4, key);
WritePrivateProfileString("04", "text", AddKeyStr, "ini\\content1.ini");
AddKeyStr = "";
AddKeyStr = Decrypt(text5, key);
WritePrivateProfileString("05", "text", AddKeyStr, "ini\\content1.ini");
AddKeyStr = "";
AddKeyStr = Decrypt(text6, key);
WritePrivateProfileString("06", "text", AddKeyStr, "ini\\content1.ini");

//解密

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐