##### 价格加密
价格加密采用 google 自定义的加密机制
[https://developers.google.com/ad-exchange/rtb/response-guide/decrypt-price#encryptionscheme](https://developers.google.com/ad-exchange/rtb/response-guide/decrypt-price#encryptionscheme)
wiki:[https://github.com/google/openrtb-doubleclick/wiki](https://github.com/google/openrtb-doubleclick/wiki)
```
/**
* Created by hzg on 2017/7/10.
*/
public class WinnoticeUtil {
public static final byte[] KEY1 = "kingsoft".getBytes();
public static final byte[] KEY2 = "adx".getBytes();
public static final BigDecimal ZERO = new BigDecimal(0);
public static final BigDecimal ONEHUNDRED = new BigDecimal(100);
public static DoubleClickCrypto.Price clickCrypto = null;
static {
try {
DoubleClickCrypto.Keys keys = new DoubleClickCrypto.Keys(
new SecretKeySpec(KEY1, "HmacSHA1"),
new SecretKeySpec(KEY2, "HmacSHA1")
);
clickCrypto = new DoubleClickCrypto.Price(keys);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encrytPrice(BigDecimal price) {
if (price == null) {
price = ZERO;
}
return clickCrypto.encodePriceValue(price.multiply(ONEHUNDRED).doubleValue(), null);
}
public static String encrytPrice(String price) {
if (price == null) {
return encrytPrice(ZERO);
}
return encrytPrice(new BigDecimal(price));
}
public static String replacePrice(String url, Object... param) {
if (StringUtils.isEmpty(url)) {
return "";
}
return MessageFormat.format(url, param);
}
public static List<String> replaceUrls(List<String> urls, String price) {
if (price == null) {
return replaceUrls(urls, ZERO);
}
return replaceUrls(urls, new BigDecimal(price));
}
public static List<String> replaceUrls(List<String> urls, BigDecimal price) {
List<String> replaceUrls = Lists.newArrayList();
if (CollectionUtils.isEmpty(urls)) {
return replaceUrls;
}
for (String url : urls) {
replaceUrls.add(replacePrice(url, encrytPrice(price)));
}
return replaceUrls;
}
public static void main(String[] args) throws SignatureException {
//加密
String cryptoStr = clickCrypto.encodePriceValue(12,null);
System.out.println(cryptoStr);
//解密
System.out.println(clickCrypto.decodePriceValue(cryptoStr));
}
}
输出
XJwszwAM1ShpYgxpFjZCHnpjOT-uc5s7eH0PzA==
12
```