/**
* TODO
*/
package com.itlt.tiancheng.util;
import java.io.*;
/**
* 加密解密处理
* @author aeolianer
* @created 2006-12-16
*
*/
public class Des64 {
public static boolean isInit=false;
public static boolean init(String dll_name, String ext_name){
try
{
//Finds a stream to the dll. Change path/class if necessary
Des64 de = new Des64();
InputStream inputStream = de.getClass().getResource("/"+dll_name+ext_name).openStream();
//复制动态库文,然后再载入,防止多项目时,不能重复载入
File temporaryDll = File.createTempFile(dll_name, ext_name);
FileOutputStream outputStream = new FileOutputStream(temporaryDll);
byte[] array = new byte[8192];
for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) {
outputStream.write(array, 0, i);
}
outputStream.close();
temporaryDll.deleteOnExit();
String path = temporaryDll.getPath();
// System.out.println("load path="+path);
System.load(path);
return true;
}
catch(Throwable e)
{
e.printStackTrace();
return false;
}
}
public static void init(){
String dll_name = "Des64";//dll名
String ext_name = ".dll";//扩展名
boolean status = false;
if(File.separator.equals("/")){
ext_name = ".so";
dll_name = "lib"+dll_name;
status = init(dll_name, ext_name);
}
else{
status = init(dll_name, ext_name);
// status = initLinux(dll_name, ext_name);
}
isInit = true;
System.out.println("load status="+status);
}
/**
* 随机产生临时文件
* @param path
* @return
*/
private File createTempDirectory(String path){
String temp_path = path+"/"+Math.round((Math.random()*200))+"/";
File directory = new File(temp_path);
System.out.println("temp_path="+temp_path);
if(directory.exists()){
return createTempDirectory(path);
}
return directory;
}
public Des64() {
// System.out.println("==========111111111Des64===========");
// String dll_name = "Des64";//dll名
// String ext_name = ".dll";//扩展名
// boolean status = false;
// if(File.separator.equals("/")){
// ext_name = ".so";
// dll_name = "lib"+dll_name;
// status = init(dll_name, ext_name);
// }
// else{
// status = init(dll_name, ext_name);
//// status = initLinux(dll_name, ext_name);
// }
// System.out.println("load status="+status);
// System.loadLibrary("Des64");
}
public native String enc(String value, String key);
public native String dec(String value, String key);
private static final String KEY_VALUE="doingsky";
public static String enCode(String value){
if(!isInit){
init();
}
String value_code = null;
Des64 de = new Des64();
if(value!=null){
value_code = de.enc(value, KEY_VALUE);
}
return value_code;
}
public static String deCode(String value_code){
if(!isInit){
init();
}
String value = null;
Des64 de = new Des64();
if(value_code!=null){
value = de.dec(value_code, KEY_VALUE);
}
return value;
}
public static void main(String[] args){
String key = "1234ABCD";
String value = "测试 helloworld 12345678";
String encryptValue = null;
System.out.println("===============加密测试===========");
System.out.println("原文:"+value);
// System.out.println("密码:"+key);
encryptValue = Des64.enCode(value);
System.out.println("密文:"+encryptValue);
System.out.println("解密:"+Des64.deCode(encryptValue));
System.out.println("解密1:"+Des64.deCode(encryptValue));
System.out.println("解密2:"+Des64.deCode(encryptValue));
System.out.println("解密3:"+Des64.deCode(encryptValue));
// Des64 de = new Des64();
//
// encryptValue = de.enc(value, key);
// System.out.println("加密密文(返回值):"+encryptValue);
//
// System.out.println();
// System.out.println();
// System.out.println();
//
// System.out.println("===============解密测试===========");
// System.out.println("密文"+encryptValue);
// System.out.println("密码:"+key);
// System.out.println("解密明文:(返回值)"+de.dec(encryptValue, key));
}
}
- 1
- 2
前往页