該算法是對稱的,因爲我可以解密每個字符和組合鍵。
#include <iostream>
using namespace std;
unsigned char enc(unsigned char ch, unsigned char key)
{
unsigned char tmp = key^(~ch+(unsigned char)0x04);
return (((tmp<<3) | (tmp>>5)) & 0xff)-0x02;
}
unsigned char dec(unsigned char ch, unsigned char key)
{
unsigned char tmp = (ch+0x02);
tmp = ((tmp>>3) | (tmp<<5)) & 0xff;
return ~((tmp^key)-(unsigned char)0x04);
}
int main()
{
// single encryption test
char c = 'A';
char key = 'K';
cout << "single test:" << (char)enc(c, key) << endl;
bool problem = false;
int k, ch;
for(k=0;k<256;k++)
{
for(ch=0;ch<256;ch++)
{
if(enc(dec((unsigned char)ch, (unsigned char)k), k) != ch)
{
problem = true;
cout << "error k=" << k << "c=" << ch
<< "result=" << (unsigned int)enc(dec((unsigned char)ch, (unsigned char)key), (unsigned char)key) << endl;
}
}
}
if(problem) cout << "There was a problem." << endl;
else cout << "There was no problem." << endl;
}
來源
2013-07-25 15:14:22
nio
你有什麼具體的* *問題:
這兩個職能由一個循環,解密值回測試的任何錯誤? 「幫助我」並不是那麼具體。 –
問題在哪裏?你有測試數據嗎? – nio
可能重複[如何解密此加密程序?](http://stackoverflow.com/questions/11677101/how-can-i-decrypt-this-encryption-routine) – harold