⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accountpacket.java

📁 j2me radius soket radius client
💻 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 AccountPacket {
    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 AccountPacket(int type,int id) 
    {
    	request[0]=(byte)type;
    	request[1]=(byte)id;
    	start = 20;
    }
    
    public synchronized byte[] composepacket(String username,String sid,String nasIdent,int statustype,long sunTime,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();

		// 40 记账状态 start
		
		request[start] = (byte) 40;
		request[start + 1] = (byte) 6;
		request[start + 2]=(byte) 0;
		request[start + 3]=(byte) 0;
		request[start + 4]=(byte) 0;
		request[start + 5]=(byte) statustype;
		
		start = start + 6;

		//44 会话ID “123”
		request[start]=(byte) 44;
		request[start + 1]=(byte) (sid.length() + 2);
		byte sid_arr[] = null;
		    try {
			sid_arr = sid.getBytes(encoding);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int sid_len = sid.length();
		System.arraycopy(sid_arr, 0, request, start + 2, sid_len);
		
		start = start + 2 + sid_len;
		
		//32 nas identifier "shaomxing"
		request[start]=(byte) 32;
		request[start + 1]=(byte) (nasIdent.length() + 2);
		byte nasidt_arr[] = null;
		    try {
		    	nasidt_arr = nasIdent.getBytes(encoding);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int nasidt_len = nasIdent.length();
		System.arraycopy(nasidt_arr, 0, request, start + 2, nasidt_len);
		
		start = start + 2 + nasIdent.length();
		
		//5 nasport 0
		request[start]=(byte) 5;
		request[start + 1]=(byte) 6;
		int lcport=0;
		try {
			lcport = udpdc.getLocalPort();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		request[start + 2]=(byte)(lcport & 0xff);
		request[start + 3]=(byte)((lcport >> 8) & 0xff);
		request[start + 4]=(byte)((lcport >> 16) & 0xff);
		request[start + 5]=(byte)(lcport >>> 24);
		
		start = start + 6;
		
		//46 会话时间
		if(statustype == 1)
		{
			request[start]=(byte) 46;
			request[start + 1]=(byte) 6;
			request[start + 2]=(byte)(lcport & 0xff);
			request[start + 3]=(byte)((lcport >> 8) & 0xff);
			request[start + 4]=(byte)((lcport >> 16) & 0xff);
			request[start + 5]=(byte)(lcport >>> 24);
			
		}


		packet_size = start; // packet length

		if (packet_size < 256) {
			request[2] = (byte) 0;
		}
		
		request[3] = (byte) start;
		int length=start;
		
		byte[] attribute=new byte[start-20];
		
		for(int ct=20;ct<start;ct++)
		{
			attribute[ct-20]=request[ct];
		}
		
		//Request Authenticator
		MessageDigest md5MessageDigest;
		try {
			md5MessageDigest = MessageDigest.getInstance("MD5");
			md5MessageDigest.reset();

			byte[] codebuf=new byte[]{4};
			byte[] idbuf=new byte[]{0};
			byte[] lbuf=new byte[]{(byte) (length >> 8)};
			byte[] ltbuf=new byte[]{(byte)(length & 0xff)};
	        byte [] requestAuthenticator = new byte [16];
	        String secrets="test";
	        byte[] secretbuf=secrets.getBytes();

	        for (int i = 0; i < 16; i++) {
	                requestAuthenticator[i] = 0;
	        }
			
	        md5MessageDigest.update(codebuf,0,codebuf.length);
	        md5MessageDigest.update(idbuf,0,idbuf.length);
	        md5MessageDigest.update(lbuf,0,lbuf.length);
	        md5MessageDigest.update(ltbuf,0,ltbuf.length);
	        md5MessageDigest.update(requestAuthenticator, 0, requestAuthenticator.length);
	        md5MessageDigest.update(attribute, 0, attribute.length);
	        md5MessageDigest.update(secretbuf,0,secretbuf.length);

	        byte[] rbuf=new byte[512];
	        int i;
			i = md5MessageDigest.digest(rbuf, 0, 511);			

			byte[] md5buf = new byte[i];
			for (int j = 0; j < i; j++) {
				md5buf[j] = rbuf[j];
			}
			System.arraycopy(md5buf, 0, request, 4, 16);
			
		} catch (NoSuchAlgorithmException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}catch (DigestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();								
		}
		
		
		
		requestbuffer = new byte[packet_size];
		for (int i = 0; i < packet_size; i++) {
			requestbuffer[i] = request[i];
		}
		return requestbuffer;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -