收到javax.crypto.IllegalBlockSizeException的错误:使用填充密码解密时,输入长度必须是8的倍数

时间:2019-04-11 04:50:40

标签: java encryption decode

每个人。我基本上是试图加密和解密字符串类型的密码。我得到的错误是 javax.crypto.IllegalBlockSizeException:使用填充密码解密时,输入长度必须是8的倍数。

我尝试使用指定的填充来执行其他算法,例如AES / CBC / NoPadding。但是出现了另一个错误java.security.InvalidKeyException:无效的AES密钥长度:5个字节。

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class Encrpyt {




public static void main(String[] args) throws Exception
{
    // TODO code application logic here

    String userName="jimmy";
    String password="chen";       
    String encryptedPassword=encrypt(password,userName);
    System.out.println("this is encrypted password:"+encryptedPassword+"");



    String decrptedPassword=decrypt(encryptedPassword,userName);
    System.out.println("this is decrpted password is :"+decrptedPassword);
}



 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="";

try {

    SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
    Cipher cipher=Cipher.getInstance("Blowfish");
    cipher.init(Cipher.DECRYPT_MODE, skeyspec);
    byte[] decrypted=cipher.doFinal(strEncrypted.getBytes());
    strData=new String(decrypted);

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


  }

1 个答案:

答案 0 :(得分:0)

您需要按如下所示填充文本字节:(请参阅内置填充的注释。)

    <?php

        namespace App;

        use Illuminate\Notifications\Notifiable;
        use Illuminate\Foundation\Auth\User as Authenticatable;

        class Employs extends Authenticatable
        {
            use Notifiable;

            protected $guard = 'Employs';

            /**
             * The attributes that are mass assignable.
             *
             * @var array
             */
            protected $fillable = [
                'name', 'email', 'password',
            ];

            /**
             * The attributes that should be hidden for arrays.
             *
             * @var array
             */
            protected $hidden = [
                'password', 'remember_token',
            ];
        }