all
This commit is contained in:
parent
bef30231f0
commit
3b255514dd
BIN
DraftEncrypt.class
Normal file
BIN
DraftEncrypt.class
Normal file
Binary file not shown.
29
test/test/.gitignore
vendored
Normal file
29
test/test/.gitignore
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
### IntelliJ IDEA ###
|
||||||
|
out/
|
||||||
|
!**/src/main/**/out/
|
||||||
|
!**/src/test/**/out/
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
bin/
|
||||||
|
!**/src/main/**/bin/
|
||||||
|
!**/src/test/**/bin/
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
5
test/test/.idea/.gitignore
vendored
Normal file
5
test/test/.idea/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 基于编辑器的 HTTP 客户端请求
|
||||||
|
/httpRequests/
|
||||||
6
test/test/.idea/encodings.xml
Normal file
6
test/test/.idea/encodings.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/DraftEncrypt.java" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
test/test/.idea/misc.xml
Normal file
6
test/test/.idea/misc.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
test/test/.idea/modules.xml
Normal file
8
test/test/.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/test.iml" filepath="$PROJECT_DIR$/test.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
test/test/1.txt
Normal file
8
test/test/1.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
535944
|
||||||
|
01
|
||||||
|
0000
|
||||||
|
00A1
|
||||||
|
0000000000000000000000000000000000000000000000000000000000000000
|
||||||
|
3136303030
|
||||||
|
05
|
||||||
|
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||||
665
test/test/src/DraftEncrypt.java
Normal file
665
test/test/src/DraftEncrypt.java
Normal file
@ -0,0 +1,665 @@
|
|||||||
|
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.println("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("send=" + 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锟斤拷锟斤拷锟斤拷转锟斤拷锟斤拷锟 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//535944
|
||||||
|
// 01
|
||||||
|
// 0000
|
||||||
|
// 00A1
|
||||||
|
//0000000000000000000000000000000000000000000000000000000000000000
|
||||||
|
// 31
|
||||||
|
// 36303030050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||||
9
test/test/src/T.java
Normal file
9
test/test/src/T.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
public class T {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(30L * 24L * 60L * 60L * 1000L);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
215
test/test/src/Test.java
Normal file
215
test/test/src/Test.java
Normal file
@ -0,0 +1,215 @@
|
|||||||
|
package com.sunyard.encrypt;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.sunyard.encrypt.DraftEncrypt;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private static 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 static void main(String[] args) throws IOException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
/*****************************************************************************
|
||||||
|
DraftEncrypt:编核押函数
|
||||||
|
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)
|
||||||
|
参数说明:
|
||||||
|
TransactType:处理类型——2k:编押 2k+1:核押
|
||||||
|
(k = 0,只有一种密钥,适用于密押机I型)
|
||||||
|
k为整数0≤k≤9,代表10种密钥类型,适用于密押机II型)
|
||||||
|
YwType: 业务种类(0x01,0x02,0x03,0x04,0x05)
|
||||||
|
(0x01=现金汇票,0x02=可转转帐汇票,
|
||||||
|
0x03=不可转转帐汇票,0x04=查复书业务,0x05=划款业务)
|
||||||
|
Date: 签票日期(例如:"19990101")
|
||||||
|
SerialNo: 汇票号码(8位"12345678",或16位”1234567812345678”)
|
||||||
|
Amount: 金额 (单位为分,不含小数点,不足15位前补'0')
|
||||||
|
SendNode: 签发网点号,(例如:"000000123456") 不足12位前补'0'
|
||||||
|
RecvNode: 兑付网点号,若无则全填'0'
|
||||||
|
(划款业务时必须为”904290099992”)
|
||||||
|
OtherInfo: 前四个字节为查复书编号,其余全填0x00,若无则全填0x00
|
||||||
|
(例如“12345678”压缩成四个字节为0x12,0x34,0x56,0x78
|
||||||
|
不足8位前补零,例如“12345”压缩成四个字节为0x00,0x01,0x23,0x45)
|
||||||
|
CipherValue:密押
|
||||||
|
编押时,出口值为计算出的密押
|
||||||
|
核押时,入口值为需核验的密押
|
||||||
|
My_IPAdress1: IP地址1
|
||||||
|
My_IPAdress2: IP地址2
|
||||||
|
TimeOut: 超时时间
|
||||||
|
编押时,通过类的函数接口:getCipherValue() 返回一个String的字符串。
|
||||||
|
核押时,入口值为需核验的密押
|
||||||
|
|
||||||
|
函数返回值:
|
||||||
|
0: 表示处理正常,编押时密押在CipherValue字段返回,核押表示密押核验正确 .
|
||||||
|
>0:密押系统正常,但要素或密押中含有非法值,或者密押错误,通讯错误等 .
|
||||||
|
错误代码:
|
||||||
|
9001 联机错误
|
||||||
|
9002 发送错误
|
||||||
|
9003 接收错误
|
||||||
|
9004 编押错误
|
||||||
|
9005 核押错误
|
||||||
|
9006 核对签名错误
|
||||||
|
9007 发报行标识错误
|
||||||
|
9008 收报行标识错误
|
||||||
|
9009 加密卡处理异常
|
||||||
|
9010 数据报文错误
|
||||||
|
9011 网络通信超时
|
||||||
|
9012 发报行无编押权限
|
||||||
|
9013 密钥不存在
|
||||||
|
9014 兑付行号错误,当业务为划款业务时,如果兑付行号不为904290099992时返回该错误
|
||||||
|
9015 密钥过期
|
||||||
|
9016 金额格式错误
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
//编押:返回值为0,说明编押正常,且可以通过getCipherValue()获得密押。
|
||||||
|
|
||||||
|
/* int TransactType =3;
|
||||||
|
char YwType =(char)0x02;
|
||||||
|
String Date = "20151020";
|
||||||
|
String SerialNo = "9040004122999001";
|
||||||
|
String Amount = "000000000550000";
|
||||||
|
String SendNode = "213162000019";
|
||||||
|
String RecvNode = "000000000000";
|
||||||
|
String OtherInfo = "0x00";
|
||||||
|
String CipherValue = "0740128292";
|
||||||
|
String My_IPAdress1 = "192.168.1.10";
|
||||||
|
String My_IPAdress2 = "172.16.8.113";*/
|
||||||
|
int TransactType =0;
|
||||||
|
char YwType =(char)0x2;
|
||||||
|
String Date = "20251110";
|
||||||
|
String SerialNo = "10300200202511100000731236";
|
||||||
|
String Amount = "600000";
|
||||||
|
String SendNode = "1022401021000880495";
|
||||||
|
String RecvNode = "";
|
||||||
|
String OtherInfo = "";
|
||||||
|
String CipherValue = "";
|
||||||
|
String My_IPAdress1 = "172.1.40.161";
|
||||||
|
String My_IPAdress2 = "172.1.40.161";
|
||||||
|
int TimeOut = 100;
|
||||||
|
/*if(TransactType%2 == 1)
|
||||||
|
{
|
||||||
|
CipherValue = "3979564876";//核押需要输入
|
||||||
|
}*/
|
||||||
|
/*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;
|
||||||
|
}*///查复书时压缩字符串
|
||||||
|
|
||||||
|
/* for(int i =0 ;i<OtherInfo.length();i++){
|
||||||
|
int a = OtherInfo.charAt(i);
|
||||||
|
System.out.println("jjjjj"+a+"ijjjj");
|
||||||
|
}*/
|
||||||
|
|
||||||
|
DraftEncrypt de = new DraftEncrypt();
|
||||||
|
int ret = de.DraftEncrypt(TransactType,YwType,Date,SerialNo,Amount,
|
||||||
|
SendNode, RecvNode,OtherInfo,CipherValue,My_IPAdress1,My_IPAdress2,TimeOut);
|
||||||
|
/*int ret = de.DraftEncrypt(0, (char) 0x04, "20020422", "9934567812345678", "00000000.0000100",
|
||||||
|
"001420100008", "001420101021", "99999999", "2448307857",//可以随意输入一个字符
|
||||||
|
"172.16.8.113", "172.16.8.113", 100);*/
|
||||||
|
if(TransactType%2 == 0)
|
||||||
|
{
|
||||||
|
CipherValue = de.getCipherValue();//只有编押时需要调用
|
||||||
|
}
|
||||||
|
//核押:返回值为0,说明通过,其他返回值见说明
|
||||||
|
/* DraftEncrypt de = new DraftEncrypt();
|
||||||
|
int ret = de.DraftEncrypt(0, (char) 0x01, "20020422", "10000000", "000000000000100",
|
||||||
|
"001420100008", "001420101021", "", "096711466",
|
||||||
|
"192.168.1.212", "192.168.1.212", 100);
|
||||||
|
*/
|
||||||
|
|
||||||
|
switch(ret){
|
||||||
|
case 0:
|
||||||
|
if(TransactType%2 == 0)
|
||||||
|
System.out.println("编押正常,密押是:"+CipherValue);
|
||||||
|
else
|
||||||
|
System.out.println("核押正常");
|
||||||
|
break;
|
||||||
|
case 9001:
|
||||||
|
System.out.println("联机错误");
|
||||||
|
break;
|
||||||
|
case 9002:
|
||||||
|
System.out.println("发送错误");
|
||||||
|
break;
|
||||||
|
case 9003:
|
||||||
|
System.out.println("接收错误 ");
|
||||||
|
break;
|
||||||
|
case 9004:
|
||||||
|
System.out.println("加押错误");
|
||||||
|
break;
|
||||||
|
case 9005:
|
||||||
|
System.out.println("核押错误");
|
||||||
|
break;
|
||||||
|
case 9006:
|
||||||
|
System.out.println("核对签名错误");
|
||||||
|
break;
|
||||||
|
case 9007:
|
||||||
|
System.out.println("发报行标识错误");
|
||||||
|
break;
|
||||||
|
case 9008:
|
||||||
|
System.out.println("收报行标识错误");
|
||||||
|
break;
|
||||||
|
case 9009:
|
||||||
|
System.out.println("加密卡处理异常");
|
||||||
|
break;
|
||||||
|
case 9010:
|
||||||
|
System.out.println("数据报文错误");
|
||||||
|
break;
|
||||||
|
case 9011:
|
||||||
|
System.out.println("网络通信超时");
|
||||||
|
break;
|
||||||
|
case 9012:
|
||||||
|
System.out.println("发报行无编押权限");
|
||||||
|
break;
|
||||||
|
case 9013:
|
||||||
|
System.out.println("密钥不存在");
|
||||||
|
break;
|
||||||
|
case 9014:
|
||||||
|
System.out.println("兑付行号错误");
|
||||||
|
break;
|
||||||
|
case 9015:
|
||||||
|
System.out.println("密钥过期");
|
||||||
|
break;
|
||||||
|
case 9016:
|
||||||
|
System.out.println("金额格式错误");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
test/test/test.iml
Normal file
11
test/test/test.iml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
547
密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.00/DraftEncrypt.java
Normal file
547
密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.00/DraftEncrypt.java
Normal file
@ -0,0 +1,547 @@
|
|||||||
|
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
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
|
||||||
|
byte data[] = new byte[Len_Send];
|
||||||
|
for(int i=0,j=0; i <a.length;i++){
|
||||||
|
if( a[i]== -62 ){
|
||||||
|
;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
197
密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.00/Test.java
Normal file
197
密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.00/Test.java
Normal file
@ -0,0 +1,197 @@
|
|||||||
|
package com.sunyard.encrypt;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.sunyard.encrypt.DraftEncrypt;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private static 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 static void main(String[] args) throws IOException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
/*****************************************************************************
|
||||||
|
DraftEncrypt:编核押函数
|
||||||
|
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)
|
||||||
|
参数说明:
|
||||||
|
TransactType:处理类型——2k:编押 2k+1:核押
|
||||||
|
(k = 0,只有一种密钥,适用于密押机I型)
|
||||||
|
k为整数0≤k≤9,代表10种密钥类型,适用于密押机II型)
|
||||||
|
YwType: 业务种类(0x01,0x02,0x03,0x04,0x05)
|
||||||
|
(0x01=现金汇票,0x02=可转转帐汇票,
|
||||||
|
0x03=不可转转帐汇票,0x04=查复书业务,0x05=划款业务)
|
||||||
|
Date: 签票日期(例如:"19990101")
|
||||||
|
SerialNo: 汇票号码(8位"12345678",或16位”1234567812345678”)
|
||||||
|
Amount: 金额 (单位为分,不含小数点,不足15位前补'0')
|
||||||
|
SendNode: 签发网点号,(例如:"000000123456") 不足12位前补'0'
|
||||||
|
RecvNode: 兑付网点号,若无则全填'0'
|
||||||
|
(划款业务时必须为”904290099992”)
|
||||||
|
OtherInfo: 前四个字节为查复书编号,其余全填0x00,若无则全填0x00
|
||||||
|
(例如“12345678”压缩成四个字节为0x12,0x34,0x56,0x78
|
||||||
|
不足8位前补零,例如“12345”压缩成四个字节为0x00,0x01,0x23,0x45)
|
||||||
|
CipherValue:密押
|
||||||
|
编押时,出口值为计算出的密押
|
||||||
|
核押时,入口值为需核验的密押
|
||||||
|
My_IPAdress1: IP地址1
|
||||||
|
My_IPAdress2: IP地址2
|
||||||
|
TimeOut: 超时时间
|
||||||
|
编押时,通过类的函数接口:getCipherValue() 返回一个String的字符串。
|
||||||
|
核押时,入口值为需核验的密押
|
||||||
|
|
||||||
|
函数返回值:
|
||||||
|
0: 表示处理正常,编押时密押在CipherValue字段返回,核押表示密押核验正确 .
|
||||||
|
>0:密押系统正常,但要素或密押中含有非法值,或者密押错误,通讯错误等 .
|
||||||
|
错误代码:
|
||||||
|
9001 联机错误
|
||||||
|
9002 发送错误
|
||||||
|
9003 接收错误
|
||||||
|
9004 编押错误
|
||||||
|
9005 核押错误
|
||||||
|
9006 核对签名错误
|
||||||
|
9007 发报行标识错误
|
||||||
|
9008 收报行标识错误
|
||||||
|
9009 加密卡处理异常
|
||||||
|
9010 数据报文错误
|
||||||
|
9011 网络通信超时
|
||||||
|
9012 发报行无编押权限
|
||||||
|
9013 密钥不存在
|
||||||
|
9014 兑付行号错误,当业务为划款业务时,如果兑付行号不为904290099992时返回该错误
|
||||||
|
9015 密钥过期
|
||||||
|
9016 金额格式错误
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
//编押:返回值为0,说明编押正常,且可以通过getCipherValue()获得密押。
|
||||||
|
|
||||||
|
int TransactType =3;
|
||||||
|
char YwType =(char)0x04;
|
||||||
|
String Date = "20020422";
|
||||||
|
String SerialNo = "9934567812345678";
|
||||||
|
String Amount = "000000000000100";
|
||||||
|
String SendNode = "001420100008";
|
||||||
|
String RecvNode = "001420101021";
|
||||||
|
String OtherInfo = "99999999";
|
||||||
|
String CipherValue = "0000000000";
|
||||||
|
String My_IPAdress1 = "172.16.8.113";
|
||||||
|
String My_IPAdress2 = "172.16.8.113";
|
||||||
|
int TimeOut = 100;
|
||||||
|
if(TransactType%2 == 1)
|
||||||
|
{
|
||||||
|
CipherValue = "3979564876";//核押需要输入
|
||||||
|
}
|
||||||
|
if(OtherInfo.length()>=8){
|
||||||
|
char[] hex1=getSerialNo(OtherInfo);
|
||||||
|
String ssss = "";
|
||||||
|
for(int i=0;i<4;i++){
|
||||||
|
ssss=ssss+hex1[i];
|
||||||
|
}
|
||||||
|
OtherInfo=ssss;
|
||||||
|
}//查复书时压缩字符串
|
||||||
|
DraftEncrypt de = new DraftEncrypt();
|
||||||
|
int ret = de.DraftEncrypt(TransactType,YwType,Date,SerialNo,Amount,
|
||||||
|
SendNode, RecvNode,OtherInfo,CipherValue,My_IPAdress1,My_IPAdress2,TimeOut);
|
||||||
|
/*int ret = de.DraftEncrypt(0, (char) 0x04, "20020422", "9934567812345678", "00000000.0000100",
|
||||||
|
"001420100008", "001420101021", "99999999", "2448307857",//可以随意输入一个字符
|
||||||
|
"172.16.8.113", "172.16.8.113", 100);*/
|
||||||
|
if(TransactType%2 == 0)
|
||||||
|
{
|
||||||
|
CipherValue = de.getCipherValue();//只有编押时需要调用
|
||||||
|
}
|
||||||
|
//核押:返回值为0,说明通过,其他返回值见说明
|
||||||
|
/* DraftEncrypt de = new DraftEncrypt();
|
||||||
|
int ret = de.DraftEncrypt(0, (char) 0x01, "20020422", "10000000", "000000000000100",
|
||||||
|
"001420100008", "001420101021", "", "096711466",
|
||||||
|
"192.168.1.212", "192.168.1.212", 100);
|
||||||
|
*/
|
||||||
|
|
||||||
|
switch(ret){
|
||||||
|
case 0:
|
||||||
|
if(TransactType%2 == 0)
|
||||||
|
System.out.println("编押正常,密押是:"+CipherValue);
|
||||||
|
else
|
||||||
|
System.out.println("核押正常");
|
||||||
|
break;
|
||||||
|
case 9001:
|
||||||
|
System.out.println("联机错误");
|
||||||
|
break;
|
||||||
|
case 9002:
|
||||||
|
System.out.println("发送错误");
|
||||||
|
break;
|
||||||
|
case 9003:
|
||||||
|
System.out.println("接收错误 ");
|
||||||
|
break;
|
||||||
|
case 9004:
|
||||||
|
System.out.println("加押错误");
|
||||||
|
break;
|
||||||
|
case 9005:
|
||||||
|
System.out.println("核押错误");
|
||||||
|
break;
|
||||||
|
case 9006:
|
||||||
|
System.out.println("核对签名错误");
|
||||||
|
break;
|
||||||
|
case 9007:
|
||||||
|
System.out.println("发报行标识错误");
|
||||||
|
break;
|
||||||
|
case 9008:
|
||||||
|
System.out.println("收报行标识错误");
|
||||||
|
break;
|
||||||
|
case 9009:
|
||||||
|
System.out.println("加密卡处理异常");
|
||||||
|
break;
|
||||||
|
case 9010:
|
||||||
|
System.out.println("数据报文错误");
|
||||||
|
break;
|
||||||
|
case 9011:
|
||||||
|
System.out.println("网络通信超时");
|
||||||
|
break;
|
||||||
|
case 9012:
|
||||||
|
System.out.println("发报行无编押权限");
|
||||||
|
break;
|
||||||
|
case 9013:
|
||||||
|
System.out.println("密钥不存在");
|
||||||
|
break;
|
||||||
|
case 9014:
|
||||||
|
System.out.println("兑付行号错误");
|
||||||
|
break;
|
||||||
|
case 9015:
|
||||||
|
System.out.println("密钥过期");
|
||||||
|
break;
|
||||||
|
case 9016:
|
||||||
|
System.out.println("金额格式错误");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
BIN
密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.01/DraftEncrypt.jar
Normal file
BIN
密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.01/DraftEncrypt.jar
Normal file
Binary file not shown.
655
密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.01/DraftEncrypt.java
Normal file
655
密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.01/DraftEncrypt.java
Normal file
@ -0,0 +1,655 @@
|
|||||||
|
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锟斤拷锟斤拷锟斤拷转锟斤拷锟斤拷锟<EFBFBD>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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
215
密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.01/Test.java
Normal file
215
密码设备应用软件_java接口程序_城银清算密押会员行_密押版_REL_1.01/Test.java
Normal file
@ -0,0 +1,215 @@
|
|||||||
|
package com.sunyard.encrypt;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.sunyard.encrypt.DraftEncrypt;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private static 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 static void main(String[] args) throws IOException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
/*****************************************************************************
|
||||||
|
DraftEncrypt:编核押函数
|
||||||
|
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)
|
||||||
|
参数说明:
|
||||||
|
TransactType:处理类型——2k:编押 2k+1:核押
|
||||||
|
(k = 0,只有一种密钥,适用于密押机I型)
|
||||||
|
k为整数0≤k≤9,代表10种密钥类型,适用于密押机II型)
|
||||||
|
YwType: 业务种类(0x01,0x02,0x03,0x04,0x05)
|
||||||
|
(0x01=现金汇票,0x02=可转转帐汇票,
|
||||||
|
0x03=不可转转帐汇票,0x04=查复书业务,0x05=划款业务)
|
||||||
|
Date: 签票日期(例如:"19990101")
|
||||||
|
SerialNo: 汇票号码(8位"12345678",或16位”1234567812345678”)
|
||||||
|
Amount: 金额 (单位为分,不含小数点,不足15位前补'0')
|
||||||
|
SendNode: 签发网点号,(例如:"000000123456") 不足12位前补'0'
|
||||||
|
RecvNode: 兑付网点号,若无则全填'0'
|
||||||
|
(划款业务时必须为”904290099992”)
|
||||||
|
OtherInfo: 前四个字节为查复书编号,其余全填0x00,若无则全填0x00
|
||||||
|
(例如“12345678”压缩成四个字节为0x12,0x34,0x56,0x78
|
||||||
|
不足8位前补零,例如“12345”压缩成四个字节为0x00,0x01,0x23,0x45)
|
||||||
|
CipherValue:密押
|
||||||
|
编押时,出口值为计算出的密押
|
||||||
|
核押时,入口值为需核验的密押
|
||||||
|
My_IPAdress1: IP地址1
|
||||||
|
My_IPAdress2: IP地址2
|
||||||
|
TimeOut: 超时时间
|
||||||
|
编押时,通过类的函数接口:getCipherValue() 返回一个String的字符串。
|
||||||
|
核押时,入口值为需核验的密押
|
||||||
|
|
||||||
|
函数返回值:
|
||||||
|
0: 表示处理正常,编押时密押在CipherValue字段返回,核押表示密押核验正确 .
|
||||||
|
>0:密押系统正常,但要素或密押中含有非法值,或者密押错误,通讯错误等 .
|
||||||
|
错误代码:
|
||||||
|
9001 联机错误
|
||||||
|
9002 发送错误
|
||||||
|
9003 接收错误
|
||||||
|
9004 编押错误
|
||||||
|
9005 核押错误
|
||||||
|
9006 核对签名错误
|
||||||
|
9007 发报行标识错误
|
||||||
|
9008 收报行标识错误
|
||||||
|
9009 加密卡处理异常
|
||||||
|
9010 数据报文错误
|
||||||
|
9011 网络通信超时
|
||||||
|
9012 发报行无编押权限
|
||||||
|
9013 密钥不存在
|
||||||
|
9014 兑付行号错误,当业务为划款业务时,如果兑付行号不为904290099992时返回该错误
|
||||||
|
9015 密钥过期
|
||||||
|
9016 金额格式错误
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
//编押:返回值为0,说明编押正常,且可以通过getCipherValue()获得密押。
|
||||||
|
|
||||||
|
/* int TransactType =3;
|
||||||
|
char YwType =(char)0x02;
|
||||||
|
String Date = "20151020";
|
||||||
|
String SerialNo = "9040004122999001";
|
||||||
|
String Amount = "000000000550000";
|
||||||
|
String SendNode = "213162000019";
|
||||||
|
String RecvNode = "000000000000";
|
||||||
|
String OtherInfo = "0x00";
|
||||||
|
String CipherValue = "0740128292";
|
||||||
|
String My_IPAdress1 = "192.168.1.10";
|
||||||
|
String My_IPAdress2 = "172.16.8.113";*/
|
||||||
|
int TransactType =0;
|
||||||
|
char YwType =(char)0x2;
|
||||||
|
String Date = "20151020";
|
||||||
|
String SerialNo = "12345678";
|
||||||
|
String Amount = "000000000000100";
|
||||||
|
String SendNode = "123456123456";
|
||||||
|
String RecvNode = "904290099992";
|
||||||
|
String OtherInfo = "123";
|
||||||
|
String CipherValue = "1160747148";
|
||||||
|
String My_IPAdress1 = "192.168.1.10";
|
||||||
|
String My_IPAdress2 = "172.16.8.113";
|
||||||
|
int TimeOut = 100;
|
||||||
|
/*if(TransactType%2 == 1)
|
||||||
|
{
|
||||||
|
CipherValue = "3979564876";//核押需要输入
|
||||||
|
}*/
|
||||||
|
/*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;
|
||||||
|
}*///查复书时压缩字符串
|
||||||
|
|
||||||
|
/* for(int i =0 ;i<OtherInfo.length();i++){
|
||||||
|
int a = OtherInfo.charAt(i);
|
||||||
|
System.out.println("jjjjj"+a+"ijjjj");
|
||||||
|
}*/
|
||||||
|
|
||||||
|
DraftEncrypt de = new DraftEncrypt();
|
||||||
|
int ret = de.DraftEncrypt(TransactType,YwType,Date,SerialNo,Amount,
|
||||||
|
SendNode, RecvNode,OtherInfo,CipherValue,My_IPAdress1,My_IPAdress2,TimeOut);
|
||||||
|
/*int ret = de.DraftEncrypt(0, (char) 0x04, "20020422", "9934567812345678", "00000000.0000100",
|
||||||
|
"001420100008", "001420101021", "99999999", "2448307857",//可以随意输入一个字符
|
||||||
|
"172.16.8.113", "172.16.8.113", 100);*/
|
||||||
|
if(TransactType%2 == 0)
|
||||||
|
{
|
||||||
|
CipherValue = de.getCipherValue();//只有编押时需要调用
|
||||||
|
}
|
||||||
|
//核押:返回值为0,说明通过,其他返回值见说明
|
||||||
|
/* DraftEncrypt de = new DraftEncrypt();
|
||||||
|
int ret = de.DraftEncrypt(0, (char) 0x01, "20020422", "10000000", "000000000000100",
|
||||||
|
"001420100008", "001420101021", "", "096711466",
|
||||||
|
"192.168.1.212", "192.168.1.212", 100);
|
||||||
|
*/
|
||||||
|
|
||||||
|
switch(ret){
|
||||||
|
case 0:
|
||||||
|
if(TransactType%2 == 0)
|
||||||
|
System.out.println("编押正常,密押是:"+CipherValue);
|
||||||
|
else
|
||||||
|
System.out.println("核押正常");
|
||||||
|
break;
|
||||||
|
case 9001:
|
||||||
|
System.out.println("联机错误");
|
||||||
|
break;
|
||||||
|
case 9002:
|
||||||
|
System.out.println("发送错误");
|
||||||
|
break;
|
||||||
|
case 9003:
|
||||||
|
System.out.println("接收错误 ");
|
||||||
|
break;
|
||||||
|
case 9004:
|
||||||
|
System.out.println("加押错误");
|
||||||
|
break;
|
||||||
|
case 9005:
|
||||||
|
System.out.println("核押错误");
|
||||||
|
break;
|
||||||
|
case 9006:
|
||||||
|
System.out.println("核对签名错误");
|
||||||
|
break;
|
||||||
|
case 9007:
|
||||||
|
System.out.println("发报行标识错误");
|
||||||
|
break;
|
||||||
|
case 9008:
|
||||||
|
System.out.println("收报行标识错误");
|
||||||
|
break;
|
||||||
|
case 9009:
|
||||||
|
System.out.println("加密卡处理异常");
|
||||||
|
break;
|
||||||
|
case 9010:
|
||||||
|
System.out.println("数据报文错误");
|
||||||
|
break;
|
||||||
|
case 9011:
|
||||||
|
System.out.println("网络通信超时");
|
||||||
|
break;
|
||||||
|
case 9012:
|
||||||
|
System.out.println("发报行无编押权限");
|
||||||
|
break;
|
||||||
|
case 9013:
|
||||||
|
System.out.println("密钥不存在");
|
||||||
|
break;
|
||||||
|
case 9014:
|
||||||
|
System.out.println("兑付行号错误");
|
||||||
|
break;
|
||||||
|
case 9015:
|
||||||
|
System.out.println("密钥过期");
|
||||||
|
break;
|
||||||
|
case 9016:
|
||||||
|
System.out.println("金额格式错误");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user