你好, banq,请教一个问题?

你好banq,看看这个加密程序,我对用户密码进行了加密,有时候添加的用户密码,在登录时能正确解析,但有时候添加的用户密码在登录时却报以下错误:IllegalBlockSizeException: Input length (with padding) not multiple of 8 bytes
你看看程序:
package com.moloon.javajc.encrypt;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* 加密程序,用于对用户密码加密
*
* @author lvyg
*
*/
public class InfoEncrypt {
/**
* 加密数据
*/
private byte[] encryptedData;

// private Log log = LogFactory.getLog(InfoEncrypt.class);
/**
* 单密钥加密算法(DES加密算法)-生成密钥
*
* @param args
* @throws NoSuchAlgorithmException
*/
public String generateKey() {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();

// 为选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = null;
try {
kg = KeyGenerator.getInstance("DES");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
kg.init(sr);

// 生成密匙
SecretKey key = kg.generateKey();

// 获取密匙数据
byte rawKeyData[] = key.getEncoded();

// 保存密匙
return doSaveAsKey(rawKeyData);
}

/**
* 单密钥加密算法(DES加密算法)-加密数据
*
* @param args
* @throws NoSuchAlgorithmException
*/
public String singleKeyEncode(String srcDate, String sysTime) {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();

// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = null;
byte rawKeyData[] = getResKey(sysTime);
try {
dks = new DESKeySpec(rawKeyData);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
// 创建一个密匙工厂,然后用它把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[] = new byte[10];/* 用某种方法获取数据 */

// ///test data/////
byte data[] = srcDate.getBytes();
// ///end/////

// 正式执行加密操作
byte[] encryptedData = cipher.doFinal(data);

this.encryptedData = encryptedData; // 将加密数据赋给成员变量
// 进一步处理加密后的数据
// doSomething(encryptedData);
// System.out.println("原始明文为:" + srcDate);

System.out.print("加密后密文为-->" + new String(encryptedData));

return new String(encryptedData);

} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("InvalidKeyException-->" + e);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("NoSuchAlgorithmException-->" + e);
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("InvalidKeySpecException-->" + e);
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("NoSuchPaddingException-->" + e);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("IllegalStateException-->" + e);
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("IllegalBlockSizeException-->" + e);
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("BadPaddingException-->" + e);
}
return null;
}

/**
* 单密钥加密算法(DES加密算法)-解密数据
*
* @param resKeyData
* @param dataEncrypted
*/
public String singleKeyDecode(byte[] resKeyData, byte[] dataEncrypted) {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();

// 获取原始密匙数据
byte rawKeyData[] = resKeyData;

try {
// 从原始密匙数据创建一个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[] = this.encryptedData; /* 获得经过加密的数据 */

// 正式执行解密操作
byte decryptedData[] = cipher.doFinal(dataEncrypted);

System.out.println("还原后明文为:" + new String(decryptedData));
return new String(decryptedData);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("InvalidKeyException: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("NoSuchAlgorithmException: " + e.getMessage());
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("InvalidKeySpecException: " + e.getMessage());
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("NoSuchPaddingException: " + e.getMessage());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("IllegalStateException: " + e.getMessage());
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("IllegalBlockSizeException: " + e.getMessage());
// IllegalBlockSizeException: Input length (with padding) not
// multiple of 8 bytes
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("BadPaddingException: " + e.getMessage());
}
return null;
}

/**
* 单密钥加密算法(DES加密算法)-保存密钥
*
* @param rawKeyData
*/
public String doSaveAsKey(byte rawKeyData[]) {
String sysTime = new Long(System.currentTimeMillis()).toString();
// String keySingleFile =
// "/usr/local/apache/htdocs/java/WEB-INF/encryptkey/"
// + sysTime;
String keySingleFile = "c:/" + sysTime;

try {
FileOutputStream keySave = new FileOutputStream(keySingleFile);
// FileInput
keySave.write(rawKeyData);
keySave.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sysTime;
// System.out.print("密钥为:");
// for (int i = 0; i < rawKeyData.length; i++) {
// System.out.print(rawKeyData);
// }
// System.out.println();
}

/**
* 单密钥加密算法(DES加密算法)-提取密钥
*
* @return
*/
public byte[] getResKey(String onlyFileName) {
// String keySingleFile =
// "/usr/local/apache/htdocs/java/WEB-INF/encryptkey/"
// + onlyFileName;
String keySingleFile = "c:/" + onlyFileName;
// byte[] rawKeyData = null;
System.out.println("onlyFileName: " + keySingleFile);
String kk = "";
try {
FileInputStream keyRead = new FileInputStream(keySingleFile);
int c = 0;
while ((c = keyRead.read()) != -1) {
kk = kk + Integer.toString(c);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return kk.getBytes();
}

/**
* 单密钥测试主程序
*
* @param args
*/
public static void main(String[] args) {
InfoEncrypt pe = new InfoEncrypt();
// 测试明文
// String test = "ABCabc123$&@#-_889~!+-*/.";
String test = "prodread";
// 密钥生成
String sysTime = pe.generateKey();
System.out.println("sysTime-->" + sysTime);
// 加密
pe.singleKeyEncode(test, new Long(sysTime).toString());

// 解密
pe.singleKeyDecode(pe.getResKey(sysTime), pe.encryptedData);

}

/**
* 公开密钥加密算法-RSA加密算法
*
* @param args
*/
public void publicKeyEncrypt(String[] args) {
// TODO Auto-generated method stub
String publicKeyFile = "c:/csc821public.txt";
String privateKeyFile = "c:/csc821private.txt";

try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
PrivateKey priv = key.getPrivate();
PublicKey pub = key.getPublic();
byte[] key1 = pub.getEncoded();
FileOutputStream keyfos1 = new FileOutputStream(publicKeyFile);
// FileInput
keyfos1.write(key1);
keyfos1.close();
byte[] key2 = priv.getEncoded();

FileOutputStream keyfos2 = new FileOutputStream(privateKeyFile);
keyfos2.write(key2);

keyfos2.close();
} catch (Exception e) {
System.err.println("异常: " + e.toString());
}
}

}