日期:2014-05-18  浏览次数:21202 次

将C++代码转成c#
C/C++ code
DWORD GetEncodePswd(DWORD SerialID, WORD date, BYTE key, BYTE *CapCode)
{
    DWORD           dwVal, dwTmp;
    WORD            i, wData, wSum;


    wData = (date>>9) * 32 * 16 ;
    wData += ((date>>5)&0xF) * 32 ;
    wData += (date&0x1F) ;

    dwTmp = (WORD)key;
    dwTmp *= 9999;
    dwTmp += wData;
    dwVal = dwTmp;

    for( wSum=0, i=1; i<= 6; i++, dwTmp/=10)
        wSum += (WORD)((dwTmp%10) * i) ;
  
    wSum = wSum%100;
    dwVal *= 100;
    dwVal += wSum;

    dwTmp = (DWORD)atoi(CapCode) ;     
#ifdef VER_PRC
    for( wSum=0, i=1; i<= 6; i++, dwTmp/=10)
        wSum += (WORD)((dwTmp%10) * i) ;
    wSum = wSum%100;
    dwTmp = (DWORD)atoi(CapCode) * wSum ;
#endif
    dwTmp+= (DWORD)SerialID ;           
    dwTmp &= 0xFFFFFF ;
    dwVal = dwVal ^ dwTmp;

    return dwVal;
}


------解决方案--------------------
DWORD -> int
WORD -> short
BYTE -> byte
BYTE *-> string 
atoi -> int.Parse

齐了
------解决方案--------------------
 参考如下代码:[color=#FFFFFF][img=http://p.blog.csdn.net/images/p_blog_csdn_net/zswang/reply.jpg][/img][/color]
C# code
uint GetEncodePswd(uint SerialID, ushort date, byte key, string CapCode)
{
    uint dwVal, dwTmp;
    ushort i, wData, wSum;

    wData = (ushort)((date >> 9) * 32 * 16);
    wData += (ushort)(((date >> 5) & 0xF) * 32);
    wData += (ushort)(date & 0x1F);

    dwTmp = (ushort)key;
    dwTmp *= 9999;
    dwTmp += wData;
    dwVal = dwTmp;

    for (wSum = 0, i = 1; i <= 6; i++, dwTmp /= 10)
        wSum += (ushort)((dwTmp % 10) * i);

    wSum = (ushort)(wSum % 100);
    dwVal *= 100;
    dwVal += wSum;

    dwTmp = 0;
    uint.TryParse(CapCode, out dwTmp);
    #if VER_PRC
    for (wSum = 0, i = 1; i <= 6; i++, dwTmp /= 10)
        wSum += (ushort)((dwTmp % 10) * i);
    wSum %= 100;
    uint.TryParse(CapCode, out dwTmp);
    dwTmp *= wSum;
    #endif
    dwTmp += (uint)SerialID;
    dwTmp &= 0xFFFFFF;
    dwVal = dwVal ^ dwTmp;

    return dwVal;
}
private void button1_Click(object sender, EventArgs e)
{
    Text = GetEncodePswd(1, 2, 3, "12").ToString();
}

------解决方案--------------------
C# code

 public uint GetEncodePswd(uint SerialID, ushort date, byte key, byte[] CapCode)
        {
            uint dwVal, dwTmp;
            ushort i, wData, wSum;

            wData = (ushort)((date >> 9) * 32 * 16);

            wData += (ushort)(((date >> 5) & 0xF) * 32);
            wData += (ushort)(date & 0x1F);

            dwTmp = (ushort)key;
            dwTmp *= 9999;
            dwTmp += wData;
            dwVal = dwTmp;
            for (wSum = 0, i = 1; i <= 6; i++, dwTmp /= 10)
                wSum += (ushort)((dwTmp % 10) * i);

            wSum = (ushort)(wSum % 100);
            dwVal *= 100;
            dwVal += wSum;
            dwTmp = (uint)Convert.ToUInt32(CapCode);
#if VER_PRC
    for( wSum=0, i=1; i<= 6; i++, dwTmp/=10)
        wSum += (ushort)((dwTmp%10) * i) ;
    wSum = wSum%100;
    dwTmp = (uint)atoi(CapCode) * wSum ;
#endif
            dwTmp += (uint)SerialID;
            dwTmp &= 0xFFFFFF;
            dwVal = dwVal ^ dwTmp;

            return dwVal;
        }