求教大神关于C++转C#的有关问题
求教大神关于C++转C#的问题
我有下面一段C++代码:
我想把这段代码翻译成C#的,但是我翻译出来的经过计算得出的值和C++的值不一样,求大神指教,我的代码如下:
------解决思路----------------------
封装成DLL,再调用
我有下面一段C++代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include "global.h"
#include "md5.h"
//byte userByte[32];
//byte RADIUS[32];
//getPIN input:userName output:PINwithUserName
void getPIN(byte *userName,byte *PIN) {
//var
int i;//循环变量
long timedivbyfive;//时间除以五
time_t timenow;//当前时间,从time()获得
byte RADIUS[16];//凑位字符
byte timeByte[4];//时间 div 5
byte beforeMD5[32];//时间 div 5+用户名+凑位
MD5_CTX md5;//MD5结构体
byte afterMD5[16];//MD5输出
byte MD501H[2]; //MD5前两位
byte MD501[3];
byte timeHash[4]; //时间div5经过第一次转后后的值
byte temp[32]; //第一次转换时所用的临时数组
byte PIN27[6]; //PIN的2到7位,由系统时间转换
//code
memcpy(RADIUS, "chongqingradius1", 16);
timenow = time(NULL);
printf("%I64d\n", timenow);
timedivbyfive = timenow / 5;
printf("%d\n", timedivbyfive);
printf("%s\n", RADIUS);
printf("%s\n", userName);
for(i = 0; i < 4; i++)
{
timeByte[i] = (byte)(timedivbyfive >> (8 * (3 - i)) & 0xFF);
printf("%02x",timeByte[i]);
}
printf("\n");
for(i = 0; i < 4; i++)
{
beforeMD5[i]= timeByte[i];
}
for(i = 4; i < 16; i++)
{
beforeMD5[i] = userName[i-4];
}
for(i = 16; i < 32; i++)
{
beforeMD5[i] = RADIUS[i-16];
}
printf("gjh %s\n",beforeMD5);
MD5Init(&md5);
MD5Update (&md5, beforeMD5, 32);
MD5Final (afterMD5, &md5);
printf("after %c\n", afterMD5[1]);
MD501H[0] = afterMD5[0] >> 4 & 0xF;
MD501H[1] = afterMD5[0] & 0xF;
printf("after %x%x\n", MD501H[0], MD501H[1]);
sprintf(MD501,"%x%x",MD501H[0],MD501H[1]);
for(i = 0; i < 32; i++)
{
temp[i] = timeByte[(31 - i) / 8] & 1;
timeByte[(31 - i) / 8] = timeByte[(31 - i) / 8] >> 1;
}
for (i = 0; i < 4; i++)
{
timeHash[i] = temp[i] * 128 + temp[4 + i] * 64 + temp[8 + i]
* 32 + temp[12 + i] * 16 + temp[16 + i] * 8 + temp[20 + i]
* 4 + temp[24 + i] * 2 + temp[28 + i];
}
printf("%02x%02x%02x%02x\n",timeHash[0],timeHash[1],timeHash[2],timeHash[3]);
temp[1] = (timeHash[0] & 3) << 4;
temp[0] = (timeHash[0] >> 2) & 0x3F;
temp[2] = (timeHash[1] & 0xF) << 2;
temp[1] = (timeHash[1] >> 4 & 0xF) + temp[1];
temp[3] = timeHash[2] & 0x3F;
temp[2] = ((timeHash[2] >> 6) & 0x3) + temp[2];
temp[5] = (timeHash[3] & 3) << 4;
temp[4] = (timeHash[3] >> 2) & 0x3F;
printf("%02x%02x%02x%02x%02x%02x\n",temp[0],temp[1],temp[2],temp[3],temp[4],temp[5]);
for (i = 0; i < 6; i++)
{
PIN27[i] = temp[i] + 0x020;
if(PIN27[i]>=0x40)
{
PIN27[i]++;
}
printf("%c",PIN27[i]);
}
printf("\n");
PIN[0] = '\r';
PIN[1] = '\n';
memcpy(PIN+2, PIN27, 6);
PIN[8] = MD501[0];
PIN[9] = MD501[1];
memcpy(PIN+10, userName, 19);
}
int main() {
byte *userName = "18966047335@ZSD.XY";
byte PIN[30] = {0};
getPIN(userName,PIN);
printf("%s",PIN);
getchar();
}
我想把这段代码翻译成C#的,但是我翻译出来的经过计算得出的值和C++的值不一样,求大神指教,我的代码如下:
public class PIN
{
string username;
string password;
public string getPIN(char[] userName, char[] pwd)
{
//var
int i;//循环变量
long timedivbyfive;//时间除以五
Int64 timenow;//当前时间,从time()获得
char[] RADIUS = new char[16];//凑位字符
char[] timeByte = new char[4];//时间 div 5
char[] beforeMD5 = new char[32];//时间 div 5+用户名+凑位
//MD5_CTX md5;//MD5结构体
char[] afterMD5 = new char[16];//MD5输出
char[] MD501H = new char[2]; //MD5前两位
char[] MD501 = new char[3];
char[] timeHash = new char[4]; //时间div5经过第一次转后后的值
char[] temp = new char[32]; //第一次转换时所用的临时数组
char[] PIN27 = new char[6]; //PIN的2到7位,由系统时间转换
//code
string s = "chongqingradius1";
RADIUS = s.ToCharArray();
DateTime timeStamp = new DateTime(1970, 1, 1); //得到1970年的时间戳
timenow = (DateTime.UtcNow.Ticks - timeStamp.Ticks) / 10000000; //获取秒数
timedivbyfive = timenow / 5;
timedivbyfive = 284231193;
for (i = 0; i < 4; i++)
{
timeByte[i] = (char)(timedivbyfive >> (8 * (3 - i)) & 0xFF);
}
for (i = 0; i < 4; i++)
{
beforeMD5[i] = timeByte[i];
}
for (i = 4; i < 16; i++)
{
beforeMD5[i] = userName[i - 4];
}
for (i = 16; i < 32; i++)
{
beforeMD5[i] = RADIUS[i - 16];
}
afterMD5=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(new string(beforeMD5), "MD5").ToLower().Substring(8, 16).ToCharArray();
MD501H[0] = (char)(afterMD5[0] >> 4 & 0xF);
MD501H[1] = (char)(afterMD5[0] & 0xF);
return new string(MD501H);
/*for (i = 0; i < 32; i++)
{
temp[i] = (char)(timeByte[(31 - i) / 8] & 1);
timeByte[(31 - i) / 8] = (char)(timeByte[(31 - i) / 8] >> 1);
}
for (i = 0; i < 4; i++)
{
timeHash[i] = (char)(temp[i] * 128 + temp[4 + i] * 64 + temp[8 + i]
* 32 + temp[12 + i] * 16 + temp[16 + i] * 8 + temp[20 + i]
* 4 + temp[24 + i] * 2 + temp[28 + i]);
}
temp[1] = (char)((timeHash[0] & 3) << 4);
temp[0] = (char)((timeHash[0] >> 2) & 0x3F);
temp[2] = (char)((timeHash[1] & 0xF) << 2);
temp[1] = (char)((timeHash[1] >> 4 & 0xF) + temp[1]);
temp[3] = (char)(timeHash[2] & 0x3F);
temp[2] = (char)(((timeHash[2] >> 6) & 0x3) + temp[2]);
temp[5] = (char)((timeHash[3] & 3) << 4);
temp[4] = (char)((timeHash[3] >> 2) & 0x3F);
for (i = 0; i < 6; i++)
{
PIN27[i] = (char)(temp[i] + 0x020);
if (PIN27[i] >= 0x40)
{
PIN27[i]++;
}
}
pwd[0] = '\r';
pwd[1] = '\n';
for (i = 2; i <= 7; i++)
{
pwd[2] = PIN27[i - 2];
}
pwd[8] = MD501[0];
pwd[9] = MD501[1];
return new string(pwd);*/
}
}
------解决思路----------------------
封装成DLL,再调用