java对称加密报错:Input length must be multiple of 8 when decrypting with padded cipher

HTTP Status 500 - Request processing failed; nested exception is javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Here is my Encryption and Decryption code

//secret key 8
    private static String strkey ="Blowfish";

UPDATED

 //encrypt using blowfish algorithm
    public static byte[] encrypt(String Data)throws Exception{

        SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
         Cipher cipher = Cipher.getInstance("Blowfish");
         cipher.init(Cipher.ENCRYPT_MODE, key);

         return (cipher.doFinal(Data.getBytes("UTF8")));

    }

    //decrypt using blow fish algorithm
    public static String decrypt(byte[] encryptedData)throws Exception{
         SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
         Cipher cipher = Cipher.getInstance("Blowfish");
         cipher.init(Cipher.DECRYPT_MODE, key);
         byte[] decrypted = cipher.doFinal(encryptedData);
         return new String(decrypted); 

    }

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

import org.apache.commons.codec.binary.Base64;

public class Test {

    private static String strkey ="Blowfish";    private static Base64 base64 = new Base64(true);

     //encrypt using blowfish algorithm    public static String encrypt(String Data)throws Exception{

        SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");         Cipher cipher = Cipher.getInstance("Blowfish");         cipher.init(Cipher.ENCRYPT_MODE, key);

         return base64.encodeToString(cipher.doFinal(Data.getBytes("UTF8")));

    }

    //decrypt using blow fish algorithm    public static String decrypt(String encrypted)throws Exception{        byte[] encryptedData = base64.decodeBase64(encrypted);         SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");         Cipher cipher = Cipher.getInstance("Blowfish");         cipher.init(Cipher.DECRYPT_MODE, key);         byte[] decrypted = cipher.doFinal(encryptedData);         return new String(decrypted); 

    }

    public static void main(String[] args) throws Exception {        String data = "will this work?";        String encoded = encrypt(data);        System.out.println(encoded);        String decoded = decrypt(encoded);        System.out.println(decoded);    }}

Hope this answers your questions.

时间: 2024-08-26 17:51:31

java对称加密报错:Input length must be multiple of 8 when decrypting with padded cipher的相关文章

Base64加密解密不一致导致AES加密报出javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher

报错的意思的是使用该种解密方式出入长度应为16bit的倍数,但实际的错误却不是这个,错误原因根本上是因为在http请求是特殊字符编码错误,具体数就是base64生成的+号,服务器接收时成了空格,然后导致base64解码出的字节数组有改变. 下面来还原并分析一下具体原因: 请求代码: @Test public void testHttp() throws Exception { //16进制字节数组,单纯的为了生成含有+的base64位字符串,是在试了好多遍都没试到+,只能把我错误例子的byte数

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher--转载

原文地址:http://songjianyong.iteye.com/blog/1571029 /** * AESHelper.java * cn.com.songjy.test * * Function: TODO * * ver date author * ────────────────────────────────── * 2012-6-29 songjianyong * * Copyright (c) 2012, TNT All Rights Reserved. */ package

java 对称加解密

对称加密 1.特点 加密密钥与解密密钥相同 2.分类 2.1 DES 2.2 3DES 2.3 AES 2.4 PBE 3.各种加密示例 3.1 DES import java.security.Key; import java.security.Security; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.Sec

spring security 5.0 密码未加密报错

使用spring security5.0后,配置文件中直接写普通的密码如:123456,会报错: java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null" 这是因为spring security5.0以后默认需要选择密码加密方式,如果还像之前版本直接配置未加密密码,就会报上面这个错误当然啦,如果还想用简单密码的话,spring security还是给了两个方案,一种是

scala文件读取报错“java.nio.charset.MalformedInputException: Input length = 1”

今天写spark程序的时候遇到了一个问题就是,读取文件的时候报了一个错:"Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1" 读取文件的代码如下: 一看这个这个错"nio"错误,第一感觉就是读文件方法这里出了问题,于是点击去看了一下Source.fromFile这个方法的源码: 果然,这个方法的重载有好几个,但是每一都直接或间

我的Java开发学习之旅------>Java NIO 报java.nio.charset.MalformedInputException: Input length = 1异常

今天在使用Java NIO的Channel和Buffer进行文件操作时候,报了java.nio.charset.MalformedInputException: Input length = 1异常,具体如下: java.nio.charset.MalformedInputException: Input length = 1 at java.nio.charset.CoderResult.throwException(CoderResult.java:260) at java.nio.char

阿里巴巴 fastjson-1.2.12.jar json解析异常java.lang.ClassFormatError: Invalid method Code length 66865 in class file com/alibaba/fastjson/serializer/ASMSerializer_6_UserKdlb

承接上篇:fastjson反序列化LocalDateTime失败的问题java.time.format.DateTimeParseException: Text '2019-05-24 13:52:11' could not be parsed at index 10 之前在线上用的版本是fastjson-1.2.7.jar 一切正常,更换以后时间解析看似一切正常. 因为在系统中设计json反序列化的地方比较多,刚刚放到生产环境,app那边的接口报错了 java.lang.ClassFormat

java如何加载本地的dll文件

首先,应当明确,dll有两类:(1)Java所依赖的dll和,(2)dll所依赖的dll.正是由于第(2)种dll的存在,才导致了java中加载dll的复杂性大大增加,许多说法都是这样的,但我实验的结果却表明似乎没有那么复杂,后面会予以详细阐述. 其次,Java中加载dll的方式也有两种:(1)通过调用System.loadLibrary(String filename)和,(2)通过调用System.load(String filename)方法.其底层都是通过使用ClassLoader中的l

php des 对称加解密类

<?php header("Content-Type: text/html;charset=utf-8"); /** * des 对称加解密 */ class des { private $key = ''; private $cipher = MCRYPT_DES; //加解密算法 private $modes = MCRYPT_MODE_ECB; //算法模式 private $iv = ''; //初始化向量 /** * 密钥 */ public function __co