我最近一直在尝试对编译代码中的各种加密算法进行逆向工程,我偶然发现了这段代码。它是 RSA 算法的一部分。我注意到密钥大小太小而无法加密/解密它应该加密/解密的数据(在本例中为int
),因此代码将消息分成两部分,分别加密/解密,然后将它们加在一起。我已经提取了拆分和连接消息的代码段,并对其进行了试验。它使用的数值似乎取决于n
模数。那么,这个方案到底是什么,它是如何工作的?
uint n = 32437;
uint origVal = 12345;
uint newVal = 0;
for (int i = 0; i < 2; ++i)
{
ulong num = (ulong)origVal * 43827549;
//uint num2 = ((origVal - (uint)(num >> 32)) / 2 + (uint)(num >> 32)) >> 14;
uint num2 = (origVal + (uint)(num >> 32)) / 32768;
origVal -= num2 * n;
// RSA encrypt/decrypt here
newVal *= n;
newVal += origVal;
origVal = num2;
}
// Put newVal into origVal, to reverse
origVal = newVal;
newVal = 0;
for (int i = 0; i < 2; ++i)
{
ulong num = (ulong)origVal * 43827549;
//uint num2 = ((origVal - (uint)(num >> 32)) / 2 + (uint)(num >> 32)) >> 14;
uint num2 = (origVal + (uint)(num >> 32)) / 32768;
origVal -= num2 * n;
// RSA encrypt/decrypt here
newVal *= n;
newVal += origVal;
origVal = num2;
}
注意:似乎应用的操作是对称的。