有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法解密密码抛出javax。加密。BadPaddingException:最后一个块没有正确填充

在JDK 1_7中,我试图解密加密的密码。从结果集(rs2)中,加密密码被传递给字符串变量,然后调用decrypt方法并传递字符串“p_password”和密钥。解密密码时,系统抛出“javax.crypto.BadPaddingException:给定的最后一个块没有正确填充” 有人能就如何解决这个问题提出建议吗
//从结果集中检索加密密码

 String key = "Blowfish";
 String p_password = rs2.getString("USER_PASSWORD");
 EncryptDecrypt encryptdecrypt = new EncryptDecrypt();  
 String p_decryptedPassword = encryptdecrypt.decrypt(p_password, key);

//Encrypt Decrypt类

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

    public class EncryptDecrypt {   
    public static String encrypt(String strClearText, String strKey) throws Exception{
            String strData="";

            try {
                SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
                Cipher cipher=Cipher.getInstance("Blowfish");
                cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
                byte[] encrypted=cipher.doFinal(strClearText.getBytes());
                strData=new String(encrypted);

            } catch (Exception e) {
                e.printStackTrace();
                throw new Exception(e);
            }
            return strData;
        }

        public static String decrypt(String strEncrypted,String strKey) throws Exception{
            String strData=null;

            try {
                byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0 };
                IvParameterSpec ivspec = new IvParameterSpec(iv);
                SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
                Cipher cipher=Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, skeyspec, ivspec);
                byte[] decrypted=cipher.doFinal(strEncrypted.getBytes());
                strData= new String(decrypted); 

            } catch (Exception e) {
                e.printStackTrace();
                throw new Exception(e);
            }
            return strData;

        }


    }

共 (0) 个答案