编辑:
似乎错误出在ReadResource()
函数内。在解密之前打印输出时,代码已经被切断。
旧文:
我在我的应用程序资源中存储了一个 RC4 加密字符串。因为 RC4 加密具有最大字符串大小,所以我将大字符串拆分为子字符串并用分隔符划分它们。
现在,我的应用程序应该从资源中读取。然后使用分隔符拆分它们,解密每个子字符串并再次组合它们。
当使用'testestestestest........test'作为字符串尝试此操作时,它可以工作,但如果我使用例如'Lorem ipsum dolor sit amet,consetetur sadipscing elitr',它会在解密期间切断部分字符串。
这是 RC4 代码:
char* rc4_crypt( char *s, size_t datalen, char *d, size_t keylen)
{
unsigned char S[SIZE];
char *data = s;
char *key = d;
int i,j,k,tmp;
for(i = 0; i < SIZE;i++)
S[i] = i;
for(j = i = 0; i < SIZE;i++) {
j = (j + S[i] + key[i%keylen]) % SIZE;
tmp = S[i];
S[i] = S[j];
S[j] = tmp;
}
for(i = j = k = 0; k < datalen;k++) {
i = (i + 1) % SIZE;
j = (j + S[i]) % SIZE;
tmp = S[i];
S[i] = S[j];
S[j] = tmp;
tmp = S[(S[i] + S[j]) % SIZE];
data[k] ^= tmp;
}
return data;
}
这是处理拆分的代码:
std::vector<std::string> &split(const std::string &s,
char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
return split(s, delim, elems);
}
void print (std::string &elem)
{
//This is because somehow 'ul' is still included
//as an extra element, this sorts it out
if(elem.length() !=2) {
cout << elem << '\n';
char *output = rc4_crypt((char *)elem.c_str(),
strlen(elem.c_str()),key, strlen(key));
cout << output << '\n';
FinalString = FinalString + output;
}
}
这是我的主要功能:
int main() {
char *output;
output = ReadResource();
std::string stringy;
stringy = output;
std::vector<std::string> splitted = split(stringy,'+ul+');
for_each (splitted.begin(), splitted.end(), print);
cout << endl;
cout << FinalString << '\n';
}
有谁知道为什么会这样?
编辑:
我正在添加我在 vb.net 中使用的 RC4 函数来加密代码。也许这可以帮助:
Private Shared Function Proper_RC4(ByVal Input As Byte(), ByVal Key As Byte()) As Byte()
Dim i, j, swap As UInteger
Dim s As UInteger() = New UInteger(255) {}
Dim Output As Byte() = New Byte(Input.Length - 1) {}
For i = 0 To 255
s(i) = i
Next
For i = 0 To 255
j = (j + Key(i Mod Key.Length) + s(i)) And 255
swap = s(i) 'Swapping of s(i) and s(j)
s(i) = s(j)
s(j) = swap
Next
i = 0 : j = 0
For c = 0 To Output.Length - 1
i = (i + 1) And 255
j = (j + s(i)) And 255
swap = s(i) 'Swapping of s(i) and s(j)
s(i) = s(j)
s(j) = swap
Output(c) = Input(c) Xor s((s(i) + s(j)) And 255)
Next
Return Output
End Function
编辑2:
这是 ReadResource() 函数:
char *ReadResource() {
TCHAR buffer[MAX_PATH];
GetModuleFileName(NULL,buffer,sizeof(buffer));
HMODULE ModuleH = GetModuleHandle(buffer);
HRSRC Res = FindResource(ModuleH, L"1", L"DATA");
HGLOBAL GlobalH = LoadResource(ModuleH,Res);
void * ptr = LockResource(GlobalH);
Size = SizeofResource(ModuleH,Res);
CHAR *m_pResourceBuffer = new CHAR[Size];
if (m_pResourceBuffer != NULL)
{
memcpy(m_pResourceBuffer, ptr, Size);
}
return m_pResourceBuffer;
}