当前位置: 首页 > 知识库问答 >
问题:

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

能可人
2023-03-14

所有人。我基本上是试图加密和解密一个字符串类型的密码。我得到的错误是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个答案

公孙鸿才
2023-03-14

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



    public static final String BLOWFISH_CIPHER = "Blowfish/ECB/NOPADDING";
    public static final String BLOWFISH_SECRET_KEY_SPEC = "Blowfish";

    public static String toHexString(byte[] array) {
        return DatatypeConverter.printHexBinary(array);
    }

    public static byte[] toByteArray(String s) {
        return DatatypeConverter.parseHexBinary(s);
    }

    public static byte[] pad(byte[] bytes) {
        int remainder = bytes.length % 8;
        if(remainder == 0){
            return bytes;
        }
        int padding = 8 - remainder;
        int newLength = padding+bytes.length;
        byte[] newBytes = new byte[newLength];
        Arrays.fill(newBytes,(byte)0);
        System.arraycopy(bytes,0,newBytes,padding,bytes.length);
        return newBytes;
    }

    public static String encryptBF(String string, String key) throws GeneralSecurityException {

        Cipher cipher = Cipher.getInstance(BLOWFISH_CIPHER);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), BLOWFISH_SECRET_KEY_SPEC);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

        return toHexString(cipher.doFinal(pad(string.getBytes())));
    }

    public static String decryptBF(String string, String key) throws GeneralSecurityException {

        Cipher cipher = Cipher.getInstance(BLOWFISH_CIPHER);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), BLOWFISH_SECRET_KEY_SPEC);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

        return new String(cipher.doFinal(toByteArray(string)));
    }
 类似资料: