package com
import java
import javax
import javax
import javax
import javax
/**
* DES加解密
*
* @author wym
*/
public class DESCipherCrossoverDelphi {
/**
* 密鑰
*/
public static final String KEY =
private final static String DES =
/**
* 加密
*
* @param src
* 明文(字節)
* @param key
* 密鑰
* @return 密文(字節)
* @throws Exception
*/
public static byte[] encrypt(byte[] src
// DES算法要求有一個可信任的隨機數源
SecureRandom sr = new SecureRandom();
// 從原始密匙數據創建DESKeySpec對象
DESKeySpec dks = new DESKeySpec(key);
// 創建一個密匙工廠
// 一個SecretKey對象
SecretKeyFactory keyFactory = SecretKeyFactory
SecretKey securekey = keyFactory
// Cipher對象實際完成加密操作
Cipher cipher = Cipher
// 用密匙初始化Cipher對象
cipher
// 現在
// 正式執行加密操作
return cipher
}
/**
* 解密
*
* @param src
* 密文(字節)
* @param key
* 密鑰
* @return 明文(字節)
* @throws Exception
*/
public static byte[] decrypt(byte[] src
// DES算法要求有一個可信任的隨機數源
SecureRandom sr = new SecureRandom();
// 從原始密匙數據創建一個DESKeySpec對象
DESKeySpec dks = new DESKeySpec(key);
// 創建一個密匙工廠
// 一個SecretKey對象
SecretKeyFactory keyFactory = SecretKeyFactory
SecretKey securekey = keyFactory
// Cipher對象實際完成解密操作
Cipher cipher = Cipher
// 用密匙初始化Cipher對象
cipher
// 現在
// 正式執行解密操作
return cipher
}
/**
* 加密
*
* @param src
* 明文(字節)
* @return 密文(字節)
* @throws Exception
*/
public static byte[] encrypt(byte[] src) throws Exception {
return encrypt(src
}
/**
* 解密
*
* @param src
* 密文(字節)
* @return 明文(字節)
* @throws Exception
*/
public static byte[] decrypt(byte[] src) throws Exception {
return decrypt(src
}
/**
* 加密
*
* @param src
* 明文(字符串)
* @return 密文(
* @throws Exception
*/
public final static String encrypt(String src) {
try {
return byte
} catch (Exception e) {
e
}
return null;
}
/**
* 解密
*
* @param src
* 密文(字符串)
* @return 明文(字符串)
* @throws Exception
*/
public final static String decrypt(String src) {
try {
return new String(decrypt(hex
} catch (Exception e) {
e
}
return null;
}
/**
* 加密
*
* @param src
* 明文(字節)
* @return 密文(
* @throws Exception
*/
public static String encryptToString(byte[] src) throws Exception {
return encrypt(new String(src));
}
/**
* 解密
*
* @param src
* 密文(字節)
* @return 明文(字符串)
* @throws Exception
*/
public static String decryptToString(byte[] src) throws Exception {
return decrypt(new String(src));
}
public static String byte
String hs =
String stmp =
for (int n =
stmp = (java
if (stmp
hs = hs +
else
hs = hs + stmp;
}
return hs
}
public static byte[] hex
if ((b
throw new IllegalArgumentException(
byte[] b
for (int n =
String item = new String(b
b
}
return b
}
public static void main(String[] args) {
try {
String src =
String crypto = DESCipherCrossoverDelphi
System
System
+ DESCipherCrossoverDelphi
} catch (Exception e) {
e
}
}
}
============================把文件進行解密加密===================================
public static File encrypt(File file
{
File EncFile = new File(path);
if (!EncFile
try
{
EncFile
}
catch (Exception e)
{
e
}
try
{
FileInputStream fin = new FileInputStream(file);
ByteArrayOutputStream bout = new ByteArrayOutputStream(fin
byte b[] = new byte[fin
int n;
while ((n = fin
{
byte temp[] = encrypt(b
bout
}
fin
bout
FileOutputStream fout = new FileOutputStream(EncFile);
BufferedOutputStream buffout = new BufferedOutputStream(fout);
buffout
buffout
fout
}
catch (Exception e)
{
e
}
return EncFile;
}
public static File decrypt(File file
{
File desFile = new File(path);
if (!desFile
try
{
desFile
}
catch (Exception e)
{
e
}
try
{
FileInputStream fin = new FileInputStream(file);
int i=fin
ByteArrayOutputStream bout = new ByteArrayOutputStream(i);
byte b[] = new byte[i];
int n;
while((n = fin
{
byte temp[] = decrypt(b
bout
}
fin
bout
FileOutputStream fout = new FileOutputStream(desFile);
BufferedOutputStream buffout = new BufferedOutputStream(fout);
buffout
buffout
fout
}
catch (Exception e)
{
e
}
return desFile;
}
結合JAVA
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25606.html