###DES加密
生成DES加密KEY
DES的KEY:abcdefgh 偏移量:abcdefgh 填充模式:DES/CBC/PKCS5Padding
1.先DES加密
2.再base64编码
如123456,进行DES加密,得到byte数组
再进行Base64编码:opfVWWpnmcY=
Java代码:
加密:
public static String encrypt(String data, String desKey) throws Exception {
if (data == null) return null;
DESKeySpec dks = new DESKeySpec(desKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, securekey, new IvParameterSpec(desKey.getBytes()));
return new BASE64Encoder().encode(cipher.doFinal(data.getBytes()));
}
解密:
public static String decrypt(String data, String desKey) throws Exception {
if (data == null) return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
DESKeySpec dks = new DESKeySpec(desKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, securekey, new IvParameterSpec(desKey.getBytes()));
byte[] bt= cipher.doFinal(buf);
return new String(bt);
}