public
class
DES
...
{
private static byte[] key;
private static byte[] iv;
static ...{
try ...{
key = new sun.misc.BASE64Decoder().decodeBuffer(""); //密钥字符串
iv = new sun.misc.BASE64Decoder().decodeBuffer(""); //初始化向量字符串
}
catch (Exception e) ...{
}
}
public static String encrypt(String data) ...{
String code = null;
try ...{
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DESede/CBC/PKCS5Padding");
javax.crypto.SecretKeyFactory skf = javax.crypto.SecretKeyFactory.getInstance("DESede");
javax.crypto.SecretKey sk = skf.generateSecret(new javax.crypto.spec.DESedeKeySpec(key));
javax.crypto.spec.IvParameterSpec ips = new javax.crypto.spec.IvParameterSpec(iv);
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, sk, ips);
byte[] b = cipher.doFinal(data.getBytes("UTF-8"));
code = new sun.misc.BASE64Encoder().encode(b);
code = code.replaceAll(" ", "");
code = code.replaceAll(" ", "");;
}
catch (Exception ex) ...{
ex.printStackTrace();
}
return code;
}
public static String decrypt(String data) ...{
String code = null;
try ...{
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DESede/CBC/PKCS5Padding");
javax.crypto.SecretKeyFactory skf = javax.crypto.SecretKeyFactory.getInstance("DESede");
javax.crypto.SecretKey sk = skf.generateSecret(new javax.crypto.spec.DESedeKeySpec(key));
javax.crypto.spec.IvParameterSpec ips = new javax.crypto.spec.IvParameterSpec(iv);
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, sk, ips);
code = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(data)), "UTF-8");
}
catch (Exception ex) ...{
ex.printStackTrace();
}
return code;
}
}
private static byte[] key;
private static byte[] iv;
static ...{
try ...{
key = new sun.misc.BASE64Decoder().decodeBuffer(""); //密钥字符串
iv = new sun.misc.BASE64Decoder().decodeBuffer(""); //初始化向量字符串
}
catch (Exception e) ...{
}
}
public static String encrypt(String data) ...{
String code = null;
try ...{
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DESede/CBC/PKCS5Padding");
javax.crypto.SecretKeyFactory skf = javax.crypto.SecretKeyFactory.getInstance("DESede");
javax.crypto.SecretKey sk = skf.generateSecret(new javax.crypto.spec.DESedeKeySpec(key));
javax.crypto.spec.IvParameterSpec ips = new javax.crypto.spec.IvParameterSpec(iv);
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, sk, ips);
byte[] b = cipher.doFinal(data.getBytes("UTF-8"));
code = new sun.misc.BASE64Encoder().encode(b);
code = code.replaceAll(" ", "");
code = code.replaceAll(" ", "");;
}
catch (Exception ex) ...{
ex.printStackTrace();
}
return code;
}
public static String decrypt(String data) ...{
String code = null;
try ...{
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DESede/CBC/PKCS5Padding");
javax.crypto.SecretKeyFactory skf = javax.crypto.SecretKeyFactory.getInstance("DESede");
javax.crypto.SecretKey sk = skf.generateSecret(new javax.crypto.spec.DESedeKeySpec(key));
javax.crypto.spec.IvParameterSpec ips = new javax.crypto.spec.IvParameterSpec(iv);
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, sk, ips);
code = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(data)), "UTF-8");
}
catch (Exception ex) ...{
ex.printStackTrace();
}
return code;
}
}