DES加密解密
package com.sinosoft.olyvem.common;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Encoder;


public class DES ...{
private byte[] desKey;


public DES(byte[] desKey) ...{
this.desKey = desKey;
}


public byte[] doEncrypt(byte[] plainText) throws Exception ...{
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();

byte rawKeyData[] = desKey;/**//* 用某种方法获得密匙数据 */
// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(rawKeyData);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, key, sr);
// 现在,获取数据并加密

byte data[] = plainText;/**//* 用某种方法获取数据 */
// 正式执行加密操作
byte encryptedData[] = cipher.doFinal(data);
return encryptedData;
}


public byte[] doDecrypt(byte[] encryptText) throws Exception ...{
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();

byte rawKeyData[] = desKey; /**//* 用某种方法获取原始密匙数据 */
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(rawKeyData);
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, key, sr);
// 现在,获取数据并解密

byte encryptedData[] = encryptText;/**//* 获得经过加密的数据 */
// 正式执行解密操作
byte decryptedData[] = cipher.doFinal(encryptedData);
return decryptedData;
}


public static void main(String[] args) throws Exception ...{
String key = "FtpXPass";
String value = "olympic";
BASE64Encoder base64Encoder = new BASE64Encoder();

DES desEncrypt = new DES(key.getBytes());
byte[] encryptText = desEncrypt.doEncrypt(value.getBytes());
//System.out.println("doEncrypt - " + toHexString(encryptText));
System.out.println("doEncrypt - "
+ base64Encoder.encode(encryptText));
byte[] decryptText = desEncrypt.doDecrypt("r9NGYcKAtdo=".getBytes());
System.out.println("doDecrypt - " + new String(decryptText));
//System.out.println("doDecrypt - " + toHexString(decryptText));

}


public static String toHexString(byte[] value) ...{
String newString = "";

for (int i = 0; i < value.length; i++) ...{
byte b = value[i];
String str = Integer.toHexString(b);

if (str.length() > 2) ...{
str = str.substring(str.length() - 2);
}

if (str.length() < 2) ...{
str = "0" + str;
}
newString += str;
}
return newString.toUpperCase();
}

}

报错啊~~
有错呀楼主,异常啦 javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher