import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
private static MessageDigest _mdInst = null;
private static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
private static MessageDigest getMdInst() {
if (_mdInst == null) {
try {
_mdInst = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return _mdInst;
public static String encode(String s) {
try {
byte[] btInput = s.getBytes();
getMdInst().update(btInput);
byte[] md = getMdInst().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;
(2)Base64实现
import java.io.UnsupportedEncodingException;
import org.apache.commons.lang.StringUtils;
import sun.misc.BASE64Decoder;
@SuppressWarnings("restriction")
public class Base64 {
public static String encode(String s) {
if (StringUtils.isEmpty(s)) {
return "";
try {
return (new sun.misc.BASE64Encoder()).encode(s.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
return "";
public static String decode(String s) {
if (StringUtils.isEmpty(s)) {
return "";
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
(3)sign签名实现
import org.apache.commons.lang3.RandomStringUtils;
public class SignUtils {
private final static String APPID = "galaxyind";
private final static String APPSECRET = "cf094f235b1248a6b7778d56f44c7c2e";
* @param userid 用户唯一标志
* @return
public static String createOpenid(String userid) {
return Base64.encode(APPID + userid);
* @param nonce // 随机数
* @param timeStamp 时间戳--自1970年01月01日起到现在的毫秒数
* @return
public static String createSign(String nonce,long timeStamp) {
String sign = MD5.encode(APPSECRET + MD5.encode(APPID + timeStamp + nonce));
return sign;
public String decodeOpenid(String openid) {
return null;
public static void main(String[] args) {
String openid = SignUtils.createOpenid("123456");
System.out.println("openid="+openid);
String nonce = RandomStringUtils.randomNumeric(6);
long timeStamp = System.currentTimeMillis();
String sign = SignUtils.createSign(nonce, timeStamp);
System.out.println(
"sign="+sign);
PHP示例代码
$appid = 'haowind';
$appsecret = 'cf094f235b1248a6b7778d56f4uu9iko';
$userid = 'useruuid';
$timestamp = msectime();
$nonce = randomStr();
$openid = base64_encode($appid.$userid);
$sign = md5($appid . md5($appid . $timestamp . $nonce));;
$url = "https://m.kuaidi100.com/app/?coname=hao123&appid={$appid}&kd100sign={$sign}&openid={$openid}&timeStamp={$timestamp}&nonce={$nonce}";
echo $url;
function randomStr(){
$arr = range(0, 9);
$str = '';
for($i = 0; $i < 6; $i++){
$str.= $arr[array_rand($arr)];
return $str;
function msectime() {
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectime;