Java 加密签名代码示例
import com.zwxt.bhc.base.common.exception.BadRequest;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
public abstract class PacketUtil {
public static String decode(String source, String sha1) {
String sign = source.substring(32, 64);
String body = source.substring(64);
try {
String sign_ = SecureCoder.md5(body + sha1);
if (!StringUtils.equals(sign, sign_)) {
throw new BadRequest("验签失败");
}
byte[] bytes = Hex.decodeHex(body);
body = new String(bytes, StandardCharsets.UTF_8);
}
catch (DecoderException e) {
throw new BadRequest("解码失败");
}
catch (UnsupportedEncodingException e) {
throw new BadRequest("字符编码不正确");
}
catch (Exception e) {
throw new BadRequest("解码失败");
}
return body;
}
public static String encode(String source, String sha1) {
try {
String mixture = SecureCoder.md5(new Date().getTime() + "");
String body = Hex.encodeHexString(source.getBytes(StandardCharsets.UTF_8));
String sign = SecureCoder.md5(body + sha1);
return mixture + sign + body;
}
catch (Exception e) {
throw new RuntimeException("编码失败");
}
}
public static void main(String[] args) throws Exception {
final String source1 = "{\"orderNo\":\"orderNo\", \"hisBillNo\":\"hisBillNo\", \"amount\": 0}";
String p = "31817498260286befc5a57a7edb7dff46d983354";
String eee = encode(source1, p);
System.err.println("密钥: " + p);
System.err.println("密文: " + eee);
System.err.println("明文: " + decode(eee, p));
System.err.println(decode("beddeb7e4d37443337e27a9890be230c19083b32707d527a9ce47afb88b398f57b0d0a202022696422203a2032303133312c0d0a202022736e22203a20223234303738393034222c0d0a202022637573746f6d4e6f22203a2022343132333236313939343130303332373530222c0d0a202022627573696e6573735479706522203a20223137222c0d0a202022627573696e6573734465736322203a2022e997a8e8af8ae98080e8b4b9222c0d0a202022627573696e657373526573756c7422203a202231222c0d0a202022627573696e657373526573756c744465736322203a202248495320e98080e8b4b9e68890e58a9f222c0d0a202022627573696e657373436f6e74656e7422203a20227b5c226172656149645c223a5c2230315c222c5c22686f73706974616c49645c223a5c22315c222c5c226f70657261746f724e6f5c223a5c223839303030382f3839303030385c222c5c226163636f756e7442616c616e63655c223a312e30307d222c0d0a202022686973427573696e6573734e6f22203a20223234303738393034222c0d0a202022706179657222203a202230303036323839313535222c0d0a20202263757272656e637922203a2022e4babae6b091e5b881222c0d0a202022616d6f756e7422203a20312e30302c0d0a202022626172636f646522203a206e756c6c2c0d0a2020227061795479706522203a202236222c0d0a2020227061794e616d6522203a2022343132333236313939343130303332373530222c0d0a2020227061794368616e6e656c22203a20223033222c0d0a20202270617954696d6522203a2022323032312d30332d31312031303a30393a3530222c0d0a2020226973526566756e6422203a202231222c0d0a202022726566756e64416d6f756e7422203a20312e30302c0d0a20202272656c61746564536e22203a2022363135343236313332353431313932313631222c0d0a202022726566756e6454696d6522203a2022323032312d30332d31312031303a30393a3530222c0d0a2020226578706972656454696d6522203a2022323032312d30332d31312031303a31343a3530222c0d0a202022706179526573756c7422203a20223032222c0d0a202022706179427573696e6573734e6f22203a2022222c0d0a2020227465726d53657269616c4e6f22203a206e756c6c2c0d0a20202263726561746554696d6522203a2022323032312d30332d31312031303a30393a3530222c0d0a2020226f70657261746f724e6f22203a20223839303030382f383930303038222c0d0a202022686f73706974616c496422203a202231222c0d0a20202261726561496422203a20223031222c0d0a202022726566756e6461626c65416d6f756e7422203a20302e30300d0a7d", p));
}
}