解密java代码 java 加解密算法
小程序后台获取openId解密的java代码怎么写
一、获取code
我们提供的服务有:成都做网站、网站设计、外贸营销网站建设、微信公众号开发、网站优化、网站认证、维西ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的维西网站制作公司
将code作为参数传递过来
//如果有code,说明是微信小程序,根据code获取openId
//classify用于标识是哪个小程序
if (!CheckUtil.checkNulls( keUser.getCode(),keUser.getClassify())){
//
String openid = OpenIdUtil.oauth2GetOpenid(keUser.getCode(),keUser.getClassify());
printParamsLog(openid, logger);
keUser.setUserId(openid);
}1234567812345678
二、工具类
package com.util;
import net.sf.json.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import java.util.HashMap;
import java.util.Map;
/**
* @author xsx
*/
public class OpenIdUtil {
public static String oauth2GetOpenid(String code,String classify) {
String appid="";
String appsecret="";
switch (classify){
case "1":
//自己的配置appid
appid = "**********";
//自己的配置APPSECRET;
appsecret = "**********";
break;
case "2":
appid = "**********";
appsecret = "************";
break;
case "3":
appid = "**********";
appsecret = "************";
break;
case "4":
appid = "**********";
appsecret = "************";
break;
case "5":
appid = "**********";
appsecret = "************";
}
//授权(必填)
String grant_type = "authorization_code";
//URL
String requestUrl = "";
//请求参数
String params = "appid=" + appid + "secret=" + appsecret + "js_code=" + code + "grant_type=" + grant_type;
//发送请求
String data = HttpUtil.get(requestUrl, params);
//解析相应内容(转换成json对象)
JSONObject json = JSONObject.fromObject(data);
//用户的唯一标识(openid)
String Openid =String.valueOf(json.get("openid"));
//System.out.println(Openid);
return Openid;
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
三、发送请求的工具类
package com.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java点虐 .URL;
import java点虐 .URLConnection;
import java.util.List;
import java.util.Map;
/**
* @author xsx
*/
public class HttpUtil {
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1name2=value2 的形式。
* @return String 所代表远程资源的响应结果
*/
public static String get(String url,String param){
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
//System.out.println(urlNameString);
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
MapString, ListString map = connection.getHeaderFields();
// 遍历所有的响应头字段
/*for (String key : map.keySet()) {
System.out.println(key + "---" + map.get(key));
}*/
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
java加密解密代码
package com.cube.limail.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;/**
* 加密解密类
*/
public class Eryptogram
{
private static String Algorithm ="DES";
private String key="CB7A92E3D3491964";
//定义 加密算法,可用 DES,DESede,Blowfish
static boolean debug = false ;
/**
* 构造子注解.
*/
public Eryptogram ()
{
} /**
* 生成密钥
* @return byte[] 返回生成的密钥
* @throws exception 扔出异常.
*/
public static byte [] getSecretKey () throws Exception
{
KeyGenerator keygen = KeyGenerator.getInstance (Algorithm );
SecretKey deskey = keygen.generateKey ();
System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
if (debug ) System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
return deskey.getEncoded ();
} /**
* 将指定的数据根据提供的密钥进行加密
* @param input 需要加密的数据
* @param key 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
public static byte [] encryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug )
{
System.out.println ("加密前的二进串:"+byte2hex (input ));
System.out.println ("加密前的字符串:"+new String (input ));
} Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.ENCRYPT_MODE ,deskey );
byte [] cipherByte =c1.doFinal (input );
if (debug ) System.out.println ("加密后的二进串:"+byte2hex (cipherByte ));
return cipherByte ;
} /**
* 将给定的已加密的数据通过指定的密钥进行解密
* @param input 待解密的数据
* @param key 密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
public static byte [] decryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));
Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.DECRYPT_MODE ,deskey );
byte [] clearByte =c1.doFinal (input );
if (debug )
{
System.out.println ("解密后的二进串:"+byte2hex (clearByte ));
System.out.println ("解密后的字符串:"+(new String (clearByte )));
} return clearByte ;
} /**
* 字节码转换成16进制字符串
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的16进制字符串
*/
public static String byte2hex (byte [] b )
{
String hs ="";
String stmp ="";
for (int n =0 ;n b.length ;n ++)
{
stmp =(java.lang.Integer.toHexString (b [n ] 0XFF ));
if (stmp.length ()==1 ) hs =hs +"0"+stmp ;
else hs =hs +stmp ;
if (n b.length -1 ) hs =hs +":";
} return hs.toUpperCase ();
}
/**
* 字符串转成字节数组.
* @param hex 要转化的字符串.
* @return byte[] 返回转化后的字符串.
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
/**
* 字节数组转成字符串.
* @param String 要转化的字符串.
* @return 返回转化后的字节数组.
*/
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i bArray.length; i++) {
sTemp = Integer.toHexString(0xFF bArray[i]);
if (sTemp.length() 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/**
* 从数据库中获取密钥.
* @param deptid 企业id.
* @return 要返回的字节数组.
* @throws Exception 可能抛出的异常.
*/
public static byte[] getSecretKey(long deptid) throws Exception {
byte[] key=null;
String value=null;
//CommDao dao=new CommDao();
// List list=dao.getRecordList("from Key k where k.deptid="+deptid);
//if(list.size()0){
//value=((com.csc.sale.bean.Key)list.get(0)).getKey();
value = "CB7A92E3D3491964";
key=hexStringToByte(value);
//}
if (debug)
System.out.println("密钥:" + value);
return key;
}
public String encryptData2(String data) {
String en = null;
try {
byte[] key=hexStringToByte(this.key);
en = bytesToHexString(encryptData(data.getBytes(),key));
} catch (Exception e) {
e.printStackTrace();
}
return en;
}
public String decryptData2(String data) {
String de = null;
try {
byte[] key=hexStringToByte(this.key);
de = new String(decryptData(hexStringToByte(data),key));
} catch (Exception e) {
e.printStackTrace();
}
return de;
}
} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //获得钥匙(字节数组)
byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //传入密码和钥匙,获得加密后的字节数组的密码
password=Eryptogram.bytesToHexString(tmp); //将字节数组转化为字符串,获得加密后的字符串密码解密与之差不多
谁能提供下java中有关加密和解密的代码
public static void main(String[] args) throws Exception {
String data = "itxxz";
System.out.println("字符串:itxxz");
System.err.println("加密:"+encrypt(data));
System.err.println("解密:"+decrypt(encrypt(data)));
}
运行结果:
由于代码太多,可到 itxxz点抗 /a/javashili/2014/1217/encrypt_decrypt.html 查看,注释也比较完整,清晰易懂
JAVA解密文本代码的问题。可以运行追加200分。
按照你同学的加密算法,只对英文小写字母进行了加密,原文和密文的对应关系如下:
原文:a b c d e f g h i j k l m n o p q r s t u v w x y z
密文:f g h i j k l m n o p q r s t u v w x y z { | } a b
怎么得到上述关系呢,其实是所有的字符都有一个对应的ASCII码,相关ASCII码如下:
可以看到v的ASCII码为118,+5为123,所以变为{
97 a
98 b
99 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z
123 {
124 |
125 }
下面为解密代码
import java.io.*;
class FileIo2 {
public static void main(String args[]) {
// 声明输入流引用
FileInputStream fis = null;
// 声明输出流引用
FileOutputStream fos = null;
try {
// 生成代表输入流的对象
fis = new FileInputStream("D:/test1.txt");
// 生成代表输出流的对象
fos = new FileOutputStream("D:/test2.txt");
// 生成一个字节数组
byte[] buffer = new byte[100];
// 调用输入对象的read方法,读取字节数组的数据
int temp = fis.read(buffer, 0, buffer.length);
for (int i = 0; i temp; i++) {
if (buffer[i] = 'f' buffer[i] = 'z') {
buffer[i] -= 5;
System.out.printf("%c", buffer[i]);
}else if (buffer[i] == 'a') {
buffer[i] = 'y';
System.out.printf("%c", buffer[i]);
}else if (buffer[i] == 'b') {
buffer[i] = 'z';
System.out.printf("%c", buffer[i]);
}else if(buffer[i] == '{'){
buffer[i] = 'v';
System.out.printf("%c", buffer[i]);
}else if(buffer[i] == '|'){
buffer[i] = 'w';
System.out.printf("%c", buffer[i]);
}else if(buffer[i] == '}'){
buffer[i] = 'x';
System.out.printf("%c", buffer[i]);
}
}
// System.out.printf("temp=%d",temp);
// temp临时定义用来接收read返回值类型,从而判断写入多少数据
fos.write(buffer, 0, temp);
} catch (Exception e) {
System.out.println(e);
}
}
}
下面这段代码是既有加密也有解密:
import java.io.*;
class FileIo2 {
public static void jiemi(){
// 声明输入流引用
FileInputStream fis = null;
// 声明输出流引用
FileOutputStream fos = null;
try {
// 生成代表输入流的对象
fis = new FileInputStream("D:/test1.txt");
// 生成代表输出流的对象
fos = new FileOutputStream("D:/test2.txt");
// 生成一个字节数组
byte[] buffer = new byte[100];
// 调用输入对象的read方法,读取字节数组的数据
int temp = fis.read(buffer, 0, buffer.length);
for (int i = 0; i temp; i++) {
if (buffer[i] = 'f' buffer[i] = 'z') {
buffer[i] -= 5;
System.out.printf("%c", buffer[i]);
}else if (buffer[i] == 'a') {
buffer[i] = 'y';
System.out.printf("%c", buffer[i]);
}else if (buffer[i] == 'b') {
buffer[i] = 'z';
System.out.printf("%c", buffer[i]);
}else if(buffer[i] == '{'){
buffer[i] = 'v';
System.out.printf("%c", buffer[i]);
}else if(buffer[i] == '|'){
buffer[i] = 'w';
System.out.printf("%c", buffer[i]);
}else if(buffer[i] == '}'){
buffer[i] = 'x';
System.out.printf("%c", buffer[i]);
}
}
// System.out.printf("temp=%d",temp);
// temp临时定义用来接收read返回值类型,从而判断写入多少数据
fos.write(buffer, 0, temp);
} catch (Exception e) {
System.out.println(e);
}
}
public static void jiami(){
// 声明输入流引用
FileInputStream fis = null;
// 声明输出流引用
FileOutputStream fos = null;
try {
// 生成代表输入流的对象
fis = new FileInputStream("D:/test.txt");
// 生成代表输出流的对象
fos = new FileOutputStream("D:/test1.txt");
// 生成一个字节数组
byte[] buffer = new byte[100];
// 调用输入对象的read方法,读取字节数组的数据
int temp = fis.read(buffer, 0, buffer.length);
for (int i = 0; i temp; i++) {
if (buffer[i] = 'a' buffer[i] = 'x') {
buffer[i] += 5;
// buffer[i]--;
System.out.printf("%c", buffer[i]);
}
if (buffer[i] == 'y') {
buffer[i] = 'a';
System.out.printf("%c", buffer[i]);
}
if (buffer[i] == 'z') {
buffer[i] = 'b';
System.out.printf("%c", buffer[i]);
}
}
// System.out.printf("temp=%d",temp);
// temp临时定义用来接收read返回值类型,从而判断写入多少数据
fos.write(buffer, 0, temp);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
jiami();//先加密
jiemi();//再解密
}
}
网页名称:解密java代码 java 加解密算法
网页URL:http://myzitong.com/article/ddigoes.html