java 接口转码、加密
备用
package http; import java.io.UnsupportedEncodingException;
import sun.misc.*; public class Base64 {
// ����
public String getBase64(String str) {
byte[] b = null;
String s = null;
try {
b = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b != null) {
s = new BASE64Encoder().encode(b);
}
return s;
} // ����
public String getFromBase64(String s) {
byte[] b = null;
String result = null;
if (s != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(s);
result = new String(b, "utf-8");
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
} }
Base64.java
package http; import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection; public class get { public static void main(String[] args) throws Exception {
System.out.println(doGet());
} public static String doGet() throws Exception {
URL localURL = new URL("http://www.cnblogs.com/tester-hehehe/");
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection; httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null; if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
try {
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader); while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
} finally {
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer.toString();
}
}
get.java
package http; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class login {
public JSONObject Res(){
String s="123";
String mi;
mi = md5.getKeyedDigest(s, "");
// String res = "{'md':'###_pt_doc_cnt','page':'hehe'}";
String res = "{'page':'gainview','md':'partner_gain_pt_doc_cnt'}";
String result = new Base64().getBase64(res);
System.out.println(result);
ArrayList re = new ArrayList();
Map req1 = new HashMap();
req1.put("pid", "****");
req1.put("authId", "***");
req1.put("time", "***");
req1.put("param", result);
String req = "pid"+"authID"+"time"+result+"**私钥***";
String req2;
sha256 bb = new sha256();
req2 = bb.Encrypt(req, "SHA-256");
req1.put("sign", req2);
System.out.println(req1);
JSONObject jsonObject = JSONObject.fromObject(req1);
//System.out.println(rr);
return jsonObject;
}
public static void main(String[] argv){
login a = new login();
JSONObject b = a.Res();
System.out.println(b); }
}
login.java
内容为从网上转载,直接套用
package http; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; /**
* @author King_wangyao
*/
public class md5 {
public static byte[] getKeyedDigest(byte[] buffer, byte[] key) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(buffer);
return md5.digest(key);
} catch (NoSuchAlgorithmException e) {
}
return null;
} public static String getKeyedDigest(String strSrc, String key) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(strSrc.getBytes("UTF8")); String result="";
byte[] temp;
temp=md5.digest(key.getBytes("UTF8"));
for (int i=0; i<temp.length; i++){
result+=Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
}
return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); }catch(Exception e)
{
e.printStackTrace();
}
return null;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String mi;
String s = "1qaz@WSX3edc";
//第二个参数请填空字符串
mi=md5.getKeyedDigest(s,"");
System.out.println("mi:"+mi);
}
}
md5.java
package http; import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection; import net.sf.json.JSONObject; public class post {
public static void main(String[] args) throws Exception {
// String result = new Base64().getFromBase64(doPost());
System.out.println(doPost().getBytes());
} public static String doPost() throws Exception {
login lg = new login();
JSONObject parameterData = lg.Res();
// JSONObject parameterData = lg.Res();
System.out.println(parameterData);
URL localURL = new URL("http://***.wanlitong.com/");
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection; httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.toString().length())); OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null; try {
outputStream = httpURLConnection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream); outputStreamWriter.write(parameterData.toString());
outputStreamWriter.flush(); if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
} inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader); while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
} finally {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (outputStream != null) {
outputStream.close();
}
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer.toString();
}
}
post.java
package http; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; public class sha256 {
public String Encrypt(String strSrc, String encName) {
MessageDigest md = null;
String strDes = null; byte[] bt = strSrc.getBytes();
try {
if (encName == null || encName.equals("")) {
encName = "SHA-256";
}
md = MessageDigest.getInstance(encName);
md.update(bt);
strDes = bytes2Hex(md.digest()); // to HexString
} catch (NoSuchAlgorithmException e) {
return null;
}
// System.out.println(strDes);
return strDes; } public static String bytes2Hex(byte[] bts) {
String des = "";
String tmp = null;
for (int i = 0; i < bts.length; i++) {
tmp = (Integer.toHexString(bts[i] & 0xFF));
if (tmp.length() == 1) {
des += "0";
}
des += tmp;
}
return des;
}
}
sha256.java
java 接口转码、加密的更多相关文章
- Java接口源码--System和应用程序进程间通信
本文參考<Android系统源代码情景分析>.作者罗升阳 一.架构代码: ~/Android/frameworks/base/core/java/android/os ----IInter ...
- java接口签名(Signature)实现方案续
一.前言 由于之前写过的一片文章 (java接口签名(Signature)实现方案 )收获了很多好评,此次来说一下另一种简单粗暴的签名方案.相对于之前的签名方案,对body.paramenter.pa ...
- java接口签名(Signature)实现方案
预祝大家国庆节快乐,赶快迎接美丽而快乐的假期吧!!! 一.前言 在为第三方系统提供接口的时候,肯定要考虑接口数据的安全问题,比如数据是否被篡改,数据是否已经过时,数据是否可以重复提交等问题.其中我认为 ...
- [C#防止反编译].NET 产品版权保护方案 (.NET源码加密保护)
[C#防止反编译].NET 产品版权保护方案 (.NET源码加密保护) 标签: .net加密产品c#dll工具 2011-03-24 21:06 27009人阅读 评论(13) 收藏 举报 分类: C ...
- java接口调用——webservice就是一个RPC而已
很多新手一听到接口就蒙逼,不知道接口是什么!其实接口就是RPC,通过远程访问别的程序提供的方法,然后获得该方法执行的接口,而不需要在本地执行该方法.就是本地方法调用的升级版而已,我明天会上一篇如何通过 ...
- Java集合源码分析(四)Vector<E>
Vector<E>简介 Vector也是基于数组实现的,是一个动态数组,其容量能自动增长. Vector是JDK1.0引入了,它的很多实现方法都加入了同步语句,因此是线程安全的(其实也只是 ...
- Java集合源码分析(三)LinkedList
LinkedList简介 LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈.队列和双端队列来使用. LinkedList同样是非线程安全 ...
- Java集合源码分析(二)ArrayList
ArrayList简介 ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境下,多线 ...
- Java集合源码学习(一)集合框架概览
>>集合框架 Java集合框架包含了大部分Java开发中用到的数据结构,主要包括List列表.Set集合.Map映射.迭代器(Iterator.Enumeration).工具类(Array ...
随机推荐
- Modify Headers模拟不同地域进行网页测试
今天要简单讲一下Modify Headers这个Firefox插件,记录一下我是怎么使用它的. Modify Headers: https://addons.mozilla.org/zh-CN/fir ...
- FL2440 Linux-3.0内核触摸屏的支持
---------------------------------------------------------------------------------------------------- ...
- oracle 10g函数大全--转换函数
chartorowid(c1) [功能]转换varchar2类型为rowid值 [参数]c1,字符串,长度为18的字符串,字符串必须符合rowid格式 [返回]返回rowid值 [示例] SELECT ...
- java源码阅读HashSet
1类签名与注解 public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneabl ...
- Android基础新手教程——4.1.3 Activity登堂入室
Android基础新手教程--4.1.3 Activity登堂入室 标签(空格分隔): Android基础新手教程 本节引言: 好的,在学习了两节的Activity后相信大家已经知道怎样去使用Acti ...
- Jquery获取当前行的数据
取表格当前行数据js代码: Java代码 $(function() { $(".myclass").each(function(){ var tmp=$(this).chi ...
- UVALive 3971 Assemble(模拟 + 二分)
UVALive 3971 题意:有b块钱.想要组装一台电脑,给出n个配件的种类,名字,价格,品质因子.若各种类配件各买一个,总价格<=b,求最差品质配件的最大品质因子. 思路: 求最大的最小值一 ...
- Ubuntu git 安装、生成sshkey、克隆、切换分支
#1.安装git apt-get install git; #2生成公钥私钥文件 2.配置git账户: git config --global user.name "yourname&quo ...
- OpenERP为form和tree视图同时指定view_id的方法
Odoo,OpenERP中文网 / 2014-07-16 文所说的是关于OpenERP中同一个对象(同名继承)使用view_id来指定form和tree视图的方法,由于官方文档中Views and E ...
- JAVA的IO编程:管道流
掌握线程通讯流(管道流)的使用 管道流的主要作用是可以进行两个线程间的通讯,分为管道输入流(PipeOutputStream)和管道输出流(PipeInputStream). 如果要想进行管道输出,则 ...