public class SendValidCode {
// 短信发送的接口网关
private static String sendUrl = "****************************";
/**
* @Title: sendMessage
* @Description: 验证码短信发送
* @param userName:发送的用户名
* @param userPass:密码
* @param iphoneNum:电话号码
* @param signature:签名
* @return
*
* @return: String返回发送的结果
*/
public static String sendMessage(String userName, String userPass, String iphoneNum, String signature,
String validcode, String channel) {
// 发送结果
String msgContent = "验证码:" + validcode + ",请在60秒内完成验证。如非本人操作,请忽略。";// 发送消息的内容
String encrypStrPara1 = "userPass=" + userPass + "&DesNo=" + iphoneNum + "&Msg=" + msgContent + "【" + signature +"】&Channel=" + channel;
String encryptStr = DESEncrypt(encrypStrPara1, userPass);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("userCode", userName));
nvps.add(new BasicNameValuePair("submitInfo", encryptStr));
String post = httpPost(sendUrl, nvps); // post请求
//String getparam = "userCode=" + userName +"&submitInfo=" + encryptStr;
//String result = httpGet(sendUrl, getparam); // get请求
// 返回结果
return post;
}
private static String DESEncrypt(String encryptStr, String key) {
String result = "";
try {
String encryptKey = SHA1(key).substring(0, 8).toUpperCase();
DESKeySpec desKeySpec = new DESKeySpec(encryptKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
IvParameterSpec ivp = new IvParameterSpec(encryptKey.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, securekey, ivp);
byte[] encryptResult = cipher.doFinal(encryptStr.getBytes("GB2312"));
result = BinaryToHexString(encryptResult);
} catch (Exception e) {
System.out.println(e.getStackTrace());
System.out.println(e.getMessage());
}
return result;
}
private static String SHA1(String key) {
StringBuffer sb = new StringBuffer();
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(key.getBytes("GB2312"));
byte[] resultsha = md.digest();
for (byte b : resultsha) {
int i = b & 0xff;
if (i < 0xf) {
sb.append(0);
}
sb.append(Integer.toHexString(i));
}
} catch (Exception e) {
}
return sb.toString();
}
private static String BinaryToHexString(byte[] bytes) {
String hexStr = "0123456789ABCDEF";
String result = "";
String hex = "";
for (int i = 0; i < bytes.length; i++) {
// 字节高4位
hex = String.valueOf(hexStr.charAt((bytes[i] & 0xF0) >> 4));
// 字节低4位
hex += String.valueOf(hexStr.charAt(bytes[i] & 0x0F));
result += hex;
}
return result;
}
private static String httpPost(String url, List<NameValuePair> params) {
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instreams = entity.getContent();
result = convertStreamToString(instreams);
}
} catch (Exception e) {
}
return result;
}
private static String httpGet(String url, String params) {
String result = "";
try {
HttpClient client = new DefaultHttpClient();
if (params != "") {
url = url + "?" + params;
}
HttpGet httpget = new HttpGet(url);
HttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instreams = entity.getContent();
result = convertStreamToString(instreams);
}
} catch (Exception e) {
}
return result;
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/*public static void main(String[] args) {
String result = SendValidCode.sendMessage("XMSTCF", "XMST123abc", "185****3547", "万兆天空", "123456","0");
System.out.println(result+">>>>>>>");
}*/
}