易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Java加密共通函数
Java加密共通函数
import
java.io.File;
import
java.io.IOException;
import
java.net.URL;
import
java.security.InvalidKeyException;
import
java.security.NoSuchAlgorithmException;
import
java.security.Security;
import
javax.crypto.BadPaddingException;
import
javax.crypto.Cipher;
import
javax.crypto.IllegalBlockSizeException;
import
javax.crypto.NoSuchPaddingException;
import
javax.crypto.spec.SecretKeySpec;
import
org.apache.commons.io.FileUtils;
import
org.apache.commons.lang.StringUtils;
public
class
DesUtils {
static
{
Security.addProvider(
new
com.sun.crypto.provider.SunJCE());
}
static
boolean
debug =
true
;
private
static
String Algorithm =
"AES"
;
private
static
String secutiyKey =
"1234567812345678"
;
private
static
String fileName1=
"DESPW1.class"
;
private
static
String fileName2=
"DESPW2.class"
;
public
static
String getKey()
throws
Exception {
String ret =
""
;
URL url=DesUtils.
class
.getResource(
""
);
// 1.read the key from 1st file
ret =
new
String(hex2byte(FileUtils.readFileToString(
new
File(url.getPath()+
"/"
+fileName1))));
// 2.read the key from 2nd file
// 3.combine the keys
ret = ret +
new
String(hex2byte(FileUtils.readFileToString(
new
File(url.getPath()+
"/"
+fileName2))));
// 4.change them to the byte array
return
ret;
}
public
static
void
writeKeyToFile(){
String key=StringUtils.strip(secutiyKey);
int
lenght=key.length();
String prefix=
""
;
String afterFix=
""
;
if
(lenght>
0
){
prefix=StringUtils.substring(key,
0
, lenght/
2
);
afterFix=StringUtils.substring(key, lenght/
2
+
1
, lenght);
URL url=DesUtils.
class
.getResource(
""
);
try
{
FileUtils.writeStringToFile(
new
File(url.getPath()+
"/"
+fileName1), byte2hex(prefix.getBytes()));
FileUtils.writeStringToFile(
new
File(url.getPath()+
"/"
+fileName2), byte2hex(afterFix.getBytes()));
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public
static
byte
[] encodeConfig(
byte
[] set,
byte
[] key) {
byte
[] ret =
null
;
// byte[] key = "tnts".getBytes();
try
{
SecretKeySpec sk =
new
SecretKeySpec(key, Algorithm);
Cipher c1;
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, sk);
ret = c1.doFinal(set);
}
catch
(NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch
(NoSuchPaddingException e) {
e.printStackTrace();
}
catch
(IllegalBlockSizeException e) {
e.printStackTrace();
}
catch
(BadPaddingException e) {
e.printStackTrace();
}
catch
(InvalidKeyException e) {
e.printStackTrace();
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
ret;
}
public
static
byte
[] decodeConfig(
byte
[] set,
byte
[] key) {
byte
[] ret =
null
;
try
{
// byte[] key = getKey();
SecretKeySpec sk =
new
SecretKeySpec(key, Algorithm);
Cipher c1;
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, sk);
ret = c1.doFinal(set);
}
catch
(NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch
(NoSuchPaddingException e) {
e.printStackTrace();
}
catch
(IllegalBlockSizeException e) {
e.printStackTrace();
}
catch
(BadPaddingException e) {
e.printStackTrace();
}
catch
(InvalidKeyException e) {
e.printStackTrace();
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
ret;
}
// byte数组转换为16进制字符串
public
static
String byte2hex(
byte
[] data) {
StringBuffer sb =
new
StringBuffer();
for
(
int
i =
0
; i < data.length; i++) {
String temp = Integer.toHexString(((
int
) data[i]) &
0xFF
);
for
(
int
t = temp.length(); t <
2
; t++) {
sb.append(
"0"
);
}
sb.append(temp);
}
return
sb.toString();
}
// 16进制转换为byte数组
public
static
byte
[] hex2byte(String hexStr) {
byte
[] bts =
new
byte
[hexStr.length() /
2
];
for
(
int
i =
0
, j =
0
; j < bts.length; j++) {
bts[j] = (
byte
) Integer.parseInt(hexStr.substring(i, i +
2
),
16
);
i +=
2
;
}
return
bts;
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图