天天看點

java使用JNative調用動态連接配接庫執行個體

import heidu004.domain.EmployeeInfo;

import heidu004.persistence.EmployeeInfoDAOImpl;

import java.io.UnsupportedEncodingException;

import java.sql.SQLException;

import org.xvolks.jnative.JNative;

import org.xvolks.jnative.Type;

import org.xvolks.jnative.exceptions.NativeException;

import org.xvolks.jnative.pointers.Pointer;

import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;

public class Dll {

  public static final Integer EncryptPswStr(String PswStr, Pointer po,Integer PswLen,Integer BuffLen) throws NativeException, IllegalAccessException{

  System.loadLibrary("WebInterface");//我用的是方法一:WebInterface指的是動态連結庫名

  JNative n = null;

  n = new JNative("WebInterface","EncryptPswStr");

  n.setRetVal(Type.INT);//傳回類型整型Type.INT  

  int i =0;

  n.setParameter(i++,PswStr);//方法的了内部參數PswStr 為字元(Type.STRING)類型 

  n.setParameter(i++,po);//方法的了内部參數po 為字元(Type.STRING)類型    

  n.setParameter(i++,PswLen);//方法的了内部參數PswLen 為字元(Type.STRING)類型

  n.setParameter(i++,BuffLen);//方法的了内部參數BuffLen 為字元(Type.STRING)類型

  n.invoke();//加載   

  return Integer.parseInt(n.getRetVal());  

    }

  public static final Integer DecryptPswStr(String PswStr, Pointer po,Integer PswLen,Integer BuffLen) throws NativeException, IllegalAccessException{ 

  System.loadLibrary("WebInterface");//WebInterface指的是動态連結庫名

  JNative n = null;

  n = new JNative("WebInterface","DecryptPswStr");//動态連接配接庫内部方法

  n.setRetVal(Type.INT);//傳回類型整型Type.INT  

  int i =0;

  n.setParameter(i++,PswStr);//方法的了内部參數PswStr 為字元(Type.STRING)類型 

  n.setParameter(i++,po);//方法的了内部參數po 為字元(Type.STRING)類型    

  n.setParameter(i++,PswLen);//方法的了内部參數PswLen 為字元(Type.STRING)類型

  n.setParameter(i++,BuffLen);//方法的了内部參數BuffLen 為字元(Type.STRING)類型

  n.invoke();//加載    

  return Integer.parseInt(n.getRetVal());  

  }

  private static String hex16Str(String theHex) {  

         String theRst = "";  

         byte[] theByte = new byte[theHex.length() / 2];  

         try {  

             for (int i = 0; i < theHex.length(); i += 2) {  

                 theByte[i / 2 ] = Integer.decode("0X"+  

                         theHex.substring(i, i + 2)).byteValue();  

             }  

             theRst = new String(theByte, 0, theByte.length, "Shift_JIS"); //Shift_JIS日文編碼表 

         } catch (Exception Ue) {  

             Ue.printStackTrace();  

         }  

         return theRst;  

     }  

  private static String str16Hex(String theStr) {  

         byte[] bytes;  

         String result = "";  

         int tmp;  

         String tmpStr;  

         try {  

             bytes = theStr.getBytes("Shift_JIS"); //Shift_JIS日文編碼表 

             for (int i = 0; i < bytes.length; i++) {  

                 if (bytes[i] < 0) {  

                     tmp = 256 + bytes[i];  

                     tmpStr = Integer.toHexString(tmp).toUpperCase();  

                     result += tmpStr;  

                 } else {  

                     tmpStr = Integer.toHexString(bytes[i]).toUpperCase();                         

                     result += tmpStr.length()==1?"0"+tmpStr:tmpStr;  

                 }  

             }  

         } catch (UnsupportedEncodingException e) {  

             // TODO Auto-generated catch block  

             e.printStackTrace();  

         }  

         return result;  

     }    

  public static void main(String[] args) throws NativeException, IllegalAccessException, SQLException {

  EmployeeInfoDAOImpl eImpl=new EmployeeInfoDAOImpl();//自定義的一個類用于查詢驗證密碼

  String pws="";  

  EmployeeInfo emp=eImpl.selectEmployeeInfo("888");//使用者名888傳回十六進制編碼 

  System.out.println("十六進制編碼:0x"+emp.getPass()); 

  pws=hex16Str(emp.getPass());  

  System.out.println("字元串組合編碼"+pws);

  Pointer po1=new Pointer(MemoryBlockFactory.createMemoryBlock(pws.length()));//定義java指針用于解決變參的傳遞

  Integer PswLen=pws.length();

  Integer BuffLen=pws.length();  

  Dll.DecryptPswStr(pws, po1, PswLen, BuffLen);//解密密碼字元串

        System.out.println("解密密碼字元串:"+po1.getAsString());  

        Dll.EncryptPswStr(po1.getAsString(), po1, PswLen, BuffLen);//加密密碼字元串

  System.out.println("生成字元串組合編碼:"+po1.getAsString());

  System.out.println("字元串轉換十六進制編碼:0x"+str16Hex(po1.getAsString()));   

  }

}