📄 accesspacket.java
字号:
package org.radiusClient.com;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import javax.microedition.io.UDPDatagramConnection;
public class AccessPacket {
public static final int MIN_PACKET_LENGTH = 20;
public static final int MAX_PACKET_LENGTH = 4096;
public static final short RADIUS_HEADER_LENGTH = 20;
public static final String EMPTYSTRING = "";
String encoding = new String("ISO-8859-1");
private int packet_size;
public byte request[]=new byte[MAX_PACKET_LENGTH];
byte buf[] = new byte[5001];
private static int start=0;
String local_host_IP;
public AccessPacket(int id)
{
request[0]=(byte)1;
request[1]=(byte)id;
start = 20;
}
public synchronized byte[] composepacket(String username,String userpass,String secret,UDPDatagramConnection udpdc)
{
byte RA[] = new byte[16];
byte[] requestbuffer;
Random rand = new Random();
for (int i = 0; i < 16; i++) {
RA[i] = (byte) (rand.nextInt() % 256);
request[4 + i] = RA[i];
}
int start = 20; // attribute start byte
request[start] = (byte) 1; // attribute type for username
request[start + 1] = (byte) (username.length() + 2);
byte username_arr[] = null;
try {
username_arr = username.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int username_len = username.length();
System.arraycopy(username_arr, 0, request, start + 2, username_len);
start = start + 2 + username.length();
// b_hash = MD5(S+RA)
int sec_len = secret.length();
byte[] S = new byte[sec_len];
try {
S = secret.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b = new byte[sec_len + 16];
System.arraycopy(S, 0, b, 0, sec_len);
System.arraycopy(RA, 0, b, sec_len, 16);
byte[] b_hash = getKeyedDigest(b);
// c = p XOR b_hash
int userpass_len = userpass.length();
byte[] p = new byte[16];
byte[] p_temp = new byte[userpass_len];
try {
p_temp = userpass.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.arraycopy(p_temp, 0, p, 0, userpass_len);
for (int i = userpass_len; i < 16; i++) {
p[i] = 0;
}
byte[] c1 = new byte[16];
for (int i = 0; i < 16; i++) {
c1[i] = (byte) ((int) p[i] ^ (int) b_hash[i]);
}
request[start] = (byte) 2; // attribute type for password
request[start + 1] = (byte) 18;
System.arraycopy(c1, 0, request, start + 2, 16);
start = start + 18;
request[start] = (byte) 4;
request[start + 1] = (byte) 6;
// local_host_IP = InetAddress.getLocalHost().toString();
try {
local_host_IP = udpdc.getLocalAddress();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int pos_slash = local_host_IP.indexOf("/");
local_host_IP = local_host_IP.substring(pos_slash + 1, local_host_IP
.length());
byte[] byte_IP_Address = new byte[4];
byte_IP_Address = String_to_ipaddr(local_host_IP);
System.arraycopy(byte_IP_Address, 0, request, start + 2, 4);
start = start + 6;
packet_size = start; // packet length
if (packet_size < 256) {
request[2] = (byte) 0;
}
request[3] = (byte) start;
requestbuffer = new byte[packet_size];
for (int i = 0; i < packet_size; i++) {
requestbuffer[i] = request[i];
}
return requestbuffer;
}
public byte[] getKeyedDigest(byte[] buffer) {
int i;
byte[] md5buf = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(buffer, 0, buffer.length);
try {
i = md5.digest(buf, 0, 5000);
md5buf = new byte[i];
for (int j = 0; j < i; j++) {
md5buf[j] = buf[j];
}
} catch (DigestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
}
return md5buf;
}
public synchronized int String_to_int(String str) {
int x = Integer.parseInt(str);
return x;
}
public synchronized byte[] String_to_ipaddr(String IP_Address) {
byte[] byte_IP_Address = new byte[4];
String dot = new String(".");
int pos_dot1 = 0, pos_dot2 = 0, pos_dot3 = 0;
pos_dot1 = IP_Address.indexOf(dot, 0);
pos_dot2 = IP_Address.indexOf(dot, pos_dot1 + 1);
pos_dot3 = IP_Address.indexOf(dot, pos_dot2 + 1);
if (pos_dot1 != -1 && pos_dot2 != -1 && pos_dot3 != -1) {
byte_IP_Address[0] = (byte) String_to_int(IP_Address.substring(0,
pos_dot1));
byte_IP_Address[1] = (byte) String_to_int(IP_Address.substring(
pos_dot1 + 1, pos_dot2));
byte_IP_Address[2] = (byte) String_to_int(IP_Address.substring(
pos_dot2 + 1, pos_dot3));
byte_IP_Address[3] = (byte) String_to_int(IP_Address.substring(
pos_dot3 + 1, IP_Address.length()));
}
return byte_IP_Address;
}
public synchronized int byte_int0255(byte x_byte) {
int x_int = x_byte & 0xFF;
return x_int;
}
public void setAttribute(int type,String str)
{
request[start] = (byte) type; // attribute type for username
request[start + 1] = (byte) (str.length() + 2);
byte str_arr[] = null;
try {
str_arr = str.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int str_len = str.length();
System.arraycopy(str_arr, 0, request, start + 2, str_len);
start = start + 2 + str.length();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -