chengyinqing/密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.01/DraftEncrypt.java
2026-06-15 17:59:34 +08:00

656 lines
18 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.sunyard.encrypt;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Properties;
//#define ERR_CONNECT 9001 /*联机错误*/
//#define ERR_SEND 9002 /*发送错误*/
//#define ERR_RECV 9003 /*接收错误*/
//#define ERR_CAL 9004 /*加押错误*/
//#define ERR_CHECK 9005 /*核押错误*/
//#define ERR_SIGN 9006 /*核对签名错误*/
//#define ERR_SENDER 9007 /*发报行标识错误*/
//#define ERR_RECEIVER 9008 /*收报行标识错误*/
//#define ERR_EXCEPTION 9009 /*加密卡处理异常*/
//#define ERR_DATA 9010 /*数据报文错误*/
//#define ERR_TIMEOUT 9011 /*网络通信超时*/
//#define ERR_PURVIEW 9012 /*发报行无编押权限*/
public class DraftEncrypt {
String ServerName1; /*密押服务器地址1*/
String ServerName2; /*密押服务器地址2*/
Socket sock_check;
int m_TimeOut;
int m_MyLoopNum;
int m_MyLoopFlag;
int m_port = 8889;
String m_CipherValue;
public DraftEncrypt() throws IOException {
sock_check = null;
m_TimeOut=10;
m_MyLoopNum=0;
m_MyLoopFlag=0;
m_CipherValue = null;
m_port = 8889;
}
public String getCipherValue() {
return m_CipherValue;
}
public void setCipherValue(String cipherValue) {
m_CipherValue = cipherValue;
}
public String config(String appName) {
InputStream is = this.getClass().getClassLoader().getResourceAsStream(
"config.ini");
Properties p = new Properties();
try {
p.load(is);
} catch (IOException e) {
e.printStackTrace();
}
return p.getProperty(appName);
}
public void CloseConnect_MY() throws IOException
{
if(sock_check!=null) {
//System.out.println("sock:"+sock_check);
sock_check.shutdownOutput();
sock_check.close();
sock_check = null;
}
if(m_MyLoopFlag==0)
m_MyLoopFlag=1;
else
m_MyLoopFlag=0;
}
public int DraftEncrypt(int TransactType,char YwType,
String Date,String SerialNo,String Amount,
String SendNode, String RecvNode, String OtherInfo,
String CipherValue,String My_IPAdress1,
String My_IPAdress2,int TimeOut) throws IOException
{
int len = OtherInfo.length();
if(len<8){
OtherInfo = addZero(OtherInfo, 8);
}else{
if(len%2 == 1){
len+=1;
}
OtherInfo = addZero(OtherInfo, len);
}
/*char[] byteOther = getSerialNo(OtherInfo);
for(int i =0 ;i<byteOther.length;i++){
System.out.println("jjjjj"+byteOther[i]+"ijjjj");
}
OtherInfo = new String(byteOther);*/
if(OtherInfo.length()>=8){
char[] hex1=getSerialNo(OtherInfo);
System.out.println(hex1);
String ssss = "";
for(int i=0;i<4;i++){
ssss=ssss+hex1[i];
}
OtherInfo=ssss;
}
System.out.println(BytesToString(OtherInfo.getBytes())+OtherInfo.length());
int y =OtherInfo.charAt(0);
char a = 0x88;
byte a1 = (byte) a;
int a2 = a;
System.out.println(y);
SetParamAll(My_IPAdress1,My_IPAdress2,TimeOut);
// System.out.println("in DraftEncrypt, hello!!!");
// m_CipherValue = "1234567890";
// return 0;
return DraftEncrypt1(TransactType,YwType,Date,SerialNo,Amount,SendNode,RecvNode,OtherInfo,CipherValue);
}
public int DraftEncrypt1(int TransactType,char YwType,String Date,String SerialNo,String Amount,String SendNode,String RecvNode,String OtherInfo,String CipherValue) throws IOException
{
int ret_code ;
ret_code = CipherTransact(TransactType, YwType, Date, SerialNo, Amount, SendNode, RecvNode, OtherInfo, CipherValue);
if((ret_code == 9001) || (ret_code == 9002) || (ret_code == 9003)
|| (ret_code == 9011) || (ret_code == 9009))
{
CloseConnect_MY();
// 如 果第一个密押服务器通讯错误则自动用第二个密押服务器的IP地址
ret_code = CipherTransact(TransactType, YwType, Date, SerialNo, Amount, SendNode, RecvNode, OtherInfo, CipherValue);
return ret_code;
}
else
{
return ret_code;
}
}
public int SetParamAll(String My_IPAdress1,String My_IPAdress2,int TimeOut)
{
int ret;
ret=My_IPAdress1.length();
if(ret>99)
return -1;
ServerName1=My_IPAdress1;
ret=My_IPAdress2.length();
if(ret>99)
return -2;
ServerName2=My_IPAdress2;
if(TimeOut<2)
m_TimeOut=2;
else if(TimeOut>120)
m_TimeOut=120;
else
m_TimeOut=TimeOut;
return 0;
}
private static char[] enlargechar(char[] summand, String addend, int from) {
int len = addend.length();
for (int i = 0; i < len; i++) {
summand[from + i] = addend.charAt(i);
}
return summand;
}
public Socket testIP(String ip, int port) throws NumberFormatException, UnknownHostException, IOException {
SocketAddress ipt;
Socket socket1;
try {
System.out.print("IP:"+ip+" port:"+port);
ipt = new InetSocketAddress(ip, port);
socket1 = new Socket();
socket1.connect(ipt, 1000);
if (socket1.isClosed() == false && socket1.isConnected() == true)
return socket1;
} catch (ConnectException e) {
System.out.println("注意:"+ip+ "连接异常!");
} catch (SocketTimeoutException e) {
System.out.println("注意:"+ip+ "Socket连接超时异常");
}
return null;
}
public BufferedOutputStream getBufferedWriter(Socket socket) throws IOException {
OutputStream out = socket.getOutputStream();
BufferedOutputStream writer = new BufferedOutputStream(out);
return writer;
}
public BufferedInputStream getBufferedRead(Socket socket) throws IOException {
InputStream in = socket.getInputStream();
BufferedInputStream reader = new BufferedInputStream(in);
return reader;
}
private byte[] getBytes (char[] chars) {
Charset cs = Charset.forName ("UTF-8");
CharBuffer cb = CharBuffer.allocate (chars.length);
cb.put (chars);
cb.flip ();
ByteBuffer bb = cs.encode (cb);
return bb.array();
}
private char[] getChars (byte[] bytes) {
Charset cs = Charset.forName ("UTF-8");
ByteBuffer bb = ByteBuffer.allocate (bytes.length);
bb.put (bytes);
bb.flip ();
CharBuffer cb = cs.decode (bb);
return cb.array();
}
private char[] getSerialNo(String s){
char hex[] =new char[8];
char ch ;
for(int i=0;i<s.length();i++){
ch=s.charAt(i);
if(ch>='0'&&ch<='9'){
ch-='0';
}
if(ch>='A'&&ch<='F'){
ch=(char) (ch-'A');
ch=(char) (ch+10);
}
if(i%2==0)
{
hex[i/2]=(char) (ch<<4);
}
else
{
hex[i/2]=(char) (hex[i/2]|ch);
}
}
return hex;
}
public int CipherTransact(int TransactType,char YwType,String Date,String SerialNo,String Amount,String SendNode,String RecvNode,String OtherInfo,String CipherValue) throws NumberFormatException, UnknownHostException, IOException
{
// unsigned char Send_Data[2000],Recv_Data[2000];
char Send_Data[] = new char[180];
char Recv_Data[] = new char[2000];
String temp;
int Len_No,Len_Send;
// int i;
// unsigned int my_value;
//Package
// memset(Send_Data,0x00,180);
int s =0;
for (int j = 0; j < Send_Data.length; j++) {
Send_Data[j] = 0x00;
}
// memcpy(Send_Data,"SYD",3);
Send_Data[0]='S';
Send_Data[1]='Y';
Send_Data[2]='D';
Send_Data[3]=1;
Send_Data[4]=(char)(m_MyLoopNum/256);
Send_Data[5]=(char)(m_MyLoopNum%256);
m_MyLoopNum=(m_MyLoopNum+1)%65536;
Send_Data[6]=0;
Send_Data[7]=124+32+5;
if(SerialNo.length()==16){
s =18000+TransactType;
char[] hex=getSerialNo(SerialNo);
String ss = "";
for(int i=0;i<hex.length;i++){
ss=ss+hex[i];
}
SerialNo=ss;
}else{
s =16000+TransactType;
}
Send_Data = enlargechar(Send_Data, s+"", 40);
// int s =16000+TransactType;
// Send_Data = enlargechar(Send_Data, s+"", 40);
// if(TransactType==0)
// Send_Data = enlargechar(Send_Data, "16000", 40);
//// memcpy(Send_Data+8+32,"16000",5);
// else if(TransactType==1)
// Send_Data = enlargechar(Send_Data, "16001", 40);
//// memcpy(Send_Data+8+32,"16001",5);
// else
// Send_Data = enlargechar(Send_Data, "16002", 40);
//// memcpy(Send_Data+8+32,"16002",5);
// memcpy(Send_Data+8+32+5,&YwType,1);
Send_Data[45] = YwType;
// memcpy(Send_Data+8+32+6,Date,8);
Send_Data = enlargechar(Send_Data, Date, 46);
// memcpy(Send_Data+8+32+14,SerialNo,8);
Send_Data = enlargechar(Send_Data, SerialNo, 54);
// memcpy(Send_Data+8+32+22,Amount,15);
Send_Data = enlargechar(Send_Data, Amount, 62);
// Len_No=strlen(SendNode);
// if(Len_No>12)
// Len_No=12;
// memcpy(Send_Data+8+32+37+(12-Len_No),SendNode,Len_No);
Len_No = SendNode.length();
if(Len_No>12){
Len_No=12;
SendNode = SendNode.substring(0,12);
}
Send_Data = enlargechar(Send_Data, SendNode, 77+(12-Len_No));
// Len_No=strlen(RecvNode);
// if(Len_No>12)
// Len_No=12;
// memcpy(Send_Data+8+32+49+(12-Len_No),RecvNode,Len_No);
Len_No = RecvNode.length();
if(Len_No>12){
Len_No=12;
RecvNode = RecvNode.substring(0,12);
}
Send_Data = enlargechar(Send_Data, RecvNode, 89+(12-Len_No));
// memcpy(Send_Data+8+32+61,OtherInfo,60);
//ding
// if(SerialNo.length()==16){
// s =18000+TransactType;
// char[] hex=getSerialNo(SerialNo);
// String ss = "";
// for(int i=0;i<hex.length;i++){
// ss=ss+hex[i];
// }
// SerialNo=ss;
// }
/*else
{
StringBuffer ddbuf = new StringBuffer();
int ddlen = OtherInfo.length();
int ddtmp = 8 - ddlen ;
for(int i=0;i<ddtmp;i++){
ddbuf.append("0");
}
ddbuf.append(OtherInfo);
String sss = ddbuf.toString() ;
OtherInfo = sss;
char[] hex1=getSerialNo(OtherInfo);
String ss = "";
for(int i=0;i<OtherInfo.length()/2+1;i++){
ss=ss+hex1[i];
}
OtherInfo=ss;
}*/
//ding
Send_Data = enlargechar(Send_Data, OtherInfo, 101);
if(TransactType%2==1)
{
Long value;
value = Long.parseLong(CipherValue);
temp = Long.toHexString(value).toUpperCase();
// System.out.println("temp:1:" + temp);
if (temp.length()!= 8)
temp = stradd0(temp,8);
//System.out.println("temp:2:" + temp);
Send_Data = enlargechar(Send_Data, temp, 161);
}
Len_Send=8+32+5+124;
// System.out.println("Send_Data:"+Send_Data.toString());
// System.out.println("zubao hou..."+Len_Send);
//SOCKET_PACKET_180
Len_Send=180;
// System.out.println("Send_Data:"+Send_Data);
// System.out.println("Send_Data FF:"+Send_Data);
//for (int i=0; i<Send_Data.length; i++) {
//System.out.println("i:"+i+":"+Send_Data[i]);
//}
//byte a[]= getBytes(Send_Data);
StringBuffer str = new StringBuffer();
// System.out.println("size:"+Send_Data.length);
for (int i=0; i<Send_Data.length; i++) {
str.append(Send_Data[i]);
// System.out.println("i:"+i+":"+Send_Data[i]);
}
// for(int j=0;j<Send_Data.length;j++){
// if(j%10==0)
// System.out.println();
// System.out.print(Send_Data[j]);
// }
byte a[] =str.toString().getBytes("UTF-8");
/*System.out.println(a.length);
System.out.println(new String(a));*/
byte data[] = new byte[Len_Send];
for(int i=0,j=0; i <a.length;i++){
if( a[i]== -62 ){
System.out.println(i);
;
}
else{
data[j]=a[i];
j++;
}
}
if(sock_check==null){
if(m_MyLoopFlag==0) {
sock_check = testIP(ServerName1, m_port);
if(sock_check==null){
sock_check = testIP(ServerName2,m_port);
if(sock_check==null){
System.out.println("连接支付密码服务器失败!");
return 9001;
}
}
}
else{
sock_check = testIP(ServerName2,m_port);
if(sock_check==null){
sock_check = testIP(ServerName2,m_port);
if(sock_check==null){
System.out.println("连接支付密码服务器失败!");
return 9001;
}
}
}
}
sock_check.setSendBufferSize(32767);
sock_check.setReceiveBufferSize(32767);
sock_check.setSoTimeout(10000);
try {
BufferedOutputStream bos = getBufferedWriter(sock_check);
System.out.println(BytesToString(data));
bos.write(data);
bos.flush();
byte[] rebound = new byte[Len_Send];
BufferedInputStream bis = getBufferedRead(sock_check);
int result = bis.read(rebound);
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
if (result == Len_Send) {
Recv_Data = getChars(rebound);
// for(int i=0;i<180;i++){
// System.out.println(Recv_Data[i]);
// }
// System.out.println("------");
// System.out.println(Recv_Data[42]);
// System.out.println(Recv_Data[43]);
// System.out.println(Recv_Data[44]);
// System.out.println(Recv_Data[45]);
} else {
CloseConnect_MY();
return 9003;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("ERROR:"+e.getMessage());
return 9001;
} finally {
//System.out.println("关闭SOCK");
//CloseConnect_MY();
}
if(!(Recv_Data[42]=='0'&&Recv_Data[43]=='0'&&Recv_Data[44]=='0'&&Recv_Data[45]=='0')){
if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='0'&&Recv_Data[45]=='2')
return (9007);
else if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='0'&&Recv_Data[45]=='3') {
System.out.println("ERROR-NO");
return (9008);
}
else if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='0'&&Recv_Data[45]=='9')
return (9009);
else if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='0'&&Recv_Data[45]=='4')
return (9010);
else if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='0'&&Recv_Data[45]=='1')
return(9005);
else if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='0'&&Recv_Data[45]=='5')
return(9012);
else if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='0'&&Recv_Data[45]=='6')
return(9004);
else if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='1'&&Recv_Data[45]=='0')
return(9014);
else if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='1'&&Recv_Data[45]=='1')
return(9013);
else if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='1'&&Recv_Data[45]=='2')
return(9015);
else if(Recv_Data[42]=='0'&&Recv_Data[43]=='3'&&Recv_Data[44]=='1'&&Recv_Data[45]=='3')
return(9016);
else
return (9010);
}
// System.out.println("Recv_Data:"+Recv_Data);
// System.out.println(TransactType);
if(TransactType%2 == 0)
{
StringBuffer temp1 = new StringBuffer();
for (int i=46; i<54; i++) {
temp1.append(Recv_Data[i]);
}
temp = temp1.toString();
Long iCipherValue;
//ding_System.out.println("temp:"+temp);
iCipherValue = Long.parseLong(temp, 16);
m_CipherValue = Long.toString(iCipherValue);
//如果小于10位前面补0
for(;m_CipherValue.length()<10;){
m_CipherValue = "0"+m_CipherValue;
}
// if (m_CipherValue.length()<10) {
// m_CipherValue = "0"+m_CipherValue;
// }
return 0;
}
return 0;
}
private static String stradd0(String str, int n) {
StringBuffer so = new StringBuffer();
for (int i = 0; i < (n - str.length()); i++) {
so.append("0");
}
so.append(str);
str = so.toString();
return str;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// System.out.println("hello");
// Long x;
// x = Long.parseLong("3663412897");
//
// String str = Long.toHexString(x);
// str = DraftEncrypt.stradd0(str, 8).toUpperCase();
// System.out.println(str.substring(0, 8));
//
// String q = "DA5B3EA1";
// Long qq = Long.parseLong(q, 16);
// System.out.println(qq);
// DraftEncrypt de = new DraftEncrypt();
// int ret = de.DraftEncrypt(0, (char) 0x03, "20120825", "10000176", "000000000004400",
// "001410122012", "001000000016", "", "", "192.168.1.212", "192.168.1.212", 100);
// System.out.println("ret="+ret+" IP:"+de.ServerName1+", "+de.getCipherValue());
}
private String addZero(String str,int lenTotal){
String strAns = str;
int len = str.length();
for(int i=0;i<lenTotal-len;i++){
strAns = strAns+"0";
}
return strAns;
}
private String BytesToString(byte[] bArray) {
if (bArray == null) {
return null;
}
if (bArray.length == 0) {
return "";
}
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/**
*
*
* @param data
* "00AF"
* @return 0x00, 0xAF
*/
private static byte[] StringToBytes(String data) {
String hexString = data.toUpperCase().trim();
if (hexString.length() % 2 != 0) {
return null;
}
byte[] retData = new byte[hexString.length() / 2];
for (int i = 0; i < hexString.length(); i++) {
int int_ch; // 锟斤拷位16锟斤拷锟斤拷锟斤拷转锟斤拷锟斤拷锟<E68BB7>0锟斤拷锟斤拷锟斤拷
char hex_char1 = hexString.charAt(i); // //锟斤拷位16锟斤拷锟斤拷锟斤拷锟叫的碉拷一位(锟斤拷位*16)
int int_ch1;
if (hex_char1 >= '0' && hex_char1 <= '9')
int_ch1 = (hex_char1 - 48) * 16; // // 0 锟斤拷Ascll - 48
else if (hex_char1 >= 'A' && hex_char1 <= 'F')
int_ch1 = (hex_char1 - 55) * 16; // // A 锟斤拷Ascll - 65
else
return null;
i++;
char hex_char2 = hexString.charAt(i); // /锟斤拷位16锟斤拷锟斤拷锟斤拷锟叫的第讹拷位(锟斤拷位)
int int_ch2;
if (hex_char2 >= '0' && hex_char2 <= '9')
int_ch2 = (hex_char2 - 48); // // 0 锟斤拷Ascll - 48
else if (hex_char2 >= 'A' && hex_char2 <= 'F')
int_ch2 = hex_char2 - 55; // // A 锟斤拷Ascll - 65
else
return null;
int_ch = int_ch1 + int_ch2;
retData[i / 2] = (byte) int_ch;// 锟斤拷转锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷Byte锟斤拷
}
return retData;
}
}