【发布时间】:2015-03-28 21:56:18
【问题描述】:
我正在尝试从 MySQL 数据库中解密密码,该密码以字节形式存储为 varbinary。当我将它放在解密算法中时,我得到给定的最终块没有正确填充错误,即使它在我在没有数据库的情况下测试它时也有效。我不确定我哪里出错了。
下面是我的代码:
public boolean selectCheckUser(String username, String password) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, Exception{
Key symKey = KeyGenerator.getInstance(algorithm).generateKey();
Cipher c = Cipher.getInstance(algorithm);
String checkUsername = null;
String checkPassword = null;
byte[] cipherPassword = null;
boolean check = false;
try{
stmt = (PreparedStatement) connect.prepareStatement("SELECT username, password FROM userdetails WHERE username = ?");
stmt.setString(1,username);
ResultSet data = stmt.executeQuery();
if(data.next()){
checkUsername = data.getString("username");
}
cipherPassword = data.getBytes("password");
System.out.println(data.getBytes("password"));
checkPassword = Encrypt.decryptPassword(cipherPassword, symKey, c);
这里是解密算法:
public static String decryptPassword(byte[] encryptionBytes, Key pkey, Cipher c) throws InvalidKeyException,
BadPaddingException, IllegalBlockSizeException, Exception {
c.init(Cipher.DECRYPT_MODE, pkey);
byte[] decrypt = c.doFinal(encryptionBytes);
String decrypted = new String(decrypt);
return decrypted;
}
【问题讨论】:
标签: java mysql sql encryption padding