示例#1
0
 /**
  * Base64解密
  *
  * @param inputText
  * @param charset 字符集
  * @return
  */
 public static String base64Decode(String inputText, String... charset) {
   if (charset.length == 1) {
     return Base64.decode(inputText, charset[0]);
   } else {
     return Base64.decode(inputText);
   }
 }
  @Override
  public UserLocation convertCoord(String lng, String lat) {
    // 百度坐标转换接口
    String convertUrl = "http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x={x}&y={y}";
    convertUrl = convertUrl.replace("{x}", lng);
    convertUrl = convertUrl.replace("{y}", lat);

    UserLocation location = new UserLocation();
    try {
      String jsonCoord = HttpClientUtil.httpGet(convertUrl);
      JSONObject jsonObject = JSONObject.fromObject(jsonCoord);
      // 对转换后的坐标进行Base64解码
      location.setBd09Lng(Base64.decode(jsonObject.getString("x"), "UTF-8"));
      location.setBd09Lat(Base64.decode(jsonObject.getString("y"), "UTF-8"));
    } catch (Exception e) {
      location = null;
      logger.info("坐标转换失败");
    }
    return location;
  }
 // 公钥解密
 public static byte[] decryptByPublicKey(String key, byte[] data) throws Exception {
   // 实例化公钥材�?
   X509EncodedKeySpec x509spec = new X509EncodedKeySpec(Base64.decode(key.getBytes()));
   // 实例化密钥工�?
   KeyFactory keyfactory = KeyFactory.getInstance(Key_ALGORITHM);
   // 获得公钥
   PublicKey pubkey = keyfactory.generatePublic(x509spec);
   // 对数据进行解�?
   Cipher cipher = Cipher.getInstance(Key_ALGORITHM);
   cipher.init(Cipher.DECRYPT_MODE, pubkey);
   return cipher.doFinal(data);
 }
 // 私钥加密
 public static byte[] encryptByPrivateKey(String key, byte[] data) throws Exception {
   // 实例化密钥材�?
   PKCS8EncodedKeySpec pcs8spec = new PKCS8EncodedKeySpec(Base64.decode(key.getBytes()));
   // 实例化密钥工�?
   KeyFactory keyfactory = KeyFactory.getInstance(Key_ALGORITHM);
   // 生成私钥
   PrivateKey pritekey = keyfactory.generatePrivate(pcs8spec);
   // 私钥加密
   Cipher cipher = Cipher.getInstance(pritekey.getAlgorithm());
   cipher.init(Cipher.ENCRYPT_MODE, pritekey);
   return cipher.doFinal(data);
 }