9 changed files with 459 additions and 27 deletions
@ -0,0 +1,150 @@ |
|||
package com.zdxt.code.utils; |
|||
|
|||
import java.io.IOException; |
|||
import java.security.MessageDigest; |
|||
|
|||
/** |
|||
* Azdg加密工具类 |
|||
* |
|||
* @author longshangbo@163.com |
|||
* |
|||
* <p>2019年11月18日 下午5:36:14</p> |
|||
*/ |
|||
public class Azdg { |
|||
|
|||
private static final String key = "chinagdn"; |
|||
|
|||
private static final Azdg INSTANCE = new Azdg(); |
|||
|
|||
/** |
|||
* 解密 |
|||
* @param cipherText 要解密的内容 |
|||
* @return 解密后的字符串 |
|||
*/ |
|||
public static String decrypt(String cipherText) { |
|||
return INSTANCE.decrypt(cipherText, key); |
|||
} |
|||
|
|||
/** |
|||
* 加密 |
|||
* @param txt 要加密的内容 |
|||
* @return 加密后的字符串 |
|||
*/ |
|||
public static String encrypt(String txt) { |
|||
return INSTANCE.encrypt(txt, key); |
|||
} |
|||
|
|||
/** |
|||
* 加密 |
|||
* @param txt |
|||
* @param key |
|||
* @return |
|||
*/ |
|||
public String encrypt(String txt, String key) { |
|||
|
|||
String encrypt_key = "0f9cfb7a9acced8a4167ea8006ccd098"; |
|||
int ctr = 0; |
|||
String tmp = ""; |
|||
int i; |
|||
|
|||
for (i = 0; i < txt.length(); i++) { |
|||
ctr = (ctr == encrypt_key.length()) ? 0 : ctr; |
|||
tmp = tmp + encrypt_key.charAt(ctr) + (char) (txt.charAt(i) ^ encrypt_key.charAt(ctr)); |
|||
ctr++; |
|||
} |
|||
|
|||
return base64_encode(key(tmp, key)); |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 解密 |
|||
* @param cipherText |
|||
* @param key |
|||
* @return |
|||
*/ |
|||
public String decrypt(String cipherText, String key) { |
|||
|
|||
// base64解码
|
|||
cipherText = base64_decode(cipherText); |
|||
cipherText = key(cipherText, key); |
|||
String tmp = ""; |
|||
|
|||
for (int i = 0; i < cipherText.length(); i++) { |
|||
int c = cipherText.charAt(i) ^ cipherText.charAt(i + 1); |
|||
String x = "" + (char) c; |
|||
tmp += x; |
|||
i++; |
|||
} |
|||
return tmp; |
|||
} |
|||
|
|||
public String base64_decode(String str) { |
|||
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); |
|||
if (str == null) |
|||
return null; |
|||
try { |
|||
return new String(decoder.decodeBuffer(str)); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public String key(String txt, String encrypt_key) { |
|||
|
|||
encrypt_key = strMD5(encrypt_key); |
|||
int ctr = 0; |
|||
String tmp = ""; |
|||
|
|||
for (int i = 0; i < txt.length(); i++) { |
|||
|
|||
ctr = (ctr == encrypt_key.length()) ? 0 : ctr; |
|||
int c = txt.charAt(i) ^ encrypt_key.charAt(ctr); |
|||
String x = "" + (char) c; |
|||
tmp = tmp + x; |
|||
ctr++; |
|||
|
|||
} |
|||
|
|||
return tmp; |
|||
|
|||
} |
|||
|
|||
public String base64_encode(String str) { |
|||
return new sun.misc.BASE64Encoder().encode(str.getBytes()); |
|||
} |
|||
|
|||
public static final String strMD5(String s) { |
|||
|
|||
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
|||
'a', 'b', 'c', 'd', 'e', 'f' }; |
|||
|
|||
try { |
|||
|
|||
byte[] strTemp = s.getBytes(); |
|||
MessageDigest mdTemp = MessageDigest.getInstance("MD5"); |
|||
mdTemp.update(strTemp); |
|||
byte[] md = mdTemp.digest(); |
|||
int j = md.length; |
|||
char str[] = new char[j * 2]; |
|||
int k = 0; |
|||
|
|||
for (int i = 0; i < j; i++) { |
|||
byte byte0 = md[i]; |
|||
str[k++] = hexDigits[byte0 >>> 4 & 0xf]; |
|||
str[k++] = hexDigits[byte0 & 0xf]; |
|||
|
|||
} |
|||
|
|||
return new String(str); |
|||
|
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
return null; |
|||
|
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,126 @@ |
|||
package com.zdxt.code.utils; |
|||
|
|||
import org.apache.axis.client.Call; |
|||
import org.apache.axis.client.Service; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import javax.xml.namespace.QName; |
|||
|
|||
/** |
|||
* 政务短信新平台客户端demo |
|||
* |
|||
* 【注意】本客户端代码仅提供参考,有些地方并不很严谨,建议勿直接使用于生产环境!!! |
|||
* |
|||
* @author longshangbo@163.com |
|||
* |
|||
* <p>2019年8月21日 下午5:55:26</p> |
|||
*/ |
|||
@Component |
|||
public class ZwSmsClient { |
|||
|
|||
|
|||
static final String url = "https://szsms.sz.gov.cn:8000/services/Sms"; |
|||
|
|||
// 此处请使用分配给本单位的用户名和密码,
|
|||
//【注意】,这里用户名和密码都需要使用AZDG加密之后的,如果不知如何加密,请联系短信平台管理员!!!
|
|||
@Value("${sms.account}") |
|||
private static String username; |
|||
@Value("${sms.pwd}") |
|||
private static String password; |
|||
|
|||
public static void main(String[] args) throws Exception { |
|||
username = Azdg.encrypt(username); |
|||
password = Azdg.encrypt(password); |
|||
ConnMas(); |
|||
String orgCode = "10"; // 发送用户标记 一般两位数字
|
|||
String mobileNum = "17608416852,18520089969"; //接收号码, 多个逗号分隔
|
|||
String content = "测试内容:xxx"; // 短信内容
|
|||
Integer needReport = 1; // 是否需要短信报告 1 需要 其他不需要
|
|||
|
|||
String sendbody = "<sendbody>\n" + |
|||
"\t<message>\n" + |
|||
"\t<orgaddr></orgaddr><mobile>%s</mobile>" + |
|||
"<content><![CDATA[%s]]></content>" + |
|||
"<sendtime></sendtime>" + |
|||
"<needreport>%s</needreport>\n" + |
|||
"\t</message>\n" + |
|||
"\t<publicContent></publicContent>\n" + |
|||
"</sendbody>"; |
|||
InsertDownSms(String.format(sendbody, mobileNum, content, needReport)); |
|||
} |
|||
|
|||
/** |
|||
* 下发短信 |
|||
* @param sendbody |
|||
* @return |
|||
* @throws Exception |
|||
*/ |
|||
public static String InsertDownSms(String sendbody) throws Exception { |
|||
Service service = new Service();//创建客户端调用webservice的代理对象
|
|||
Call call = (Call) service.createCall();//创建一个调用对象,代表对web service 的一次调用
|
|||
call.setTargetEndpointAddress(new java.net.URL(url)); //设置web service的url 地址
|
|||
call.setOperationName(new QName("", "InsertDownSms")); //设置操作名称,QName 对象的两个参数分别为命名空间和方法名称
|
|||
|
|||
call.addParameter("username", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); |
|||
call.addParameter("password", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); |
|||
call.addParameter("batch", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); |
|||
call.addParameter("sendbody", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); |
|||
|
|||
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); |
|||
|
|||
String result = (String) call.invoke(new Object[]{username, password, "2409", sendbody}); |
|||
System.out.println(result);//打印返回的结果
|
|||
return result; |
|||
} |
|||
|
|||
/** |
|||
* 验证用户名密码 |
|||
* @return |
|||
* @throws Exception |
|||
*/ |
|||
public static String ConnMas() throws Exception { |
|||
Service service = new Service();//创建客户端调用webservice的代理对象
|
|||
Call call = (Call) service.createCall();//创建一个调用对象,代表对web service 的一次调用
|
|||
call.setTargetEndpointAddress(new java.net.URL(url)); //设置web service的url 地址
|
|||
call.setOperationName(new QName("", "ConnMas")); //设置操作名称,QName 对象的两个参数分别为命名空间和方法名称
|
|||
|
|||
call.addParameter("username", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); // 为本次调用的方法增加参数,第一次 //参数为参数类型:字符串类型,第二个参数指参数模式:入参
|
|||
call.addParameter("password", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); // 为本次调用的方法增加参数,第一次 //参数为参数类型:字符串类型,第二个参数指参数模式:入参
|
|||
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//为本次调用方法设置返回类型,这里是字符串类型
|
|||
String result = (String) call.invoke(new Object[]{username, password}); |
|||
System.out.println(result);//打印返回的结果
|
|||
return result; |
|||
} |
|||
|
|||
/** |
|||
* 发送短信接口 |
|||
* @param phone 接收人电话,支持多个逗号隔开 |
|||
* @param content 消息主体 |
|||
* @return |
|||
* @throws Exception |
|||
*/ |
|||
public static String sendMessage(String phone,String content) throws Exception{ |
|||
username = Azdg.encrypt("122"); |
|||
password = Azdg.encrypt("szsfj1800"); |
|||
ConnMas(); |
|||
String orgCode = "10"; // 发送用户标记 一般两位数字
|
|||
String mobileNum = phone; //接收号码, 多个逗号分隔
|
|||
//String content = "测试内容:xxx"; // 短信内容
|
|||
Integer needReport = 1; // 是否需要短信报告 1 需要 其他不需要
|
|||
|
|||
String sendbody = "<sendbody>\n" + |
|||
"\t<message>\n" + |
|||
"\t<orgaddr></orgaddr><mobile>%s</mobile>" + |
|||
"<content><![CDATA[%s]]></content>" + |
|||
"<sendtime></sendtime>" + |
|||
"<needreport>%s</needreport>\n" + |
|||
"\t</message>\n" + |
|||
"\t<publicContent></publicContent>\n" + |
|||
"</sendbody>"; |
|||
String s = InsertDownSms(String.format(sendbody, mobileNum, content, needReport)); |
|||
return s; |
|||
} |
|||
|
|||
|
|||
} |
|||
Loading…
Reference in new issue