在做闪讯拨号器时,翻译代码时遇到困难,C翻JAVA,大神进来看看。代码里面有注释
C源代码:#include <stdio.h>#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <pppd/pppd.h>
#include <pppd/md5.h>
typedef unsigned char byte;
char pppd_version[] = VERSION;
static char saveuser = {0};
static char savepwd = {0};
static void getPIN(byte *userName, byte *PIN)
{
int i,j;//循环变量
long timedivbyfive;//时间除以五
time_t timenow;//当前时间,从time()获得
byte RADIUS;//凑位字符
byte timeByte;//时间 div 5
byte beforeMD5;//时间 div 5+用户名+凑位
MD5_CTX md5;//MD5结构体
byte afterMD5;//MD5输出
byte MD501H; //MD5前两位
byte MD501;
byte timeHash; //时间div5经过第一次转后后的值
byte temp; //第一次转换时所用的临时数组
byte PIN27; //PIN的2到7位,由系统时间转换
//code
info("sxplugin : using zjxinlisx01");
strcpy(RADIUS, "zjxinlisx01");
timenow = time(NULL);
timedivbyfive = timenow / 5;
for(i = 0; i < 4; i++) {
timeByte = (byte)(timedivbyfive >> (8 * (3 - i)) & 0xFF);
}
for(i = 0; i < 4; i++) {
beforeMD5= timeByte;
}
for(i = 4; i < 16 && userName!='@' ; i++) {
beforeMD5 = userName;
}
j=0;
while(RADIUS!='\0')
beforeMD5 = RADIUS;
MD5_Init(&md5);
MD5_Update (&md5, beforeMD5, i);
printf("%d %s\n",i,beforeMD5);
MD5_Final (afterMD5, &md5);
//就是这一块,翻译成JAVA时总是失败
MD501H = afterMD5 >> 4 & 0xF;
MD501H = afterMD5 & 0xF;
sprintf(MD501,"%x%x",MD501H,MD501H);//到这里
//我翻译的java代码
/* MD501H = (char) (afterMD5 >> 4 & 0xF);
MD501H = (char) (afterMD5 & 0xF);
byte MD1=(byte)(MD501H);byte MD2=(byte)(MD501H);
String MD=String.format("%x%x",MD1,MD2);
MD501=MD.toCharArray();*/
for(i = 0; i < 32; i++) {
temp = timeByte[(31 - i) / 8] & 1;
timeByte[(31 - i) / 8] = timeByte[(31 - i) / 8] >> 1;
}
for (i = 0; i < 4; i++) {
timeHash = temp * 128 + temp * 64 + temp
* 32 + temp * 16 + temp * 8 + temp
* 4 + temp * 2 + temp;
}
temp = (timeHash & 3) << 4;
temp = (timeHash >> 2) & 0x3F;
temp = (timeHash & 0xF) << 2;
temp = (timeHash >> 4 & 0xF) + temp;
temp = timeHash & 0x3F;
temp = ((timeHash >> 6) & 0x3) + temp;
temp = (timeHash & 3) << 4;
temp = (timeHash >> 2) & 0x3F;
for (i = 0; i < 6; i++) {
PIN27 = temp + 0x020;
if(PIN27>=0x40) {
PIN27++;
}
}
PIN = '\r';
PIN = '\n';
memcpy(PIN+2, PIN27, 6);
PIN = MD501;
PIN = MD501;
strcpy(PIN+10, userName);
}
{:301_999:}为什么没有人啊。。。。。。 package cn.zjun.rucksack;
import java.util.Date;
public class GetPIN {
String username;
String password;
public String getPIN(char[] userName,char[] PIN)
{
//var
int i,j = 0;//循环变量
long timedivbyfive;//时间除以五
long timenow ; //当前时间,从time()获得
char[] RADIUS = new char;//凑位字符
char[] timeByte = new char;//时间 div 5
char[] beforeMD5 = new char;//时间 div 5+用户名+凑位
//MD5_CTX md5;//MD5结构体
char[] afterMD5 = new char;//MD5输出
char[] MD501H = new char; //MD5前两位
char[] MD501 = new char;
char[] timeHash = new char; //时间div5经过第一次转后后的值
char[] temp = new char; //第一次转换时所用的临时数组
char[] PIN27 = new char; //PIN的2到7位,由系统时间转换
//code
String s = "zjxinlisx01";
RADIUS = s.toCharArray();
Date dt= new Date();
timenow=dt.getTime()/ 1000; //得到1970年的时间戳获取秒数
timedivbyfive = timenow / 5;
for (i = 0; i < 4; i++)
{
timeByte = (char) (timedivbyfive >> (8 * (3 - i)) & 0xFF);
}
for (i = 0; i < 4; i++)
{
beforeMD5 = timeByte;
}
for (i = 4; i < 16; i++)
{
beforeMD5 = userName;
}
while(j<RADIUS.length)
beforeMD5 = RADIUS;
Md5 m=new Md5();
afterMD5=(m.MD5(beforeMD5.toString())).toCharArray();
int MD5 =( afterMD5 >> 4 )& 0xF;
intMDH = afterMD5 & 0xF;
//byte MD1=(byte)(MD501H);byte MD2=(byte)(MD501H);
String MD=String.format("%x%x",MD5,MDH);
MD501=MD.toCharArray();
for (i = 0; i < 32; i++)
{
temp = (char)(timeByte[(31 - i) / 8] & 1);
timeByte[(31 - i) / 8] = (char)(timeByte[(31 - i) / 8] >> 1);
}
for (i = 0; i < 4; i++)
{
timeHash = (char)(temp * 128 + temp * 64 + temp
* 32 + temp * 16 + temp * 8 + temp
* 4 + temp * 2 + temp);
}
temp = (char)((timeHash & 3) << 4);
temp = (char)((timeHash >> 2) & 0x3F);
temp = (char)((timeHash & 0xF) << 2);
temp = (char)((timeHash >> 4 & 0xF) + temp);
temp = (char)(timeHash & 0x3F);
temp = (char)(((timeHash >> 6) & 0x3) + temp);
temp = (char)((timeHash & 3) << 4);
temp = (char)((timeHash >> 2) & 0x3F);
for (i = 0; i < 6; i++)
{
PIN27 = (char)(temp + 0x020);
if (PIN27 >= 0x40)
{
PIN27++;
}
}
PIN = '\r';
PIN = '\n';
//System.arraycopy(PIN,2,PIN27,0,6);
for(j=0;j<=5;j++){
PIN=PIN27;
}
PIN = MD501;
PIN = MD501;
for(j=0;j<userName.length;j++){
PIN=userName;
}
return new String(PIN);
}
}
附上我翻译的JAVA代码 虽然我不懂,但是我顶你
页:
[1]