我使用DES加密来加密/解密数据。这段代码运行正常。但是现在这个代码在带有填充错误的Linux机器上失败了,在Windows机器上我没有看到这个问题。 这些是我的观察
有谁能让我知道可能导致此问题的原因?
这是我的代码
/**
* Generate the private key using the passed string.
*
* @param keyGeneratorString
* : The string which is to be used to generate the private key.
* @return : SecretKey else null.
*/
public SecretKey getKey(String keyGeneratorString) {
SecretKeyFactory keyFactory = null;
DESKeySpec keySpec = null;
try {
// only the first 8 Bytes of the constructor argument are used
// as material for generating the keySpec
keySpec = new DESKeySpec(keyGeneratorString.getBytes("UTF-8"));
// Get the DES encryption standard instance
keyFactory = SecretKeyFactory.getInstance("DES");
// Generate and return the key.
return keyFactory.generateSecret(keySpec);
} catch (UnsupportedEncodingException uee) {
logger.error("****** Error while generating key : "
+ uee.getMessage());
} catch (InvalidKeyException ike) {
logger.error("****** Error while generating key : "
+ ike.getMessage());
} catch (NoSuchAlgorithmException e) {
logger.error("****** Error while generating key : "
+ e.getMessage());
} catch (InvalidKeySpecException e) {
logger.error("****** Error while generating key : "
+ e.getMessage());
}
// There was error while generating the key hence return null.
return null;
}
/**
* Encrypt the string using the SecretKey.
*
* @param stringToBeEncrypted
* : The String to be encrypted.
* @param key
* : The secret key to be used for encryption.
* @return : Encrypted byte[] or null.
*/
public byte[] encrypt(String stringToBeEncrypted, SecretKey key) {
Cipher cipherInst;
try {
cipherInst = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipherInst.init(Cipher.ENCRYPT_MODE, key);// cipher is not thread
// safe
byte[] encrypted = cipherInst.doFinal(stringToBeEncrypted
.getBytes());
return encrypted;
} catch (NoSuchAlgorithmException e) {
logger.error("****** Error while encrypting : "
+ e.getMessage());
} catch (NoSuchPaddingException e) {
logger.error("****** Error while encrypting : "
+ e.getMessage());
} catch (InvalidKeyException e) {
logger.error("****** Error while encrypting : "
+ e.getMessage());
} catch (IllegalBlockSizeException e) {
logger.error("****** Error while encrypting : "
+ e.getMessage());
} catch (BadPaddingException e) {
logger.error("****** Error while encrypting : "
+ e.getMessage());
}
return null;
}
/**
* Decrypt the string using the SecretKey.
*
* @param stringToBeDecrypted : byte[] to be decrypted.
* @param key : The secret key to be used for decryption.
* @return : Decrypted byte[] or null.
*/
public byte[] decrypt(byte[] stringToBeDecrypted, SecretKey key) {
Cipher cipherInst;
try {
cipherInst = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipherInst.init(Cipher.DECRYPT_MODE, key);
byte[] original = cipherInst
.doFinal(stringToBeDecrypted);
return original;
} catch (NoSuchAlgorithmException e) {
logger.error("****** Error while decrypting : "
+ e.getMessage());
} catch (NoSuchPaddingException e) {
logger.error("****** Error while decrypting : "
+ e.getMessage());
} catch (InvalidKeyException e) {
logger.error("****** Error while decrypting : "
+ e.getMessage());
} catch (IllegalBlockSizeException e) {
logger.error("****** Error while decrypting : "
+ e.getMessage());
} catch (BadPaddingException e) {
logger.error("****** Error while decrypting : "
+ e.getMessage());
}
return null;
}
答案 0 :(得分:2)
将字符串转换为始终使用相同编码的字节时要小心。这一行
byte[] encrypted = cipherInst.doFinal(stringToBeEncrypted
.getBytes());
使用您机器的默认字符编码,在Linux和Windows之间可能会有所不同。使用像“UTF8”这样的一致编码来生成keySpec
。
byte[] encrypted = cipherInst.doFinal(stringToBeEncrypted
.getBytes("UTF-8"));
请注意,“填充错误”通常不是这样的;这是一个非常误导性的错误信息。