无法解密密码抛出javax.crypto.BadPaddingException:最终块未正确填充

时间:2018-02-23 11:25:18

标签: java encryption aes

在JDK 1_7中,我试图解密加密的密码。从结果集(rs2)开始,加密的密码被传递给字符串变量,然后调用decrypt方法并传递字符串' p_password&#39 ;解密密码系统时抛出" 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);

// EncryptDecrypt class

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 个答案:

没有答案
相关问题