简体   繁体   English

如何在C中反转以下算法

[英]How do I reverse the following algorithm in C

I have the following algorithm: 我有以下算法:

    unsigned long specialNum=0x4E67C6A7;
    unsigned int ch;
    char inputVal[]="                        AAPB2GXG";


    for(int i=0;i<strlen(inputVal);i++)
    {
        ch=inputVal[i];

        ch=ch+(specialNum*32);
        ch=ch+(specialNum/4);

        specialNum=bitXor(specialNum,ch);
    }

    int outputVal=specialNum;

The bitXor simply does the Xor operation: bitXor只是执行Xor操作:

int bitXor(int a,int b)
{
    return (a & ~b) | (~a & b);
}

Now I want to reverse The Algorithm . 现在我想逆转算法 Given the outputVal, I want to get the inputVal. 给定outputVal,我想获取inputVal。 This is what I have done till now. 这是我到目前为止所做的。 But it is not working :( 但这不起作用:(

    while(outputVal!=0x4E67C6A7)
    {
        ch=bitXor(outputVal,0x4E67C6A7);

        outputVal=outputVal-(ch*4);
        outputVal=outputVal-(ch/32);
        inputVal[i++]=(char)ch;
        if(i>32)
            break;
    }

EDIT: Ok, agreed its not possible. 编辑:好的,同意不可能。 But I wonder why is my question downvoted. 但是我想知道为什么我的问题没有被采纳。 I have followed the guidelines before posting and I dont think the question is too bad for downvoting it. 在发布之前,我已经遵循了指导原则,我认为这个问题对于投票否定来说不是太糟糕。

In the requested "reversed" algorithm you are asking for converting 4-byte number into an arbitrary-length string. 在请求的“反向”算法中,您要求将4字节数字转换为任意长度的字符串。 It is obviously not possible, since assuming you are doing this, you have invented an ultimate compression algorithm. 显然不可能,因为假设您正在这样做,那么您已经发明了一种最终的压缩算法。

The "reverse algorithm" you are asking for is impossible. 您所要求的“反向算法”是不可能的。 Your example inputVal is a character string over 30 letters long. 您的示例inputVal是一个长度超过30个字母的字符串。 Your outputVal is 4 or 8 bytes at best. 您的outputVal为4或8个字节。 Information theory dictates that it's impossible to store any arbitrary n-byte string into a k-byte string if n > k. 信息理论指出,如果n> k,则不可能将任意的n字节字符串存储到k字节字符串中。

Your forward algorithm is basically bit-packing values from the array inputVal into the scalar variable specialNum in an awkward way. 你前进的算法基本上是位压缩从数组值inputVal入标量specialNum处于一种尴尬的方式。

Also, at the end there is an unsafe conversion from unsigned long specialNum to int outputVal . 另外,最后有从unsigned long specialNum int outputValint outputVal的不安全转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM