DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
获取的key.getBytes()长度要>=24,在小于24时会报错,添加如下判断:
public static String encrypt(String encryptText, String key) {
if (encryptText == null || key == null) {
throw new IllegalArgumentException("encryptText or key must not be null");
}
try {
byte[] keyBytes = key.getBytes();
int byteLen = keyBytes.length;
if (byteLen < 24) { // short key ? .. extend to 24 byte key
byte[] tmpKey = new byte[24];
System.arraycopy(keyBytes, 0, tmpKey, 0, byteLen);
keyBytes = tmpKey;
}
DESKeySpec desKeySpec = new DESKeySpec(keyBytes);
解密也添加相同的