package comtxltest;
import javasecuritySecureRandom;
import javaxcryptoCipher;
import javaxcryptoSecretKey;
import javaxcryptoSecretKeyFactory;
import javaxcryptospecDESKeySpec;
/**
* DES加解密支持与delphi交互(字符串编码需统一为UTF)
*
* @author wym
*/
public class DESCipherCrossoverDelphi {
/**
* 密钥
*/
public static final String KEY = uGquZ;
private final static String DES = DES;
/**
* 加密
*
* @param src
* 明文(字节)
* @param key
* 密钥长度必须是的倍数
* @return 密文(字节)
* @throws Exception
*/
public static byte[] encrypt(byte[] src byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂然后用它把DESKeySpec转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactorygetInstance(DES);
SecretKey securekey = keyFactorygenerateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = CiphergetInstance(DES);
// 用密匙初始化Cipher对象
cipherinit(CipherENCRYPT_MODE securekey sr);
// 现在获取数据并加密
// 正式执行加密操作
return cipherdoFinal(src);
}
/**
* 解密
*
* @param src
* 密文(字节)
* @param key
* 密钥长度必须是的倍数
* @return 明文(字节)
* @throws Exception
*/
public static byte[] decrypt(byte[] src byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂然后用它把DESKeySpec对象转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactorygetInstance(DES);
SecretKey securekey = keyFactorygenerateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = CiphergetInstance(DES);
// 用密匙初始化Cipher对象
cipherinit(CipherDECRYPT_MODE securekey sr);
// 现在获取数据并解密
// 正式执行解密操作
return cipherdoFinal(src);
}
/**
* 加密
*
* @param src
* 明文(字节)
* @return 密文(字节)
* @throws Exception
*/
public static byte[] encrypt(byte[] src) throws Exception {
return encrypt(src KEYgetBytes());
}
/**
* 解密
*
* @param src
* 密文(字节)
* @return 明文(字节)
* @throws Exception
*/
public static byte[] decrypt(byte[] src) throws Exception {
return decrypt(src KEYgetBytes());
}
/**
* 加密
*
* @param src
* 明文(字符串)
* @return 密文(进制字符串)
* @throws Exception
*/
public final static String encrypt(String src) {
try {
return bytehex(encrypt(srcgetBytes() KEYgetBytes()));
} catch (Exception e) {
eprintStackTrace();
}
return null;
}
/**
* 解密
*
* @param src
* 密文(字符串)
* @return 明文(字符串)
* @throws Exception
*/
public final static String decrypt(String src) {
try {
return new String(decrypt(hexbyte(srcgetBytes()) KEYgetBytes()));
} catch (Exception e) {
eprintStackTrace();
}
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 bytehex(byte[] b) {
String hs = ;
String stmp = ;
for (int n = ; n < blength; n++) {
stmp = (javalangIntegertoHexString(b[n] & XFF));
if (stmplength() == )
hs = hs + + stmp;
else
hs = hs + stmp;
}
return hstoUpperCase();
}
public static byte[] hexbyte(byte[] b) {
if ((blength % ) != )
throw new IllegalArgumentException(长度不是偶数);
byte[] b = new byte[blength / ];
for (int n = ; n < blength; n += ) {
String item = new String(b n );
b[n / ] = (byte) IntegerparseInt(item );
}
return b;
}
public static void main(String[] args) {
try {
String src = hello;
String crypto = DESCipherCrossoverDelphiencrypt(src);
Systemoutprintln(密文[ + src + ]: + crypto);
Systemoutprintln(解密后:
+ DESCipherCrossoverDelphidecrypt(crypto));
} catch (Exception e) {
eprintStackTrace();
}
}
}
============================把文件进行解密加密===================================
public static File encrypt(File file String path)
{
File EncFile = new File(path);
if (!EncFileexists())
try
{
EncFilecreateNewFile();
}
catch (Exception e)
{
eprintStackTrace();
}
try
{
FileInputStream fin = new FileInputStream(file);
ByteArrayOutputStream bout = new ByteArrayOutputStream(finavailable());
byte b[] = new byte[finavailable()];
int n;
while ((n = finread(b)) != )
{
byte temp[] = encrypt(b keygetBytes());
boutwrite(temp templength);
}
finclose();
boutclose();
FileOutputStream fout = new FileOutputStream(EncFile);
BufferedOutputStream buffout = new BufferedOutputStream(fout);
buffoutwrite(bouttoByteArray());
buffoutclose();
foutclose();
}
catch (Exception e)
{
eprintStackTrace();
}
return EncFile;
}
public static File decrypt(File file String path)
{
File desFile = new File(path);
if (!desFileexists())
try
{
desFilecreateNewFile();
}
catch (Exception e)
{
eprintStackTrace();
}
try
{
FileInputStream fin = new FileInputStream(file);
int i=finavailable()finavailable()%keylength();
ByteArrayOutputStream bout = new ByteArrayOutputStream(i);
byte b[] = new byte[i];
int n;
while((n = finread(b)) !=)
{
byte temp[] = decrypt(b keygetBytes());
boutwrite(temp templength);
}
finclose();
boutclose();
FileOutputStream fout = new FileOutputStream(desFile);
BufferedOutputStream buffout = new BufferedOutputStream(fout);
buffoutwrite(bouttoByteArray());
buffoutclose();
foutclose();
}
catch (Exception e)
{
eprintStackTrace();
}
return desFile;
}
结合JAVAIOZipOutputStream 可以用来加密解密压缩文件